Merge "RIC:1060: Change in PTL"
[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 namespaces, keys or keys data in the given namespace.`
47
48         getExample = `  # List all the namespaces in database.
49   sdlcli get namespaces
50
51   # List keys in the given namespace.
52   sdlcli get keys sdlns
53
54   # Reads key data in the given namespace.
55   sdlcli get sdlns key1
56
57   # Read multiple keys data in the given namespace.
58   sdlcli get sdlns key1 key2 key3`
59 )
60
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",
65                 Long:    getLong,
66                 Example: getExample,
67                 RunE: func(cmd *cobra.Command, args []string) error {
68                         sdlgoredis.SetDbLogger(&buf)
69                         if len(args) < 2 {
70                                 return fmt.Errorf("accepts command or arguments, received %d", len(args))
71                         }
72                         data, err := runGet(sdlCb, args)
73                         if err != nil {
74                                 fmt.Fprintf(os.Stderr, "%s", buf.String())
75                                 return err
76                         }
77                         printData(cmd, data)
78                         return nil
79                 },
80         }
81         cmd.SetOut(os.Stdout)
82         return cmd
83 }
84
85 func runGet(sdlCb SyncStorageCreateCb, args []string) (map[string]interface{}, error) {
86         data, err := sdlCb().Get(args[0], args[1:])
87         if err != nil {
88                 return nil, err
89         }
90         return data, nil
91 }
92
93 func printData(cmd *cobra.Command, data map[string]interface{}) {
94         var str string
95         var sortedKeys []string
96         for key := range data {
97                 sortedKeys = append(sortedKeys, key)
98         }
99         sort.Strings(sortedKeys)
100         for _, k := range sortedKeys {
101                 value, ok := data[k]
102                 if ok && value != nil {
103                         str = fmt.Sprintf("%s:%s", k, value)
104                         cmd.Printf(str + "\n")
105                 }
106         }
107 }