/
kvstore_get.go
71 lines (61 loc) · 1.46 KB
/
kvstore_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018-2019 Authors of Cilium
package cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/cilium/cilium/pkg/command"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/spf13/cobra"
)
var kvstoreGetCmd = &cobra.Command{
Use: "get [options] <key>",
Short: "Retrieve a key",
Example: "cilium kvstore get --recursive foo",
Run: func(cmd *cobra.Command, args []string) {
key := ""
if len(args) > 0 {
key = args[0]
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
setupKvstore(ctx)
if recursive {
pairs, err := kvstore.Client().ListPrefix(ctx, key)
if err != nil {
Fatalf("Unable to list keys: %s", err)
}
if command.OutputJSON() {
if err := command.PrintOutput(pairs); err != nil {
os.Exit(1)
}
return
}
for k, v := range pairs {
fmt.Printf("%s => %s\n", k, v.Data)
}
} else {
val, err := kvstore.Client().Get(ctx, key)
if err != nil {
Fatalf("Unable to retrieve key %s: %s", key, err)
}
if val == nil {
Fatalf("key %s is not found", key)
}
if command.OutputJSON() {
if err := command.PrintOutput(string(val)); err != nil {
os.Exit(1)
}
return
}
fmt.Printf("%s => %s\n", key, val)
}
},
}
func init() {
kvstoreCmd.AddCommand(kvstoreGetCmd)
kvstoreGetCmd.Flags().BoolVar(&recursive, "recursive", false, "Recursive lookup")
command.AddJSONOutput(kvstoreGetCmd)
}