-
Notifications
You must be signed in to change notification settings - Fork 235
/
root.go
120 lines (103 loc) · 2.94 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"
"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"
"github.com/superfly/flyctl/internal/flyerr"
)
func NewRootCmd(client *client.Client) *cobra.Command {
rootStrings := docstrings.Get("flyctl")
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
},
},
}
rootCmd.PersistentFlags().StringP("access-token", "t", "", "Fly API Access Token")
err := viper.BindPFlag(flyctl.ConfigAPIToken, rootCmd.PersistentFlags().Lookup("access-token"))
checkErr(err)
rootCmd.PersistentFlags().Bool("verbose", 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(
newCurlCommand(client),
newCertificatesCommand(client),
newConfigCommand(client),
newDashboardCommand(client),
newDeployCommand(client),
newDocsCommand(client),
newHistoryCommand(client),
newInfoCommand(client),
newIPAddressesCommand(client),
newListCommand(client),
newLogsCommand(client),
newMonitorCommand(client),
newOpenCommand(client),
newPlatformCommand(client),
newRegionsCommand(client),
newReleasesCommand(client),
newScaleCommand(client),
newAutoscaleCommand(client),
newSecretsCommand(client),
newStatusCommand(client),
newDNSCommand(client),
newDomainsCommand(client),
newImageCommand(client),
newVolumesCommand(client),
newWireGuardCommand(client),
newSSHCommand(client),
newAgentCommand(client),
newChecksCommand(client),
newPostgresCommand(client),
newVMCommand(client),
newLaunchCommand(client),
newMachineCommand(client),
newProxyCommand(client),
newDigCommand(client),
newTurbokuCommand(client),
)
return rootCmd.Command
}
func checkErr(err error) {
if err == nil {
return
}
if !isCancelledError(err) {
fmt.Println(aurora.Red("Error"), err)
}
os.Exit(1)
}
func isCancelledError(err error) bool {
if err == flyerr.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
}