/
values.go
55 lines (47 loc) · 1.12 KB
/
values.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"log"
"os"
"strings"
"text/tabwriter"
)
// valuesCmd represents the values command
var valuesCmd = &cobra.Command{
Use: "values PATH",
Aliases: []string{"v"},
Short: "Show values for params in PATH",
Long: `Show values for all params in given path.`,
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 1:
getValues(args[0])
default:
log.Fatal("wrong number of arguments")
}
},
}
// get values for params, either exact match or path
func getValues(path string) {
// path must start with /, so help out user and add if missing
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
const format = "%v\t%v\n"
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintf(tw, format, "NAME", "VALUE")
// find any param that is an exact match
p := paramByName(path)
if p != nil {
fmt.Fprintf(tw, format, *p.Name, *p.Value)
}
// find params with this path prefix
for _, p := range paramsByPath(path) {
fmt.Fprintf(tw, format, *p.Name, *p.Value)
}
tw.Flush()
}
func init() {
rootCmd.AddCommand(valuesCmd)
}