Merge "RIC:1060: Change in PTL"
[ric-plt/sdlgo.git] / internal / cli / keys.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 func init() {
36         getCmd.AddCommand(newKeysCmd(func() ISyncStorage {
37                 return sdlgo.NewSyncStorage()
38         }))
39 }
40
41 var (
42         keysLong = `List keys in the given namespace matching key search pattern.`
43
44         keysExample = `  # List all keys in the given namespace.
45   sdlcli get keys sdlns
46   # List keys in the given namespace matching given key search pattern.
47   sdlcli get keys sdlns 'he*'
48
49   Supported search glob-style patterns:
50     h?llo matches hello, hallo and hxllo
51     h*llo matches hllo and heeeello
52     h[ae]llo matches hello and hallo, but not hillo
53     h[^e]llo matches hallo, hbllo, ... but not hello
54     h[a-b]llo matches hallo and hbllo
55
56   The \ escapes character in key search pattern and those will be treated as a normal
57   character:
58     h\[?llo\* matches h[ello* and h[allo*`
59 )
60
61 func newKeysCmd(sdlCb SyncStorageCreateCb) *cobra.Command {
62         cmd := &cobra.Command{
63                 Use:     "keys <namespace> [pattern|default '*']",
64                 Short:   "List keys in the given namespace matching key search pattern",
65                 Long:    keysLong,
66                 Example: keysExample,
67                 Args:    cobra.RangeArgs(1, 2),
68                 RunE: func(cmd *cobra.Command, args []string) error {
69                         sdlgoredis.SetDbLogger(&buf)
70                         keysArgs := NewKeysArgs(args[0], "*")
71                         if len(args) > 1 {
72                                 keysArgs.pattern = args[1]
73                         }
74                         if err := keysArgs.Validate(); err != nil {
75                                 return err
76                         }
77                         keys, err := runListKeys(sdlCb, keysArgs)
78                         if err != nil {
79                                 fmt.Fprintf(os.Stderr, "%s", buf.String())
80                                 return err
81                         }
82                         printKeys(cmd, keys)
83                         return nil
84                 },
85         }
86         cmd.SetOut(os.Stdout)
87         return cmd
88 }
89
90 func runListKeys(sdlCb SyncStorageCreateCb, args keysArgs) ([]string, error) {
91         keys, err := sdlCb().ListKeys(args.ns, args.pattern)
92         if err != nil {
93                 return nil, err
94         }
95         sort.Strings(keys)
96         return keys, err
97 }
98
99 func printKeys(cmd *cobra.Command, keys []string) {
100         for _, k := range keys {
101                 cmd.Println(k)
102         }
103 }