-
Notifications
You must be signed in to change notification settings - Fork 219
/
root.go
139 lines (114 loc) · 2.87 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
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")
viper.BindPFlag(flyctl.ConfigAPIToken, rootCmd.PersistentFlags().Lookup("access-token"))
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
viper.BindPFlag(flyctl.ConfigVerboseOutput, rootCmd.PersistentFlags().Lookup("verbose"))
rootCmd.PersistentFlags().BoolP("json", "j", false, "json output")
viper.BindPFlag(flyctl.ConfigJSONOutput, rootCmd.PersistentFlags().Lookup("json"))
rootCmd.PersistentFlags().Bool("gqlerrorlogging", false, "Log GraphQL errors directly to stdout")
rootCmd.PersistentFlags().MarkHidden("gqlerrorlogging")
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(),
newSecretsCommand(),
newStatusCommand(),
newSuspendCommand(),
newVersionCommand(),
newOrgsCommand(),
)
initConfig()
}
func initConfig() {
flyctl.InitConfig()
flyctl.CheckForUpdate()
}
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)
}