diff --git a/app/cli/cmd/config_reset.go b/app/cli/cmd/config_reset.go index 3e7cd9131..e0d39db63 100644 --- a/app/cli/cmd/config_reset.go +++ b/app/cli/cmd/config_reset.go @@ -1,5 +1,5 @@ // -// Copyright 2023 The Chainloop Authors. +// Copyright 2023-2025 The Chainloop Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,6 +26,9 @@ func newConfigResetCmd() *cobra.Command { cmd := &cobra.Command{ Use: "reset", Short: "Reset the CLI configuration", + Annotations: map[string]string{ + skipActionOptsInit: trueString, + }, Run: func(cmd *cobra.Command, args []string) { configFile := viper.ConfigFileUsed() err := os.Remove(configFile) diff --git a/app/cli/cmd/config_save.go b/app/cli/cmd/config_save.go index 6bd2a2991..25851ecaf 100644 --- a/app/cli/cmd/config_save.go +++ b/app/cli/cmd/config_save.go @@ -1,5 +1,5 @@ // -// Copyright 2023 The Chainloop Authors. +// Copyright 2023-2025 The Chainloop Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,6 +25,9 @@ func newConfigSaveCmd() *cobra.Command { Use: "save", Short: "Persist the current settings to the config file", Example: "chainloop config save --control-plane localhost:1234 --artifact-cas localhost:1235", + Annotations: map[string]string{ + skipActionOptsInit: trueString, + }, RunE: func(cmd *cobra.Command, args []string) error { return viper.WriteConfig() }, diff --git a/app/cli/cmd/config_view.go b/app/cli/cmd/config_view.go index 367d1e809..ee78059cc 100644 --- a/app/cli/cmd/config_view.go +++ b/app/cli/cmd/config_view.go @@ -1,5 +1,5 @@ // -// Copyright 2023 The Chainloop Authors. +// Copyright 2023-2025 The Chainloop Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,6 +26,9 @@ func newConfigViewCmd() *cobra.Command { cmd := &cobra.Command{ Use: "view", Short: "View the current CLI configuration", + Annotations: map[string]string{ + skipActionOptsInit: trueString, + }, Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Config file: %s\n", viper.ConfigFileUsed()) diff --git a/app/cli/cmd/root.go b/app/cli/cmd/root.go index 4800e2cb3..b06e59375 100644 --- a/app/cli/cmd/root.go +++ b/app/cli/cmd/root.go @@ -58,7 +58,9 @@ const ( useAPIToken = "withAPITokenAuth" // Ask for confirmation when user token is used and API token is preferred confirmWhenUserToken = "confirmWhenUserToken" - appName = "chainloop" + // Skip ActionOpts initialization for commands that operate locally + skipActionOptsInit = "skipActionOptsInit" + appName = "chainloop" //nolint:gosec tokenEnvVarName = "CHAINLOOP_TOKEN" // Follow the convention stated on https://consoledonottrack.com/ @@ -102,6 +104,12 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command { logger.Debug().Str("path", viper.ConfigFileUsed()).Msg("using config file") + // Commands annotated with skipActionOptsInit don't need ActionOpts initialization + // These are local-only commands that don't interact with the control plane + if cmd.Annotations[skipActionOptsInit] == trueString { + return nil + } + if apiInsecure() { logger.Warn().Msg("API contacted in insecure mode") } @@ -203,7 +211,10 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command { return nil }, PersistentPostRunE: func(_ *cobra.Command, _ []string) error { - return cleanup(ActionOpts.CPConnection) + if ActionOpts != nil { + return cleanup(ActionOpts.CPConnection) + } + return nil }, }