forked from weaveworks/footloose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_get.go
54 lines (48 loc) · 1.08 KB
/
config_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
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"github.com/spf13/cobra"
"github.com/kingdonb/footloose/pkg/config"
)
var getConfigCmd = &cobra.Command{
Use: "get",
Short: "Get config file information",
RunE: getConfig,
}
var getOptions struct {
config string
}
func init() {
getConfigCmd.Flags().StringVarP(&getOptions.config, "config", "c", Footloose, "Cluster configuration file")
configCmd.AddCommand(getConfigCmd)
}
func getConfig(cmd *cobra.Command, args []string) error {
c, err := config.NewConfigFromFile(configFile(getOptions.config))
if err != nil {
return err
}
var detail interface{}
if len(args) > 0 {
detail, err = config.GetValueFromConfig(args[0], c)
if err != nil {
log.Println(err)
return fmt.Errorf("Failed to get config detail")
}
} else {
detail = c
}
if reflect.ValueOf(detail).Kind() != reflect.String {
res, err := json.MarshalIndent(detail, "", " ")
if err != nil {
log.Println(err)
return fmt.Errorf("Cannot convert result to json")
}
fmt.Printf("%s", res)
} else {
fmt.Printf("%s", detail)
}
return nil
}