-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add global/per-context configuration preferences, add configura…
…tion subcommand
- Loading branch information
Showing
25 changed files
with
648 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/state" | ||
) | ||
|
||
func NewCommand(s state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "config", | ||
Short: "Manage configuration", | ||
Args: util.Validate, | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
cmd.AddCommand( | ||
newSetCommand(s), | ||
) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/cli/internal/state/config" | ||
) | ||
|
||
func newSetCommand(s state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "set <key> <value>", | ||
Short: "Set a configuration value", | ||
Args: util.Validate, | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
RunE: state.Wrap(s, runSet), | ||
} | ||
cmd.Flags().Bool("global", false, "Set the value globally (for all contexts)") | ||
return cmd | ||
} | ||
|
||
func runSet(s state.State, cmd *cobra.Command, args []string) error { | ||
global, _ := cmd.Flags().GetBool("global") | ||
|
||
var prefs config.Preferences | ||
|
||
if global { | ||
prefs = s.Config().Preferences() | ||
} else { | ||
ctx := s.Config().ActiveContext() | ||
if ctx == nil { | ||
if ctxName := config.OptionContext.Value(); ctxName != "" { | ||
return fmt.Errorf("active context \"%s\" not found", ctxName) | ||
} else { | ||
return fmt.Errorf("no active context (use --global flag to set a global option)") | ||
} | ||
} | ||
prefs = ctx.Preferences() | ||
} | ||
|
||
key, value := args[0], args[1] | ||
if err := prefs.Set(key, value); err != nil { | ||
return err | ||
} | ||
|
||
return s.Config().Write(nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.