Skip to content

Commit

Permalink
add Config.UseContext(*string), remove Config.Reset(), refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed May 29, 2024
1 parent ababd77 commit d54c018
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
5 changes: 1 addition & 4 deletions internal/cmd/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func runGet(s state.State, cmd *cobra.Command, args []string) error {
allowSensitive, _ := cmd.Flags().GetBool("allow-sensitive")

if global {
// set context to nil and then reload
config.OptionContext.OverrideAny(s.Config(), nil)
s.Config().Reset()
if err := config.ReadConfig(s.Config(), nil); err != nil {
if err := s.Config().UseContext(nil); err != nil {
return err
}
}
Expand Down
5 changes: 1 addition & 4 deletions internal/cmd/config/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ func runList(s state.State, cmd *cobra.Command, _ []string) error {
outOpts := output.FlagsForCommand(cmd)

if global {
// set context to nil and then reload
config.OptionContext.OverrideAny(s.Config(), nil)
s.Config().Reset()
if err := config.ReadConfig(s.Config(), nil); err != nil {
if err := s.Config().UseContext(nil); err != nil {
return err
}
}
Expand Down
22 changes: 17 additions & 5 deletions internal/state/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ type Config interface {
// Write writes the config to the given writer. If w is nil, the config is written to the config file.
Write(w io.Writer) error

// Reset resets the config by creating a new viper instance and a new FlagSet
Reset()
// ParseConfigFile parses the given config file f, environment variables and flags and reads the values into the config
ParseConfigFile(f any) error

// ActiveContext returns the currently active context
ActiveContext() Context
// SetActiveContext sets the currently active context and also modifies the schema to reflect this change
// This does NOT change any configuration values. Use [ReadConfig] to re-read the newly set active context.
// This does NOT change any configuration values. Use [config.Config.UseConfig] to read the actual context into memory.
SetActiveContext(Context)
// Contexts returns a list of currently loaded contexts
Contexts() []Context
// SetContexts sets the list of contexts and also modifies the schema to reflect this change
SetContexts([]Context)
// UseContext temporarily switches context to the given context name and reloads the config, loading the values of the given context.
// If name is nil, the context is unloaded and only the global preferences are used.
// This change will not be written to the schema, so `active_context` will not be changed after writing.
UseContext(name *string) error

// Preferences returns the global preferences (as opposed to [Context.Preferences])
Preferences() Preferences
Expand Down Expand Up @@ -66,11 +68,11 @@ type config struct {

func NewConfig() Config {
cfg := &config{}
cfg.Reset()
cfg.reset()
return cfg
}

func (cfg *config) Reset() {
func (cfg *config) reset() {
cfg.v = viper.New()
cfg.v.SetConfigType("toml")
cfg.v.SetEnvPrefix("HCLOUD")
Expand Down Expand Up @@ -257,6 +259,16 @@ func (cfg *config) SetContexts(contexts []Context) {
cfg.schema.Contexts = cfg.contexts
}

func (cfg *config) UseContext(name *string) error {
if name == nil {
OptionContext.OverrideAny(cfg, nil)
} else {
OptionContext.OverrideAny(cfg, *name)
}
cfg.reset()
return ReadConfig(cfg, nil)
}

func (cfg *config) Preferences() Preferences {
if cfg.preferences == nil {
cfg.preferences = make(Preferences)
Expand Down

0 comments on commit d54c018

Please sign in to comment.