-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathroot.go
More file actions
184 lines (156 loc) · 4.54 KB
/
Copy pathroot.go
File metadata and controls
184 lines (156 loc) · 4.54 KB
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
package cmd
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/fosrl/cli/cmd/apply"
"github.com/fosrl/cli/cmd/auth"
"github.com/fosrl/cli/cmd/auth/login"
"github.com/fosrl/cli/cmd/auth/logout"
"github.com/fosrl/cli/cmd/authdaemon"
"github.com/fosrl/cli/cmd/down"
"github.com/fosrl/cli/cmd/logs"
selectcmd "github.com/fosrl/cli/cmd/select"
"github.com/fosrl/cli/cmd/ssh"
"github.com/fosrl/cli/cmd/status"
"github.com/fosrl/cli/cmd/up"
"github.com/fosrl/cli/cmd/update"
"github.com/fosrl/cli/cmd/version"
"github.com/fosrl/cli/internal/api"
"github.com/fosrl/cli/internal/config"
"github.com/fosrl/cli/internal/logger"
versionpkg "github.com/fosrl/cli/internal/version"
"github.com/spf13/cobra"
)
// Initialize a root Cobra command.
//
// Set initResources to false when generating documentation to avoid
// parsing configuration files and instantiating the API client, among
// other such external resources. This is to avoid depending on external
// state when doing doc generation.
func RootCommand(initResources bool) (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "pangolin",
Short: "Pangolin CLI",
SilenceUsage: true,
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
PersistentPreRunE: mainCommandPreRun,
}
cmd.AddCommand(auth.AuthCommand())
if authDaemonCmd := authdaemon.AuthDaemonCmd(); authDaemonCmd != nil {
cmd.AddCommand(authDaemonCmd)
}
cmd.AddCommand(apply.ApplyCommand())
cmd.AddCommand(selectcmd.SelectCmd())
// Platform-specific commands - nil on unsupported platforms
if upCmd := up.UpCmd(); upCmd != nil {
cmd.AddCommand(upCmd)
}
if downCmd := down.DownCmd(); downCmd != nil {
cmd.AddCommand(downCmd)
}
if logsCmd := logs.LogsCmd(); logsCmd != nil {
cmd.AddCommand(logsCmd)
}
if statusCmd := status.StatusCmd(); statusCmd != nil {
cmd.AddCommand(statusCmd)
}
cmd.AddCommand(ssh.SSHCmd())
cmd.AddCommand(update.UpdateCmd())
cmd.AddCommand(version.VersionCmd())
cmd.AddCommand(login.LoginCmd())
cmd.AddCommand(logout.LogoutCmd())
if !initResources {
return cmd, nil
}
cfg, err := config.LoadConfig()
if err != nil {
return nil, err
}
if err := cfg.Validate(); err != nil {
return nil, err
}
accountStore, err := config.LoadAccountStore()
if err != nil {
return nil, err
}
var apiBaseURL string
var sessionToken string
if activeAccount, _ := accountStore.ActiveAccount(); activeAccount != nil {
apiBaseURL = activeAccount.Host
sessionToken = activeAccount.SessionToken
} else {
apiBaseURL = ""
sessionToken = ""
}
client, err := api.InitClient(apiBaseURL, sessionToken)
if err != nil {
return nil, err
}
ctx := context.Background()
ctx = api.WithAPIClient(ctx, client)
ctx = config.WithAccountStore(ctx, accountStore)
ctx = config.WithConfig(ctx, cfg)
cmd.SetContext(ctx)
return cmd, nil
}
func mainCommandPreRun(cmd *cobra.Command, args []string) error {
if shouldSkipRuntimeInit(cmd) {
return nil
}
cfg := config.ConfigFromContext(cmd.Context())
if err := ensureRuntimeDirs(cfg); err != nil {
return err
}
// Check for updates asynchronously
if !cfg.DisableUpdateCheck {
versionpkg.CheckForUpdateAsync(func(release *versionpkg.GitHubRelease) {
logger.Warning("A new version is available: %s (current: %s)", release.TagName, versionpkg.Version)
logger.Info("Run 'pangolin update' to update to the latest version")
fmt.Println()
})
}
return nil
}
// shouldSkipRuntimeInit returns true for commands that must not touch runtime
// directories or emit diagnostics to stdout (for example shell completion).
func shouldSkipRuntimeInit(cmd *cobra.Command) bool {
for c := cmd; c != nil; c = c.Parent() {
switch c.Name() {
case "completion", "version", "update":
return true
}
}
return false
}
// Make sure all required directories exist once before executing subcommands.
func ensureRuntimeDirs(cfg *config.Config) error {
configDir, err := config.GetPangolinConfigDir()
if err != nil {
return fmt.Errorf("failed to create pangolin configuration directory: %w", err)
}
if err := os.MkdirAll(configDir, 0o755); err != nil {
return fmt.Errorf("failed to create %s: %w", configDir, err)
}
if cfg.LogFile != "" {
logPathDirname := filepath.Dir(cfg.LogFile)
if err := os.MkdirAll(logPathDirname, 0o755); err != nil {
return fmt.Errorf("failed to create %s: %w", logPathDirname, err)
}
}
return nil
}
// Execute is called by main.go
func Execute() {
cmd, err := RootCommand(true)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}