-
Notifications
You must be signed in to change notification settings - Fork 0
Remove global state #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,51 +16,62 @@ import ( | |
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var rootCmd = &cobra.Command{ | ||
| Use: "lstk", | ||
| Short: "LocalStack CLI", | ||
| Long: "lstk is the command-line interface for LocalStack.", | ||
| PreRunE: initConfig, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| rt, err := runtime.NewDockerRuntime() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if err := runStart(cmd.Context(), rt); err != nil { | ||
| if !output.IsSilent(err) { | ||
| func NewRootCmd(cfg *env.Env) *cobra.Command { | ||
| root := &cobra.Command{ | ||
| Use: "lstk", | ||
| Short: "LocalStack CLI", | ||
| Long: "lstk is the command-line interface for LocalStack.", | ||
| PreRunE: initConfig, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| rt, err := runtime.NewDockerRuntime() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| os.Exit(1) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.Version = version.Version() | ||
| rootCmd.SetVersionTemplate(versionLine() + "\n") | ||
| if err := runStart(cmd.Context(), rt, cfg); err != nil { | ||
| if !output.IsSilent(err) { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| } | ||
| os.Exit(1) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| root.Version = version.Version() | ||
| root.SetVersionTemplate(versionLine() + "\n") | ||
|
|
||
| configureHelp(root) | ||
|
|
||
| root.InitDefaultVersionFlag() | ||
| root.Flags().Lookup("version").Usage = "Show version" | ||
|
|
||
| configureHelp(rootCmd) | ||
| root.AddCommand( | ||
| newStartCmd(cfg), | ||
| newStopCmd(), | ||
| newLoginCmd(cfg), | ||
| newLogoutCmd(cfg), | ||
| newLogsCmd(), | ||
| newConfigCmd(), | ||
| newVersionCmd(), | ||
| ) | ||
|
|
||
| rootCmd.InitDefaultVersionFlag() | ||
| rootCmd.Flags().Lookup("version").Usage = "Show version" | ||
| rootCmd.AddCommand(startCmd) | ||
| return root | ||
| } | ||
|
|
||
| func Execute(ctx context.Context) error { | ||
| return rootCmd.ExecuteContext(ctx) | ||
| cfg := env.Init() | ||
| return NewRootCmd(cfg).ExecuteContext(ctx) | ||
|
Comment on lines
+63
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat -n cmd/root.go | head -80Repository: localstack/lstk Length of output: 2558 🏁 Script executed: cat -n internal/env/env.go | head -100Repository: localstack/lstk Length of output: 1185 🏁 Script executed: rg -n 'func Init|config\.|viper\.|os\.Getenv|LookupEnv' internal/env/env.go -A 2 -B 1Repository: localstack/lstk Length of output: 739 🏁 Script executed: rg -n 'func initConfig|PreRunE' cmd/root.go -A 5Repository: localstack/lstk Length of output: 360 🏁 Script executed: fd -e go internal/config | head -20Repository: localstack/lstk Length of output: 424 🏁 Script executed: rg -n 'func Init' internal/config -A 10 | head -50Repository: localstack/lstk Length of output: 666 🏁 Script executed: rg -n 'func loadConfig|func firstExistingConfigPath' internal/config -A 15Repository: localstack/lstk Length of output: 1763 🏁 Script executed: cat -n internal/config/config.go | head -80Repository: localstack/lstk Length of output: 2186 Move The current code captures env values at line 63 before Either call 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| func runStart(ctx context.Context, rt runtime.Runtime) error { | ||
| platformClient := api.NewPlatformClient() | ||
| func runStart(ctx context.Context, rt runtime.Runtime, cfg *env.Env) error { | ||
| platformClient := api.NewPlatformClient(cfg.APIEndpoint) | ||
| if ui.IsInteractive() { | ||
| return ui.Run(ctx, rt, version.Version(), platformClient) | ||
| return ui.Run(ctx, rt, version.Version(), platformClient, cfg.AuthToken, cfg.ForceFileKeyring, cfg.WebAppURL) | ||
| } | ||
| return container.Start(ctx, rt, output.NewPlainSink(os.Stdout), platformClient, false) | ||
| return container.Start(ctx, rt, output.NewPlainSink(os.Stdout), platformClient, cfg.AuthToken, cfg.ForceFileKeyring, cfg.WebAppURL, false) | ||
| } | ||
|
|
||
| func initConfig(_ *cobra.Command, _ []string) error { | ||
| env.Init() | ||
| return config.Init() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check stderr write errors before exiting.
fmt.Fprintfresults are ignored on Line 28 and Line 34. Please check and handle write errors explicitly.🛠️ Minimal fix
if err != nil { - fmt.Fprintf(os.Stderr, "Error: %v\n", err) + if _, writeErr := fmt.Fprintf(os.Stderr, "Error: %v\n", err); writeErr != nil { + // best-effort logging; continue exiting with original error + } os.Exit(1) } @@ if err := runStart(cmd.Context(), rt, cfg); err != nil { if !output.IsSilent(err) { - fmt.Fprintf(os.Stderr, "Error: %v\n", err) + if _, writeErr := fmt.Fprintf(os.Stderr, "Error: %v\n", err); writeErr != nil { + // best-effort logging; continue exiting with original error + } } os.Exit(1) }As per coding guidelines: "Errors returned by functions should always be checked unless in test files."
🤖 Prompt for AI Agents