From 83926647d76b8da02757ff4ab31af4e07f3a0558 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 3 Nov 2020 16:44:50 +0300 Subject: [PATCH] [#144] Support well-known application attributes for container in CLI Signed-off-by: Alex Vanin --- cmd/neofs-cli/modules/container.go | 41 +++++++++++++++++++++++++----- cmd/neofs-cli/modules/util.go | 13 ++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 877d15ce8..4f2b82b2a 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -39,11 +39,13 @@ const ( var ( containerOwner string - containerACL string - containerNonce string - containerPolicy string - containerAttributes []string - containerAwait bool + containerACL string + containerNonce string + containerPolicy string + containerAttributes []string + containerAwait bool + containerName string + containerNoTimestamp bool containerID string @@ -477,6 +479,8 @@ func init() { "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().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 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) { - result := make([]*v2container.Attribute, 0, len(attributes)) + result := make([]*v2container.Attribute, 0, len(attributes)+2) // name + timestamp attributes for i := range attributes { kvPair := strings.Split(attributes[i], attributeDelimiter) @@ -552,6 +556,22 @@ func parseAttributes(attributes []string) ([]*v2container.Attribute, error) { 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 } @@ -646,6 +666,15 @@ func prettyPrintContainer(cnr *container.Container, jsonEncoding bool) { } 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()) } diff --git a/cmd/neofs-cli/modules/util.go b/cmd/neofs-cli/modules/util.go index e10413bca..e2576adec 100644 --- a/cmd/neofs-cli/modules/util.go +++ b/cmd/neofs-cli/modules/util.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "io/ioutil" + "strconv" + "time" "github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg/token" @@ -178,3 +180,14 @@ func prettyPrintJSON(cmd *cobra.Command, data []byte) { 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() +}