SDL CLI 'get' reads keys data in the given namespace
[ric-plt/sdlgo.git] / internal / cli / get.go
1 /*
2    Copyright (c) 2021 AT&T Intellectual Property.
3    Copyright (c) 2018-2021 Nokia.
4
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
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
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.
16 */
17
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 package cli
24
25 import (
26         "fmt"
27         "os"
28         "sort"
29
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"
33 )
34
35 var getCmd = newGetCmd(func() ISyncStorage {
36         return sdlgo.NewSyncStorage()
37 })
38
39 func init() {
40         rootCmd.AddCommand(getCmd)
41 }
42
43 var (
44         getLong = `Display one or many resources.
45
46 Prints keys and keys data in the given namespace.`
47
48         getExample = `  # Get reads keys data in the given namespace.
49   sdlcli get sdlns key1
50
51   # Get reads multiple keys data in the given namespace.
52   sdlcli get sdlns key1 key2 key3
53
54   # List keys in the given namespace.
55   sdlcli get keys sdlns`
56 )
57
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",
62                 Long:    getLong,
63                 Example: getExample,
64                 RunE: func(cmd *cobra.Command, args []string) error {
65                         sdlgoredis.SetDbLogger(&buf)
66                         if len(args) < 2 {
67                                 return fmt.Errorf("accepts command or arguments, received %d", len(args))
68                         }
69                         data, err := runGet(sdlCb, args)
70                         if err != nil {
71                                 fmt.Fprintf(os.Stderr, "%s", buf.String())
72                                 return err
73                         }
74                         printData(cmd, data)
75                         return nil
76                 },
77         }
78 }
79
80 func runGet(sdlCb SyncStorageCreateCb, args []string) (map[string]interface{}, error) {
81         data, err := sdlCb().Get(args[0], args[1:])
82         if err != nil {
83                 return nil, err
84         }
85         return data, nil
86 }
87
88 func printData(cmd *cobra.Command, data map[string]interface{}) {
89         var str string
90         var sortedKeys []string
91         for key := range data {
92                 sortedKeys = append(sortedKeys, key)
93         }
94         sort.Strings(sortedKeys)
95         for _, k := range sortedKeys {
96                 value, ok := data[k]
97                 if ok && value != nil {
98                         str = fmt.Sprintf("%s:%s", k, value)
99                         cmd.Printf(str + "\n")
100                 }
101         }
102 }