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 keys and keys data in the given namespace.`
48 getExample = ` # Get reads keys data in the given namespace.
51 # Get reads multiple keys data in the given namespace.
52 sdlcli get sdlns key1 key2 key3
54 # List keys in the given namespace.
55 sdlcli get keys sdlns`
58 func newGetCmd(sdlCb SyncStorageCreateCb) *cobra.Command {
59 return &cobra.Command{
60 Use: "get <namespace> <key> [<key2> <key3>... <keyN>]",
61 Short: "Display one or many resources",
64 RunE: func(cmd *cobra.Command, args []string) error {
65 sdlgoredis.SetDbLogger(&buf)
67 return fmt.Errorf("accepts command or arguments, received %d", len(args))
69 data, err := runGet(sdlCb, args)
71 fmt.Fprintf(os.Stderr, "%s", buf.String())
80 func runGet(sdlCb SyncStorageCreateCb, args []string) (map[string]interface{}, error) {
81 data, err := sdlCb().Get(args[0], args[1:])
88 func printData(cmd *cobra.Command, data map[string]interface{}) {
90 var sortedKeys []string
91 for key := range data {
92 sortedKeys = append(sortedKeys, key)
94 sort.Strings(sortedKeys)
95 for _, k := range sortedKeys {
97 if ok && value != nil {
98 str = fmt.Sprintf("%s:%s", k, value)
99 cmd.Printf(str + "\n")