-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
247 lines (203 loc) · 5.24 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
package config
import (
"context"
"fmt"
"os"
"time"
"github.com/micro/cli/v2"
"github.com/dreamlu/go-micro/v2"
"github.com/dreamlu/go-micro/v2/config/cmd"
proto "github.com/dreamlu/go-micro/v2/config/source/service/proto"
log "github.com/dreamlu/go-micro/v2/logger"
"github.com/dreamlu/micro/v2/config/handler"
)
var (
// Service name
Name = "go.micro.config"
// Default database store
Database = "store"
// Default key
Namespace = "global"
)
func setPlatform(ctx *cli.Context) {
if ctx.Bool("platform") {
os.Setenv("MICRO_PROXY", "service")
os.Setenv("MICRO_PROXY_ADDRESS", "proxy.micro.mu:443")
}
}
func Run(c *cli.Context, srvOpts ...micro.Option) {
if len(c.String("server_name")) > 0 {
Name = c.String("server_name")
}
if len(c.String("watch_topic")) > 0 {
handler.WatchTopic = c.String("watch_topic")
}
srvOpts = append(srvOpts, micro.Name(Name))
service := micro.NewService(srvOpts...)
h := &handler.Config{
Store: *cmd.DefaultCmd.Options().Store,
}
proto.RegisterConfigHandler(service.Server(), h)
micro.RegisterSubscriber(handler.WatchTopic, service.Server(), handler.Watcher)
if err := service.Run(); err != nil {
log.Fatalf("config Run the service error: ", err)
}
}
func setConfig(ctx *cli.Context) error {
setPlatform(ctx)
pb := proto.NewConfigService("go.micro.config", *cmd.DefaultCmd.Options().Client)
args := ctx.Args()
if args.Len() == 0 {
log.Fatal("Required usage; micro config set key val")
}
// key val
key := args.Get(0)
val := args.Get(1)
// TODO: allow the specifying of a config.Key. This will be service name
// The actuall key-val set is a path e.g micro/accounts/key
_, err := pb.Update(context.TODO(), &proto.UpdateRequest{
Change: &proto.Change{
// global key
Namespace: Namespace,
// actual key for the value
Path: key,
// The value
ChangeSet: &proto.ChangeSet{
Data: string(val),
Format: "json",
Source: "cli",
Timestamp: time.Now().Unix(),
},
},
})
if err != nil {
log.Fatalf("Error setting key-val: %v", err)
}
return nil
}
func getConfig(ctx *cli.Context) error {
setPlatform(ctx)
pb := proto.NewConfigService("go.micro.config", *cmd.DefaultCmd.Options().Client)
args := ctx.Args()
if args.Len() == 0 {
log.Fatal("Required usage; micro config get key")
}
// key val
key := args.Get(0)
if len(key) == 0 {
log.Fatal("key cannot be blank")
}
// TODO: allow the specifying of a config.Key. This will be service name
// The actuall key-val set is a path e.g micro/accounts/key
rsp, err := pb.Read(context.TODO(), &proto.ReadRequest{
// The global key,
Namespace: Namespace,
// The actual key for the val
Path: key,
})
if err != nil {
log.Fatalf("Error reading key-val: %v", err)
}
if rsp.Change == nil || rsp.Change.ChangeSet == nil {
return nil
}
// don't do it
if v := rsp.Change.ChangeSet.Data; len(v) == 0 || string(v) == "null" {
return nil
}
fmt.Println("Value:", string(rsp.Change.ChangeSet.Data))
return nil
}
func delConfig(ctx *cli.Context) error {
setPlatform(ctx)
pb := proto.NewConfigService("go.micro.config", *cmd.DefaultCmd.Options().Client)
args := ctx.Args()
if args.Len() == 0 {
log.Fatal("Required usage; micro config get key")
}
// key val
key := args.Get(0)
if len(key) == 0 {
log.Fatal("key cannot be blank")
}
// TODO: allow the specifying of a config.Key. This will be service name
// The actuall key-val set is a path e.g micro/accounts/key
_, err := pb.Delete(context.TODO(), &proto.DeleteRequest{
Change: &proto.Change{
// The global key,
Namespace: Namespace,
// The actual key for the val
Path: key,
},
})
if err != nil {
log.Fatalf("Error deleting key-val: %v", err)
}
return nil
}
func Commands(options ...micro.Option) []*cli.Command {
command := &cli.Command{
Name: "config",
Usage: "Run the config server",
Subcommands: []*cli.Command{
{
Name: "set",
Usage: "Set a key-val; micro config set key val",
Action: setConfig,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "platform",
Usage: "Call through to the platform",
},
},
},
{
Name: "get",
Usage: "Get a value; micro config get key",
Action: getConfig,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "platform",
Usage: "Call through to the platform",
},
},
},
{
Name: "del",
Usage: "Delete a value; micro config del key",
Action: delConfig,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "platform",
Usage: "Call through to the platform",
},
},
},
},
Action: func(ctx *cli.Context) error {
Run(ctx, options...)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "namespace",
EnvVars: []string{"MICRO_CONFIG_NAMESPACE"},
Usage: "Set the namespace used by the Config Service e.g. go.micro.srv.config",
},
&cli.StringFlag{
Name: "watch_topic",
EnvVars: []string{"MICRO_CONFIG_WATCH_TOPIC"},
Usage: "watch the change event.",
},
},
}
for _, p := range Plugins() {
if cmds := p.Commands(); len(cmds) > 0 {
command.Subcommands = append(command.Subcommands, cmds...)
}
if flags := p.Flags(); len(flags) > 0 {
command.Flags = append(command.Flags, flags...)
}
}
return []*cli.Command{command}
}