-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
139 lines (120 loc) · 3.4 KB
/
config.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cmd
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/criteo/command-launcher/internal/config"
"github.com/criteo/command-launcher/internal/context"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type ConfigFlags struct {
Json bool
}
var (
configFlags = ConfigFlags{}
)
func AddConfigCmd(rootCmd *cobra.Command, appCtx context.LauncherContext) {
configCmd := &cobra.Command{
Use: "config",
Short: "Manage configurations",
Long: fmt.Sprintf(`Manage the command launcher configurations
Example:
get configuration
%s config [key]
set configuration
%s config [key] [value]
`, appCtx.AppName(), appCtx.AppName()),
Run: func(cmd *cobra.Command, args []string) {
// list all configs
if len(args) == 0 {
settings := viper.AllSettings()
printSettings(settings, configFlags.Json)
}
// get configuration with key
if len(args) == 1 {
if viper.Get(args[0]) == nil {
return
}
if configFlags.Json {
printSettings(map[string]interface{}{
args[0]: viper.Get(args[0]),
}, true)
} else {
fmt.Println(viper.Get(args[0]))
}
}
// set configuration with key
if len(args) == 2 {
if err := config.SetSettingValue(args[0], args[1]); err != nil {
fmt.Println(err)
return
}
if err := viper.WriteConfig(); err != nil {
log.Error("cannot write the default configuration: ", err)
return
}
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
lowerKeys := []string{}
for _, k := range config.SettingKeys {
lowerKeys = append(lowerKeys, strings.ToLower(k))
}
return lowerKeys, cobra.ShellCompDirectiveNoFileComp
},
}
configCmd.Flags().BoolVarP(&configFlags.Json, "json", "", false, "output in JSON format")
rootCmd.AddCommand(configCmd)
}
// get printable settings in alphabet order
func printableSettingsInOrder(settings map[string]interface{}) []string {
sorted := []string{}
keys := []string{}
for k := range settings {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if k == strings.ToLower(config.EXTRA_REMOTES_KEY) {
remotes, _ := config.Remotes()
for _, remote := range remotes {
key := fmt.Sprintf("extra_remotes.%s.remote_base_url", remote.Name)
sorted = append(sorted, fmt.Sprintf("%-40v: %v", key, remote.RemoteBaseUrl))
key = fmt.Sprintf("extra_remotes.%s.repository_dir", remote.Name)
sorted = append(sorted, fmt.Sprintf("%-40v: %v", key, remote.RepositoryDir))
key = fmt.Sprintf("extra_remotes.%s.sync_policy", remote.Name)
sorted = append(sorted, fmt.Sprintf("%-40v: %v", key, remote.SyncPolicy))
}
} else {
sorted = append(sorted, fmt.Sprintf("%-40v: %v", k, settings[k]))
}
}
return sorted
}
func printSettings(settings map[string]interface{}, jsonFormat bool) {
if jsonFormat {
printInJson(settings)
} else {
printInPlainText(settings)
}
}
func printInJson(settings map[string]interface{}) {
val, err := json.MarshalIndent(settings, "", " ")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(val))
}
}
func printInPlainText(settings map[string]interface{}) {
printableSettings := printableSettingsInOrder(settings)
for _, line := range printableSettings {
fmt.Println(line)
}
}