-
Notifications
You must be signed in to change notification settings - Fork 235
/
root.go
156 lines (131 loc) · 3.34 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
package cmd
import (
"context"
"errors"
"fmt"
"os"
"github.com/hashicorp/go-multierror"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/internal/client"
)
// ErrAbort - Error generated when application aborts
var ErrAbort = errors.New("abort")
var flyctlClient *client.Client
var rootStrings = docstrings.Get("flyctl")
var rootCmd = &Command{
Command: &cobra.Command{
Use: rootStrings.Usage,
Short: rootStrings.Short,
Long: rootStrings.Long,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cmd.SilenceUsage = true
cmd.SilenceErrors = true
flyctlClient = client.NewClient()
},
},
}
// GetRootCommand - root for commands
func GetRootCommand() *cobra.Command {
return rootCmd.Command
}
// Execute - root command execution
func Execute() {
defer flyctl.BackgroundTaskWG.Wait()
err := rootCmd.Execute()
checkErr(err)
}
func init() {
rootCmd.PersistentFlags().StringP("access-token", "t", "", "Fly API Access Token")
err := viper.BindPFlag(flyctl.ConfigAPIToken, rootCmd.PersistentFlags().Lookup("access-token"))
checkErr(err)
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
err = viper.BindPFlag(flyctl.ConfigVerboseOutput, rootCmd.PersistentFlags().Lookup("verbose"))
checkErr(err)
rootCmd.PersistentFlags().BoolP("json", "j", false, "json output")
err = viper.BindPFlag(flyctl.ConfigJSONOutput, rootCmd.PersistentFlags().Lookup("json"))
checkErr(err)
rootCmd.PersistentFlags().String("builtinsfile", "", "Load builtins from named file")
err = viper.BindPFlag(flyctl.ConfigBuiltinsfile, rootCmd.PersistentFlags().Lookup("builtinsfile"))
checkErr(err)
err = rootCmd.PersistentFlags().MarkHidden("builtinsfile")
checkErr(err)
rootCmd.AddCommand(
newAppsCommand(),
newAuthCommand(),
newBuildsCommand(),
newCurlCommand(),
newCertificatesCommand(),
newConfigCommand(),
newDashboardCommand(),
newDeployCommand(),
newDestroyCommand(),
newDocsCommand(),
newHistoryCommand(),
newInfoCommand(),
newInitCommand(),
newIPAddressesCommand(),
newListCommand(),
newLogsCommand(),
newMonitorCommand(),
newMoveCommand(),
newOpenCommand(),
newPlatformCommand(),
newRegionsCommand(),
newReleasesCommand(),
newRestartCommand(),
newResumeCommand(),
newScaleCommand(),
newAutoscaleCommand(),
newSecretsCommand(),
newStatusCommand(),
newSuspendCommand(),
newVersionCommand(),
newDNSCommand(),
newDomainsCommand(),
newOrgsCommand(),
newBuiltinsCommand(),
newVolumesCommand(),
newWireGuardCommand(),
newSSHCommand(),
newChecksCommand(),
newPostgresCommand(),
newVMCommand(),
newLaunchCommand(),
)
initConfig()
}
func initConfig() {
flyctl.InitConfig()
flyctl.CheckForUpdate(false, false) // allow skipping, don't be silent
}
func checkErr(err error) {
if err == nil {
return
}
if !isCancelledError(err) {
fmt.Println(aurora.Red("Error"), err)
}
safeExit()
}
func isCancelledError(err error) bool {
if err == ErrAbort {
return true
}
if err == context.Canceled {
return true
}
if merr, ok := err.(*multierror.Error); ok {
if len(merr.Errors) == 1 && merr.Errors[0] == context.Canceled {
return true
}
}
return false
}
func safeExit() {
flyctl.BackgroundTaskWG.Wait()
os.Exit(1)
}