[#144] Support well-known application attributes for container in CLI

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-11-03 16:44:50 +03:00 committed by Alex Vanin
parent 62bd22a379
commit 83926647d7
2 changed files with 48 additions and 6 deletions

View file

@ -44,6 +44,8 @@ var (
containerPolicy string containerPolicy string
containerAttributes []string containerAttributes []string
containerAwait bool containerAwait bool
containerName string
containerNoTimestamp bool
containerID string containerID string
@ -477,6 +479,8 @@ func init() {
"comma separated pairs of container attributes in form of Key1=Value1,Key2=Value2") "comma separated pairs of container attributes in form of Key1=Value1,Key2=Value2")
createContainerCmd.Flags().StringVarP(&containerNonce, "nonce", "n", "", "UUIDv4 nonce value for container") createContainerCmd.Flags().StringVarP(&containerNonce, "nonce", "n", "", "UUIDv4 nonce value for container")
createContainerCmd.Flags().BoolVar(&containerAwait, "await", false, "block execution until container is persisted") createContainerCmd.Flags().BoolVar(&containerAwait, "await", false, "block execution until container is persisted")
createContainerCmd.Flags().StringVar(&containerName, "name", "", "container name attribute")
createContainerCmd.Flags().BoolVar(&containerNoTimestamp, "disable-timestamp", false, "disable timestamp container attribute")
// container delete // container delete
deleteContainerCmd.Flags().StringVar(&containerID, "cid", "", "container ID") deleteContainerCmd.Flags().StringVar(&containerID, "cid", "", "container ID")
@ -537,7 +541,7 @@ func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error)
} }
func parseAttributes(attributes []string) ([]*v2container.Attribute, error) { func parseAttributes(attributes []string) ([]*v2container.Attribute, error) {
result := make([]*v2container.Attribute, 0, len(attributes)) result := make([]*v2container.Attribute, 0, len(attributes)+2) // name + timestamp attributes
for i := range attributes { for i := range attributes {
kvPair := strings.Split(attributes[i], attributeDelimiter) kvPair := strings.Split(attributes[i], attributeDelimiter)
@ -552,6 +556,22 @@ func parseAttributes(attributes []string) ([]*v2container.Attribute, error) {
result = append(result, parsedAttribute) result = append(result, parsedAttribute)
} }
if !containerNoTimestamp {
timestamp := new(v2container.Attribute)
timestamp.SetKey(container.AttributeTimestamp)
timestamp.SetValue(strconv.FormatInt(time.Now().Unix(), 10))
result = append(result, timestamp)
}
if containerName != "" {
cnrName := new(v2container.Attribute)
cnrName.SetKey(container.AttributeName)
cnrName.SetValue(containerName)
result = append(result, cnrName)
}
return result, nil return result, nil
} }
@ -646,6 +666,15 @@ func prettyPrintContainer(cnr *container.Container, jsonEncoding bool) {
} }
for _, attribute := range cnr.GetAttributes() { for _, attribute := range cnr.GetAttributes() {
if attribute.GetKey() == container.AttributeTimestamp {
fmt.Printf("attribute: %s=%s (%s)\n",
attribute.GetKey(),
attribute.GetValue(),
prettyPrintUnixTime(attribute.GetValue()))
continue
}
fmt.Printf("attribute: %s=%s\n", attribute.GetKey(), attribute.GetValue()) fmt.Printf("attribute: %s=%s\n", attribute.GetKey(), attribute.GetValue())
} }

View file

@ -6,6 +6,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strconv"
"time"
"github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-api-go/pkg/token"
@ -178,3 +180,14 @@ func prettyPrintJSON(cmd *cobra.Command, data []byte) {
cmd.Println(buf) cmd.Println(buf)
} }
func prettyPrintUnixTime(s string) string {
unixTime, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return "malformed"
}
timestamp := time.Unix(unixTime, 0)
return timestamp.String()
}