X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=internal%2Fcli%2Fkeys.go;fp=internal%2Fcli%2Fkeys.go;h=33dce25b32843f06f0f411fbafe581d829ddc7ca;hb=273d032db02901dba8deeb692f92ed2366dc6ee5;hp=0000000000000000000000000000000000000000;hpb=7c256b622c8fd065e91a7e289937d6e692a7eb1d;p=ric-plt%2Fsdlgo.git diff --git a/internal/cli/keys.go b/internal/cli/keys.go new file mode 100644 index 0000000..33dce25 --- /dev/null +++ b/internal/cli/keys.go @@ -0,0 +1,98 @@ +/* + Copyright (c) 2021 AT&T Intellectual Property. + Copyright (c) 2018-2021 Nokia. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/* + * This source code is part of the near-RT RIC (RAN Intelligent Controller) + * platform project (RICP). + */ + +package cli + +import ( + "fmt" + "os" + "sort" + + "gerrit.o-ran-sc.org/r/ric-plt/sdlgo" + "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis" + "github.com/spf13/cobra" +) + +func init() { + getCmd.AddCommand(newKeysCmd(func() ISyncStorage { + return sdlgo.NewSyncStorage() + })) +} + +var ( + keysLong = `List keys in the given namespace matching key search pattern.` + + keysExample = ` # List all keys in the given namespace. + sdlcli get keys sdlns + # List keys in the given namespace matching given key search pattern. + sdlcli get keys sdlns 'he*' + + Supported search glob-style patterns: + h?llo matches hello, hallo and hxllo + h*llo matches hllo and heeeello + h[ae]llo matches hello and hallo, but not hillo + h[^e]llo matches hallo, hbllo, ... but not hello + h[a-b]llo matches hallo and hbllo + + The \ escapes character in key search pattern and those will be treated as a normal + character: + h\[?llo\* matches h[ello* and h[allo*` +) + +func newKeysCmd(sdlCb SyncStorageCreateCb) *cobra.Command { + return &cobra.Command{ + Use: "keys [pattern|default '*']", + Short: "List keys in the given namespace matching key search pattern", + Long: keysLong, + Example: keysExample, + Args: cobra.RangeArgs(1, 2), + RunE: func(cmd *cobra.Command, args []string) error { + sdlgoredis.SetDbLogger(&buf) + keysArgs := newKeysArgs(args[0], "*") + if len(args) > 1 { + keysArgs.pattern = args[1] + } + keys, err := runListKeys(sdlCb, keysArgs) + if err != nil { + fmt.Fprintf(os.Stderr, "%s", buf.String()) + return err + } + printKeys(cmd, keys) + return nil + }, + } +} + +func runListKeys(sdlCb SyncStorageCreateCb, args keysArgs) ([]string, error) { + keys, err := sdlCb().ListKeys(args.ns, args.pattern) + if err != nil { + return nil, err + } + sort.Strings(keys) + return keys, err +} + +func printKeys(cmd *cobra.Command, keys []string) { + for _, k := range keys { + cmd.Println(k) + } +}