Merge "RIC:1060: Change in PTL"
[ric-plt/sdlgo.git] / internal / cli / remove.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         "bytes"
27         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
28         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
29         "github.com/spf13/cobra"
30         "os"
31 )
32
33 const removeLongHelp = "Remove key(s) under given namespace from SDL DB. A key to remove\n" +
34         "can be an exact key name or a key search pattern. Multiple key names\n" +
35         "or search patterns can be given in a single command, each of which\n" +
36         "is separated from the one next to it by a space. If no key or\n" +
37         "search pattern is given, command removes all the keys under given\n" +
38         "namespace."
39
40 const removeExample = "  # Remove all keys in the given namespace.\n" +
41         "  sdlcli remove sdlns\n" +
42         "  # Remove keys 'key1' and 'key2' in the given namespace.\n" +
43         "  sdlcli remove sdlns key1 key2\n" +
44         "  # Remove keys in the given namespace matching given key search pattern.\n" +
45         "  sdlcli remove sdlns 'he*'\n\n" +
46
47         "  Supported search glob-style patterns:\n" +
48         "    h?llo matches hello, hallo and hxllo\n" +
49         "    h*llo matches hllo and heeeello\n" +
50         "    h[ae]llo matches hello and hallo, but not hillo\n" +
51         "    h[^e]llo matches hallo, hbllo, ... but not hello\n" +
52         "    h[a-b]llo matches hallo and hbllo\n\n" +
53         "  The \\ escapes character in key search pattern and those will be\n" +
54         "  treated as a normal character:\n" +
55         "    h\\[?llo\\* matches h[ello* and h[allo"
56
57 func init() {
58         rootCmd.AddCommand(newRemoveCmd(func() ISyncStorage {
59                 return sdlgo.NewSyncStorage()
60         }))
61 }
62
63 func newRemoveCmd(sdlCreateCb SyncStorageCreateCb) *cobra.Command {
64         cmd := &cobra.Command{
65                 Use:     "remove <namespace> [<key|pattern>... <keyN|patternN>]",
66                 Short:   "Remove key(s) under given namespace from SDL DB",
67                 Long:    removeLongHelp,
68                 Example: removeExample,
69                 Args:    cobra.MinimumNArgs(1),
70                 RunE: func(cmd *cobra.Command, args []string) error {
71                         var buf bytes.Buffer
72                         sdlgoredis.SetDbLogger(&buf)
73                         err := runRemove(sdlCreateCb, args)
74                         if err != nil {
75                                 cmd.PrintErrf("%s\n", buf.String())
76                         }
77                         return err
78                 },
79         }
80         cmd.SetOut(os.Stdout)
81         return cmd
82 }
83
84 func runRemove(sdlCreateCb SyncStorageCreateCb, args []string) error {
85         sdl := sdlCreateCb()
86         ns := args[0]
87         keyPatterns := []string{"*"}
88         if len(args) > 1 {
89                 keyPatterns = args[1:]
90         }
91         for _, keyPattern := range keyPatterns {
92                 keys, err := sdl.ListKeys(ns, keyPattern)
93                 if err != nil {
94                         return err
95                 }
96                 if err := sdl.Remove(ns, keys); err != nil {
97                         return err
98                 }
99         }
100         return nil
101 }