Skip to content

Commit c00b1a5

Browse files
authored
fix(config): allow commands to run without valid connection (#2596)
Signed-off-by: Sylwester Piskozub <[email protected]>
1 parent ed92f11 commit c00b1a5

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

app/cli/cmd/config_reset.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023 The Chainloop Authors.
2+
// Copyright 2023-2025 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -26,6 +26,9 @@ func newConfigResetCmd() *cobra.Command {
2626
cmd := &cobra.Command{
2727
Use: "reset",
2828
Short: "Reset the CLI configuration",
29+
Annotations: map[string]string{
30+
skipActionOptsInit: trueString,
31+
},
2932
Run: func(cmd *cobra.Command, args []string) {
3033
configFile := viper.ConfigFileUsed()
3134
err := os.Remove(configFile)

app/cli/cmd/config_save.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023 The Chainloop Authors.
2+
// Copyright 2023-2025 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -25,6 +25,9 @@ func newConfigSaveCmd() *cobra.Command {
2525
Use: "save",
2626
Short: "Persist the current settings to the config file",
2727
Example: "chainloop config save --control-plane localhost:1234 --artifact-cas localhost:1235",
28+
Annotations: map[string]string{
29+
skipActionOptsInit: trueString,
30+
},
2831
RunE: func(cmd *cobra.Command, args []string) error {
2932
return viper.WriteConfig()
3033
},

app/cli/cmd/config_view.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023 The Chainloop Authors.
2+
// Copyright 2023-2025 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -26,6 +26,9 @@ func newConfigViewCmd() *cobra.Command {
2626
cmd := &cobra.Command{
2727
Use: "view",
2828
Short: "View the current CLI configuration",
29+
Annotations: map[string]string{
30+
skipActionOptsInit: trueString,
31+
},
2932
Run: func(cmd *cobra.Command, args []string) {
3033
fmt.Printf("Config file: %s\n", viper.ConfigFileUsed())
3134

app/cli/cmd/root.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ const (
5858
useAPIToken = "withAPITokenAuth"
5959
// Ask for confirmation when user token is used and API token is preferred
6060
confirmWhenUserToken = "confirmWhenUserToken"
61-
appName = "chainloop"
61+
// Skip ActionOpts initialization for commands that operate locally
62+
skipActionOptsInit = "skipActionOptsInit"
63+
appName = "chainloop"
6264
//nolint:gosec
6365
tokenEnvVarName = "CHAINLOOP_TOKEN"
6466
// Follow the convention stated on https://consoledonottrack.com/
@@ -102,6 +104,12 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
102104

103105
logger.Debug().Str("path", viper.ConfigFileUsed()).Msg("using config file")
104106

107+
// Commands annotated with skipActionOptsInit don't need ActionOpts initialization
108+
// These are local-only commands that don't interact with the control plane
109+
if cmd.Annotations[skipActionOptsInit] == trueString {
110+
return nil
111+
}
112+
105113
if apiInsecure() {
106114
logger.Warn().Msg("API contacted in insecure mode")
107115
}
@@ -203,7 +211,10 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
203211
return nil
204212
},
205213
PersistentPostRunE: func(_ *cobra.Command, _ []string) error {
206-
return cleanup(ActionOpts.CPConnection)
214+
if ActionOpts != nil {
215+
return cleanup(ActionOpts.CPConnection)
216+
}
217+
return nil
207218
},
208219
}
209220

0 commit comments

Comments
 (0)