forked from micro/micro
/
cli.go
176 lines (161 loc) · 4.35 KB
/
cli.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
// Package cli is a command line interface
package cli
import (
"fmt"
"os"
osexec "os/exec"
"strings"
"github.com/chzyer/readline"
"github.com/tickoalcantara12/micro/v3/client/cli/util"
"github.com/tickoalcantara12/micro/v3/cmd"
"github.com/urfave/cli/v2"
_ "github.com/tickoalcantara12/micro/v3/client/cli/auth"
_ "github.com/tickoalcantara12/micro/v3/client/cli/config"
_ "github.com/tickoalcantara12/micro/v3/client/cli/debug"
_ "github.com/tickoalcantara12/micro/v3/client/cli/gen"
_ "github.com/tickoalcantara12/micro/v3/client/cli/init"
_ "github.com/tickoalcantara12/micro/v3/client/cli/network"
_ "github.com/tickoalcantara12/micro/v3/client/cli/new"
_ "github.com/tickoalcantara12/micro/v3/client/cli/run"
_ "github.com/tickoalcantara12/micro/v3/client/cli/shutdown"
_ "github.com/tickoalcantara12/micro/v3/client/cli/store"
_ "github.com/tickoalcantara12/micro/v3/client/cli/user"
)
var (
prompt = "micro> "
// TODO: only run fixed set of commands for security purposes
commands = map[string]*command{}
)
type command struct {
name string
usage string
exec util.Exec
}
func Run(c *cli.Context) error {
// take the first arg as the binary
binary := os.Args[0]
r, err := readline.New(prompt)
if err != nil {
return err
}
defer r.Close()
for {
args, err := r.Readline()
if err != nil {
fmt.Fprint(os.Stdout, err)
return err
}
args = strings.TrimSpace(args)
// skip no args
if len(args) == 0 {
continue
}
parts := strings.Split(args, " ")
if len(parts) == 0 {
continue
}
cmd := osexec.Command(binary, parts...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Println(string(err.(*osexec.ExitError).Stderr))
}
}
return nil
}
func init() {
cmd.Register(
&cli.Command{
Name: "cli",
Usage: "Run the interactive CLI",
Action: Run,
},
&cli.Command{
Name: "call",
Usage: `Call a service e.g micro call greeter Say.Hello '{"name": "John"}'`,
Action: util.Print(CallService),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Usage: "Set the address of the service instance to call",
EnvVars: []string{"MICRO_ADDRESS"},
},
&cli.StringFlag{
Name: "output, o",
Usage: "Set the output format; json (default), raw",
EnvVars: []string{"MICRO_OUTPUT"},
},
&cli.StringSliceFlag{
Name: "metadata",
Usage: "A list of key-value pairs to be forwarded as metadata",
EnvVars: []string{"MICRO_METADATA"},
},
&cli.StringFlag{
Name: "request_timeout",
Usage: "timeout duration",
},
},
},
&cli.Command{
Name: "get",
Usage: `Get resources from micro`,
Subcommands: []*cli.Command{
{
Name: "service",
Usage: "Get a specific service from the registry",
Action: util.Print(GetService),
},
},
},
&cli.Command{
Name: "stream",
Usage: `Create a service stream e.g. micro stream foo Bar.Baz '{"key": "value"}'`,
Action: util.Print(streamService),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output, o",
Usage: "Set the output format; json (default), raw",
EnvVars: []string{"MICRO_OUTPUT"},
},
&cli.StringSliceFlag{
Name: "metadata",
Usage: "A list of key-value pairs to be forwarded as metadata",
EnvVars: []string{"MICRO_METADATA"},
},
},
},
&cli.Command{
Name: "env",
Usage: "Get/set micro cli environment",
Action: util.Print(listEnvs),
Subcommands: []*cli.Command{
{
Name: "get",
Usage: "Get the currently selected environment",
Action: util.Print(getEnv),
},
{
Name: "set",
Usage: "Set the environment to use for subsequent commands e.g. micro env set dev",
Action: util.Print(setEnv),
},
{
Name: "add",
Usage: "Add a new environment e.g. micro env add foo 127.0.0.1:8081",
Action: util.Print(addEnv),
},
{
Name: "del",
Usage: "Delete an environment from your list e.g. micro env del foo",
Action: util.Print(delEnv),
},
},
},
&cli.Command{
Name: "services",
Usage: "List services in the registry",
Action: util.Print(ListServices),
},
)
}