-
Notifications
You must be signed in to change notification settings - Fork 218
/
root.go
120 lines (98 loc) · 2.33 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
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/flyctl"
"github.com/superfly/flyctl/internal/client"
)
// ErrAbort - Error generated when application aborts
var ErrAbort = errors.New("abort")
var flyctlClient *client.Client
var rootCmd = &Command{
Command: &cobra.Command{
Use: "flyctl",
Short: "The Fly CLI",
Long: `flyctl is a command line interface for the Fly.io platform`,
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.AddCommand(
newAuthCommand(),
newAppStatusCommand(),
newAppListCommand(),
newAppReleasesListCommand(),
newAppLogsCommand(),
newAppSecretsCommand(),
newVersionCommand(),
newDeployCommand(),
newAppInfoCommand(),
newBuildsCommand(),
newAppHistoryCommand(),
newCertificatesCommand(),
newDocsCommand(),
newIPAddressesCommand(),
newConfigCommand(),
newScaleCommand(),
newPlatformCommand(),
newCurlCommand(),
newRegionsCommand(),
newOpenCommand(),
)
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)
}