2 Copyright (c) 2021 AT&T Intellectual Property.
3 Copyright (c) 2018-2021 Nokia.
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
30 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
31 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
32 "github.com/spf13/cobra"
35 var getCmd = newGetCmd(func() ISyncStorage {
36 return sdlgo.NewSyncStorage()
40 rootCmd.AddCommand(getCmd)
44 getLong = `Display one or many resources.
46 Prints namespaces, keys or keys data in the given namespace.`
48 getExample = ` # List all the namespaces in database.
51 # List keys in the given namespace.
54 # Reads key data in the given namespace.
57 # Read multiple keys data in the given namespace.
58 sdlcli get sdlns key1 key2 key3`
61 func newGetCmd(sdlCb SyncStorageCreateCb) *cobra.Command {
62 cmd := &cobra.Command{
63 Use: "get <namespace> <key> [<key2> <key3>... <keyN>]",
64 Short: "Display one or many resources",
67 RunE: func(cmd *cobra.Command, args []string) error {
68 sdlgoredis.SetDbLogger(&buf)
70 return fmt.Errorf("accepts command or arguments, received %d", len(args))
72 data, err := runGet(sdlCb, args)
74 fmt.Fprintf(os.Stderr, "%s", buf.String())
85 func runGet(sdlCb SyncStorageCreateCb, args []string) (map[string]interface{}, error) {
86 data, err := sdlCb().Get(args[0], args[1:])
93 func printData(cmd *cobra.Command, data map[string]interface{}) {
95 var sortedKeys []string
96 for key := range data {
97 sortedKeys = append(sortedKeys, key)
99 sort.Strings(sortedKeys)
100 for _, k := range sortedKeys {
102 if ok && value != nil {
103 str = fmt.Sprintf("%s:%s", k, value)
104 cmd.Printf(str + "\n")