-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathroot.go
More file actions
297 lines (267 loc) · 8.54 KB
/
Copy pathroot.go
File metadata and controls
297 lines (267 loc) · 8.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package cmd
import (
"context"
"fmt"
"io"
"os"
"runtime"
"strings"
"time"
"github.com/charmbracelet/fang"
"github.com/charmbracelet/lipgloss/v2"
"github.com/kernel/cli/cmd/mcp"
"github.com/kernel/cli/cmd/proxies"
"github.com/kernel/cli/pkg/auth"
"github.com/kernel/cli/pkg/update"
"github.com/kernel/cli/pkg/util"
"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type Metadata struct {
Version string
Commit string
Date string
GoVersion string
}
var metadata = Metadata{
// these are set at build-time via ldflags.
// https://goreleaser.com/cookbooks/using-main.version/
Version: "dev",
Commit: "none",
Date: "unknown",
GoVersion: runtime.Version(),
}
// rootCmd is the base command for the CLI.
var rootCmd = &cobra.Command{
Use: "kernel",
Short: "CLI for Kernel deployment and invocation",
Run: func(cmd *cobra.Command, args []string) {
// If called without any subcommands, just show help.
_ = cmd.Help()
},
}
var logger *pterm.Logger
func logLevelToPterm(level string) pterm.LogLevel {
switch level {
case "trace":
return pterm.LogLevelTrace
case "debug":
return pterm.LogLevelDebug
case "info":
return pterm.LogLevelInfo
case "warn":
return pterm.LogLevelWarn
case "error":
return pterm.LogLevelError
case "fatal":
return pterm.LogLevelFatal
case "print":
return pterm.LogLevelPrint
default:
return pterm.LogLevelInfo
}
}
func getKernelClient(cmd *cobra.Command) kernel.Client {
return util.GetKernelClient(cmd)
}
// isAuthExempt returns true if the command should skip auth.
func isAuthExempt(cmd *cobra.Command) bool {
// Root command doesn't need auth
if cmd == rootCmd {
return true
}
// Walk up to find the top-level command (direct child of rootCmd)
topLevel := cmd
for topLevel.Parent() != nil && topLevel.Parent() != rootCmd {
topLevel = topLevel.Parent()
}
// Check if the top-level command is in the exempt list
switch topLevel.Name() {
case "login", "logout", "help", "completion", "create", "mcp", "upgrade", "status":
return true
case "auth":
// Only exempt the auth command itself (status display), not its subcommands
return cmd == topLevel
}
return false
}
func init() {
rootCmd.PersistentFlags().BoolP("version", "v", false, "Print the CLI version")
rootCmd.PersistentFlags().BoolP("no-color", "", false, "Disable color output")
rootCmd.PersistentFlags().String("log-level", "warn", "Set the log level (trace, debug, info, warn, error, fatal, print)")
rootCmd.PersistentFlags().String("project", "", "Project ID or name to scope all requests to (or set KERNEL_PROJECT_ID env var)")
rootCmd.SilenceUsage = true
rootCmd.SilenceErrors = true
cobra.OnInitialize(initConfig)
// Version flag handling: we use our own persistent pre-run to handle it globally.
// We also inject a Kernel client object into the command context for commands to use
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
logLevel, _ := cmd.Flags().GetString("log-level")
logger = pterm.DefaultLogger.WithLevel(logLevelToPterm(logLevel))
if noColor, _ := cmd.Flags().GetBool("no-color"); noColor {
pterm.DisableStyling()
}
// Skip auth check for commands that don't need it (including children, e.g., "completion zsh")
if isAuthExempt(cmd) {
return nil
}
clientOpts := []option.RequestOption{
option.WithHeader("X-Kernel-Cli-Version", metadata.Version),
}
projectVal, _ := cmd.Flags().GetString("project")
if projectVal == "" {
projectVal = os.Getenv("KERNEL_PROJECT_ID")
}
// If the value looks like a name (not a cuid2 ID), we need to
// resolve it after authenticating. Build the client first without
// the project header, resolve, then re-create with the header.
needsResolve := projectVal != "" && !looksLikeCUID(projectVal)
if projectVal != "" && !needsResolve {
clientOpts = append(clientOpts, option.WithHeader("X-Kernel-Project-Id", projectVal))
}
client, err := auth.GetAuthenticatedClient(clientOpts...)
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
if needsResolve {
resolved, resolveErr := resolveProjectByName(cmd.Context(), *client, projectVal)
if resolveErr != nil {
return resolveErr
}
clientOpts = append(clientOpts, option.WithHeader("X-Kernel-Project-Id", resolved))
client, err = auth.GetAuthenticatedClient(clientOpts...)
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
}
ctx := context.WithValue(cmd.Context(), util.KernelClientKey, *client)
cmd.SetContext(ctx)
return nil
}
// Register subcommands
rootCmd.AddCommand(deployCmd)
rootCmd.AddCommand(invokeCmd)
rootCmd.AddCommand(browsersCmd)
rootCmd.AddCommand(browserPoolsCmd)
rootCmd.AddCommand(appCmd)
rootCmd.AddCommand(profilesCmd)
rootCmd.AddCommand(proxies.ProxiesCmd)
rootCmd.AddCommand(extensionsCmd)
rootCmd.AddCommand(credentialsCmd)
rootCmd.AddCommand(credentialProvidersCmd)
rootCmd.AddCommand(projectsCmd)
rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(mcp.MCPCmd)
rootCmd.AddCommand(upgradeCmd)
rootCmd.AddCommand(statusCmd)
rootCmd.PersistentPostRunE = func(cmd *cobra.Command, args []string) error {
// running synchronously so we never slow the command
update.MaybeShowMessage(cmd.Context(), metadata.Version, 24*time.Hour)
return nil
}
}
func initConfig() {
// Placeholder for future configuration (env vars, config files, etc.)
pterm.EnableStyling() // ensure pterm is initialised in case env disables it
}
// Execute executes the root command.
func Execute(m Metadata) {
metadata = m
vt := "kernel"
if metadata.Version != "" {
vt += " " + metadata.Version
}
if metadata.Commit != "" {
vt += " (" + metadata.Commit + ")"
}
if metadata.GoVersion != "" {
vt += " " + metadata.GoVersion
}
if metadata.Date != "" {
vt += " " + metadata.Date
}
vt += "\n"
rootCmd.SetVersionTemplate(vt)
if err := fang.Execute(context.Background(), rootCmd,
fang.WithVersion(metadata.Version),
fang.WithCommit(metadata.Commit),
fang.WithErrorHandler(func(w io.Writer, styles fang.Styles, err error) {
err = util.CleanedUpSdkError{Err: err}
// remove margins so that it matches other pterm.error "style"
// we should add them back later as it looks cleaner
errorTextStyle := styles.ErrorText.UnsetMargins()
pterm.Error.Println(errorTextStyle.Render(strings.TrimSpace(err.Error())))
if isUsageError(err) {
pterm.Println()
pterm.Println(lipgloss.JoinHorizontal(
lipgloss.Left,
errorTextStyle.UnsetWidth().Render("Try"),
styles.Program.Flag.Render("--help"),
errorTextStyle.UnsetWidth().UnsetTransform().PaddingLeft(1).Render("for usage."),
))
}
}),
); err != nil {
// fang takes care of printing the error
os.Exit(1)
}
}
// isUsageError is a hack to detect usage errors.
// See: https://github.com/spf13/cobra/pull/2266
// from github.com/charmbracelet/fang/help.go
func isUsageError(err error) bool {
s := err.Error()
for _, prefix := range []string{
"flag needs an argument:",
"unknown flag:",
"unknown shorthand flag:",
"unknown command",
"invalid argument",
} {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
// looksLikeCUID returns true if s matches the cuid2 format used for resource IDs.
// Delegates to the shared cuidRegex defined in browsers.go.
func looksLikeCUID(s string) bool {
return cuidRegex.MatchString(s)
}
// resolveProjectByName lists the caller's projects and returns the ID of the
// one whose name matches (case-insensitive). Returns an error if no match or
// multiple matches are found.
func resolveProjectByName(ctx context.Context, client kernel.Client, name string) (string, error) {
projects, err := client.Projects.List(ctx, kernel.ProjectListParams{})
if err != nil {
return "", fmt.Errorf("failed to resolve project name %q: %w", name, err)
}
var matched []struct{ id, name string }
lower := strings.ToLower(name)
for _, p := range projects.Items {
if strings.ToLower(p.Name) == lower {
matched = append(matched, struct{ id, name string }{p.ID, p.Name})
}
}
switch len(matched) {
case 0:
return "", fmt.Errorf("no project found with name %q", name)
case 1:
pterm.Debug.Printf("Resolved project %q → %s\n", matched[0].name, matched[0].id)
return matched[0].id, nil
default:
return "", fmt.Errorf("multiple projects match name %q; use a project ID instead", name)
}
}
// onCancel runs a function when the provided context is cancelled
func onCancel(ctx context.Context, fn func()) {
go func() {
<-ctx.Done()
if ctx.Err() == context.Canceled {
fn()
}
}()
}