-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
275 lines (256 loc) · 9.15 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/cloud-barista/cb-ladybug/src/grpc-api/cbadm/app"
)
type ConfigOptions struct {
*app.Options
Ladybug_Server_addr string
Ladybug_Timeout string
Ladybug_Endpoint string
Ladybug_Service_name string
Ladybug_Sample_rate string
Spider_Server_addr string
Spider_Timeout string
Spider_Endpoint string
Spider_Service_name string
Spider_Sample_rate string
}
func (o *ConfigOptions) writeYaml(in interface{}) {
if b, err := yaml.Marshal(in); err != nil {
o.PrintlnError(err)
} else {
o.WriteBody(b)
}
}
// returns a cobra command
func NewCommandConfig(options *app.Options) *cobra.Command {
o := &ConfigOptions{
Options: options,
}
// root
cmds := &cobra.Command{
Use: "config",
Short: "Configuration command",
Long: "This is a configuration command",
Run: func(c *cobra.Command, args []string) {
c.Help()
},
}
// add-context
cmdC := &cobra.Command{
Use: "add-context (NAME | --name NAME) [options]",
Short: "Add a context",
DisableFlagsInUseLine: true,
Args: app.BindCommandArgs(&o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, func() error {
if len(o.Name) == 0 {
return fmt.Errorf("Name is required.")
}
if _, ok := app.Config.Contexts[o.Name]; ok {
return fmt.Errorf("The context '%s' is alreaday exist", o.Name)
} else {
var sConf *app.CliConfig = new(app.CliConfig)
sConf.ServerAddr = o.Spider_Server_addr
sConf.Timeout = o.Spider_Timeout
sConf.Interceptors.Opentracing.Jaeger.Endpoint = o.Spider_Endpoint
sConf.Interceptors.Opentracing.Jaeger.ServiceName = o.Spider_Service_name
sConf.Interceptors.Opentracing.Jaeger.SampleRate = o.Spider_Sample_rate
var gConf *app.CliConfig = new(app.CliConfig)
gConf.ServerAddr = o.Ladybug_Server_addr
gConf.Timeout = o.Ladybug_Timeout
gConf.Interceptors.Opentracing.Jaeger.Endpoint = o.Ladybug_Endpoint
gConf.Interceptors.Opentracing.Jaeger.ServiceName = o.Ladybug_Service_name
gConf.Interceptors.Opentracing.Jaeger.SampleRate = o.Ladybug_Sample_rate
app.Config.Contexts[o.Name] = &app.ConfigContext{
Name: o.Name,
Namespace: o.Namespace,
Ladybugcli: gConf,
Spidercli: sConf,
}
}
app.Config.WriteConfig()
o.writeYaml(app.Config)
return nil
}())
},
}
cmdC.Flags().StringVarP(&o.Ladybug_Server_addr, "ladybug_server_addr", "", "127.0.0.1:50254", "Server Addr URL")
cmdC.Flags().StringVarP(&o.Ladybug_Timeout, "ladybug_timeout", "", "1000s", "Timeout")
cmdC.Flags().StringVarP(&o.Ladybug_Endpoint, "ladybug_endpoint", "", "localhost:6834", "endpoint URL")
cmdC.Flags().StringVarP(&o.Ladybug_Service_name, "ladybug_service_name", "", "ladybug grpc client", "Service Name")
cmdC.Flags().StringVarP(&o.Ladybug_Sample_rate, "ladybug_sample_rate", "", "1", "sample rate")
cmdC.Flags().StringVarP(&o.Spider_Server_addr, "spider_server_addr", "", "127.0.0.1:2048", "Server Addr URL")
cmdC.Flags().StringVarP(&o.Spider_Timeout, "spider_timeout", "", "1000s", "Timeout")
cmdC.Flags().StringVarP(&o.Spider_Endpoint, "spider_endpoint", "", "localhost:6832", "endpoint URL")
cmdC.Flags().StringVarP(&o.Spider_Service_name, "spider_service_name", "", "spider grpc client", "Service Name")
cmdC.Flags().StringVarP(&o.Spider_Sample_rate, "spider_sample_rate", "", "1", "sample rate")
cmds.AddCommand(cmdC)
// view
cmds.AddCommand(&cobra.Command{
Use: "view",
Short: "Get contexts",
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, func() error {
o.writeYaml(app.Config)
return nil
}())
},
})
// get context
cmds.AddCommand(&cobra.Command{
Use: "get-context (NAME | --name NAME) [options]",
Short: "Get a context",
Args: app.BindCommandArgs(&o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, func() error {
if o.Name == "" {
for k := range app.Config.Contexts {
o.Println(k)
}
} else {
if app.Config.Contexts[o.Name] != nil {
o.writeYaml(app.Config.Contexts[o.Name])
}
}
return nil
}())
},
})
// set context
cmdS := &cobra.Command{
Use: "set-context (NAME | --name NAME) [options]",
Short: "Set a context",
Args: app.BindCommandArgs(&o.Name),
DisableFlagsInUseLine: true,
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, func() error {
if o.Name == "" {
c.Help()
} else if app.Config.Contexts[o.Name] != nil {
app.Config.Contexts[o.Name].Name = o.Name
if o.Ladybug_Server_addr != "" {
app.Config.Contexts[o.Name].Ladybugcli.ServerAddr = o.Ladybug_Server_addr
}
if o.Ladybug_Timeout != "" {
app.Config.Contexts[o.Name].Ladybugcli.Timeout = o.Ladybug_Timeout
}
if o.Ladybug_Endpoint != "" {
app.Config.Contexts[o.Name].Ladybugcli.Interceptors.Opentracing.Jaeger.Endpoint = o.Ladybug_Endpoint
}
if o.Ladybug_Service_name != "" {
app.Config.Contexts[o.Name].Ladybugcli.Interceptors.Opentracing.Jaeger.ServiceName = o.Ladybug_Service_name
}
if o.Ladybug_Sample_rate != "" {
app.Config.Contexts[o.Name].Ladybugcli.Interceptors.Opentracing.Jaeger.SampleRate = o.Ladybug_Sample_rate
}
if o.Spider_Server_addr != "" {
app.Config.Contexts[o.Name].Spidercli.ServerAddr = o.Spider_Server_addr
}
if o.Spider_Timeout != "" {
app.Config.Contexts[o.Name].Spidercli.Timeout = o.Spider_Timeout
}
if o.Spider_Endpoint != "" {
app.Config.Contexts[o.Name].Spidercli.Interceptors.Opentracing.Jaeger.Endpoint = o.Spider_Endpoint
}
if o.Spider_Service_name != "" {
app.Config.Contexts[o.Name].Spidercli.Interceptors.Opentracing.Jaeger.ServiceName = o.Spider_Service_name
}
if o.Spider_Sample_rate != "" {
app.Config.Contexts[o.Name].Spidercli.Interceptors.Opentracing.Jaeger.SampleRate = o.Spider_Sample_rate
}
o.writeYaml(app.Config.Contexts[o.Name])
} else {
o.Println("Not found a context (name=%s)", o.Name)
}
return nil
}())
},
}
cmdS.Flags().StringVarP(&o.Ladybug_Server_addr, "ladybug_server_addr", "", "127.0.0.1:50254", "Server Addr URL")
cmdS.Flags().StringVarP(&o.Ladybug_Timeout, "ladybug_timeout", "", "1000s", "Timeout")
cmdS.Flags().StringVarP(&o.Ladybug_Endpoint, "ladybug_endpoint", "", "localhost:6834", "endpoint URL")
cmdS.Flags().StringVarP(&o.Ladybug_Service_name, "ladybug_service_name", "", "ladybug grpc client", "Service Name")
cmdS.Flags().StringVarP(&o.Ladybug_Sample_rate, "ladybug_sample_rate", "", "1", "sample rate")
cmdS.Flags().StringVarP(&o.Spider_Server_addr, "spider_server_addr", "", "127.0.0.1:2048", "Server Addr URL")
cmdS.Flags().StringVarP(&o.Spider_Timeout, "spider_timeout", "", "1000s", "Timeout")
cmdS.Flags().StringVarP(&o.Spider_Endpoint, "spider_endpoint", "", "localhost:6832", "endpoint URL")
cmdS.Flags().StringVarP(&o.Spider_Service_name, "spider_service_name", "", "spider grpc client", "Service Name")
cmdS.Flags().StringVarP(&o.Spider_Sample_rate, "spider_sample_rate", "", "1", "sample rate")
cmds.AddCommand(cmdS)
// current-context (get/set)
cmds.AddCommand(&cobra.Command{
Use: "current-context (NAME | --name NAME) [options]",
Short: "Get/Set a current context",
DisableFlagsInUseLine: true,
Args: app.BindCommandArgs(&o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, func() error {
if len(o.Name) > 0 {
_, ok := app.Config.Contexts[o.Name]
if ok {
app.Config.CurrentContext = o.Name
app.Config.WriteConfig()
} else {
o.Println("context '%s' is not exist\n", o.Name)
}
}
o.writeYaml(app.Config.GetCurrentContext().Name)
return nil
}())
},
})
// delete-context
cmds.AddCommand(&cobra.Command{
Use: "delete-context (NAME | --name NAME) [options]",
Short: "Delete a context",
Args: app.BindCommandArgs(&o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, func() error {
if o.Name == "" {
return fmt.Errorf("Name Required.")
}
conf := app.Config
if len(conf.Contexts) > 1 {
delete(conf.Contexts, o.Name)
if o.Name == conf.CurrentContext {
conf.CurrentContext = func() string {
if len(conf.Contexts) > 0 {
for k := range conf.Contexts {
return k
}
}
return ""
}()
}
conf.WriteConfig()
}
o.writeYaml(conf)
return nil
}())
},
})
// set-namespace
cmds.AddCommand(&cobra.Command{
Use: "set-namespace (NAME | --name NAME) [options]",
Short: "Set a namespace to context",
Args: app.BindCommandArgs(&o.Name),
DisableFlagsInUseLine: true,
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, func() error {
if len(app.Config.GetCurrentContext().Name) == 0 {
c.Help()
} else {
app.Config.GetCurrentContext().Namespace = args[0]
app.Config.WriteConfig()
o.writeYaml(app.Config.GetCurrentContext())
}
return nil
}())
},
})
return cmds
}