-
Notifications
You must be signed in to change notification settings - Fork 224
/
root.go
230 lines (201 loc) · 6.52 KB
/
root.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
// Package root implements the root command.
package root
import (
"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/cmd"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/agent"
"github.com/superfly/flyctl/internal/command/apps"
"github.com/superfly/flyctl/internal/command/auth"
"github.com/superfly/flyctl/internal/command/checks"
"github.com/superfly/flyctl/internal/command/config"
"github.com/superfly/flyctl/internal/command/create"
"github.com/superfly/flyctl/internal/command/curl"
"github.com/superfly/flyctl/internal/command/deploy"
"github.com/superfly/flyctl/internal/command/destroy"
"github.com/superfly/flyctl/internal/command/dig"
"github.com/superfly/flyctl/internal/command/docs"
"github.com/superfly/flyctl/internal/command/doctor"
"github.com/superfly/flyctl/internal/command/help"
"github.com/superfly/flyctl/internal/command/history"
"github.com/superfly/flyctl/internal/command/image"
"github.com/superfly/flyctl/internal/command/info"
"github.com/superfly/flyctl/internal/command/ips"
"github.com/superfly/flyctl/internal/command/jobs"
"github.com/superfly/flyctl/internal/command/launch"
"github.com/superfly/flyctl/internal/command/logs"
"github.com/superfly/flyctl/internal/command/machine"
"github.com/superfly/flyctl/internal/command/monitor"
"github.com/superfly/flyctl/internal/command/move"
"github.com/superfly/flyctl/internal/command/open"
"github.com/superfly/flyctl/internal/command/orgs"
"github.com/superfly/flyctl/internal/command/ping"
"github.com/superfly/flyctl/internal/command/platform"
"github.com/superfly/flyctl/internal/command/postgres"
"github.com/superfly/flyctl/internal/command/proxy"
"github.com/superfly/flyctl/internal/command/redis"
"github.com/superfly/flyctl/internal/command/releases"
"github.com/superfly/flyctl/internal/command/restart"
"github.com/superfly/flyctl/internal/command/resume"
"github.com/superfly/flyctl/internal/command/scale"
"github.com/superfly/flyctl/internal/command/secrets"
"github.com/superfly/flyctl/internal/command/services"
"github.com/superfly/flyctl/internal/command/ssh"
"github.com/superfly/flyctl/internal/command/status"
"github.com/superfly/flyctl/internal/command/suspend"
"github.com/superfly/flyctl/internal/command/turboku"
"github.com/superfly/flyctl/internal/command/version"
"github.com/superfly/flyctl/internal/command/vm"
"github.com/superfly/flyctl/internal/command/volumes"
)
// New initializes and returns a reference to a new root command.
func New() *cobra.Command {
/*
const (
long = `flyctl is a command line interface to the Fly.io platform.
It allows users to manage authentication, application launch,
deployment, network configuration, logging and more with just the
one command.
Launch an app with the launch command
Deploy an app with the deploy command
View a deployed web application with the open command
Check the status of an application with the status command
To read more, use the docs command to view Fly's help on the web.
`
short = "The Fly CLI"
usage = "flyctl"
)
root := command.New(usage, short, long, nil)
root.SilenceUsage = true
root.SilenceErrors = true
fs := root.PersistentFlags()
_ = fs.StringP(flag.AccessTokenName, "t", "", "Fly API Access Token")
_ = fs.BoolP(flag.JSONOutputName, "j", false, "JSON output")
_ = fs.BoolP(flag.VerboseName, "v", false, "Verbose output")
root.AddCommand(
version.New(),
apps.New(),
create.New(), // TODO: deprecate
destroy.New(), // TODO: deprecate
move.New(), // TODO: deprecate
suspend.New(), // TODO: deprecate
resume.New(), // TODO: deprecate
restart.New(), // TODO: deprecate
orgs.New(),
auth.New(),
builds.New(),
open.New(), // TODO: deprecate
curl.New(),
platform.New(),
docs.New(),
releases.New(),
deploy.New(),
history.New(),
status.New(),
logs.New(),
doctor.New(),
dig.New(),
volumes.New(),
agent.New(),
)
if os.Getenv("DEV") != "" {
root.AddCommand(services.New())
}
return root
*/
flyctl.InitConfig()
// what follows is a hack in order to achieve compatibility with what exists
// already. the commented out code above, is what should remain after the
// migration is complete.
// newCommands is the set of commands which work with the new way
newCommands := []*cobra.Command{
version.New(),
apps.New(),
create.New(), // TODO: deprecate
destroy.New(), // TODO: deprecate
move.New(), // TODO: deprecate
suspend.New(), // TODO: deprecate
resume.New(), // TODO: deprecate
restart.New(), // TODO: deprecate
orgs.New(),
auth.New(),
open.New(), // TODO: deprecate
curl.New(),
platform.New(),
docs.New(),
releases.New(),
deploy.New(),
history.New(),
status.New(),
logs.New(),
doctor.New(),
dig.New(),
volumes.New(),
agent.New(),
image.New(),
ping.New(),
proxy.New(),
machine.New(),
monitor.New(),
postgres.New(),
ips.New(),
secrets.New(),
ssh.New(),
ssh.NewSFTP(),
redis.New(),
vm.New(),
checks.New(),
launch.New(),
info.New(),
jobs.New(),
turboku.New(),
services.New(),
config.New(),
scale.New(),
}
// if os.Getenv("DEV") != "" {
// newCommands = append(newCommands, services.New())
// }
// newCommandNames is the set of the names of the above commands
newCommandNames := make(map[string]struct{}, len(newCommands))
for _, cmd := range newCommands {
newCommandNames[cmd.Name()] = struct{}{}
}
// instead of root being constructed like in the commented out snippet, we
// rebuild it the old way.
root := cmd.NewRootCmd(client.New())
// gather the slice of commands which must be replaced with their new
// iterations
var commandsToReplace []*cobra.Command
for _, cmd := range root.Commands() {
if _, exists := newCommandNames[cmd.Name()]; exists {
commandsToReplace = append(commandsToReplace, cmd)
}
}
// remove them
root.RemoveCommand(commandsToReplace...)
// make sure the remaining old commands run the preparers
// TODO: remove when migration is done
wrapRunE(root)
// and finally, add the new commands
root.AddCommand(newCommands...)
root.SetHelpCommand(help.New(root))
root.RunE = help.NewRootHelp().RunE
return root
}
func wrapRunE(cmd *cobra.Command) {
if cmd.HasAvailableSubCommands() {
for _, c := range cmd.Commands() {
wrapRunE(c)
}
}
if cmd.RunE == nil && cmd.Run == nil {
return
}
if cmd.RunE == nil {
panic(cmd.Name())
}
cmd.RunE = command.WrapRunE(cmd.RunE)
}