35fef032a1a41f3abe908d6a4a58e91bb70cd1e5
[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.\n" +
34         "Multiple keys to remove can be given in a single command,\n" +
35         "each key is separated from the one next to it by a space."
36
37 func init() {
38         rootCmd.AddCommand(newRemoveCmd(func() ISyncStorage {
39                 return sdlgo.NewSyncStorage()
40         }))
41 }
42
43 func newRemoveCmd(sdlCreateCb SyncStorageCreateCb) *cobra.Command {
44         cmd := &cobra.Command{
45                 Use:   "remove <namespace> <key> [<key2>... <keyN>]",
46                 Short: "Remove key(s) under given namespace from SDL DB",
47                 Long:  removeLongHelp,
48                 Args:  cobra.MinimumNArgs(2),
49                 RunE: func(cmd *cobra.Command, args []string) error {
50                         var buf bytes.Buffer
51                         sdlgoredis.SetDbLogger(&buf)
52                         err := runRemove(sdlCreateCb, args)
53                         if err != nil {
54                                 cmd.PrintErrf("%s\n", buf.String())
55                         }
56                         return err
57                 },
58         }
59         cmd.SetOut(os.Stdout)
60         return cmd
61 }
62
63 func runRemove(sdlCreateCb SyncStorageCreateCb, args []string) error {
64         sdl := sdlCreateCb()
65         if err := sdl.Remove(args[0], args[1:]); err != nil {
66                 return err
67         }
68         return nil
69 }