Skip to content

Commit

Permalink
refactor, remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jun 5, 2024
1 parent a61a856 commit ce0e5d3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 70 deletions.
24 changes: 7 additions & 17 deletions internal/cmd/config/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,18 @@ func NewAddCommand(s state.State) *cobra.Command {
func runAdd(s state.State, cmd *cobra.Command, args []string) error {
global, _ := cmd.Flags().GetBool("global")

var (
added []any
ctx config.Context
prefs config.Preferences
)

if global {
prefs = s.Config().Preferences()
} else {
ctx = s.Config().ActiveContext()
if util.IsNil(ctx) {
return fmt.Errorf("no active context (use --global flag to set a global option)")
}
prefs = ctx.Preferences()
ctx, prefs, err := getPreferences(s.Config(), global)
if err != nil {
return err
}

key, values := args[0], args[1:]
opt, ok := config.Options[key]
if !ok || !opt.HasFlag(config.OptionFlagPreference) {
return fmt.Errorf("unknown preference: %s", key)
opt, err := getPreference(key)
if err != nil {
return err
}

var added []any
val, _ := prefs.Get(key)
switch opt.T().(type) {
case []string:
Expand Down
24 changes: 7 additions & 17 deletions internal/cmd/config/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,20 @@ func NewRemoveCommand(s state.State) *cobra.Command {
func runRemove(s state.State, cmd *cobra.Command, args []string) error {
global, _ := cmd.Flags().GetBool("global")

var (
removed []any
ctx config.Context
prefs config.Preferences
)

if global {
prefs = s.Config().Preferences()
} else {
ctx = s.Config().ActiveContext()
if util.IsNil(ctx) {
return fmt.Errorf("no active context (use --global to remove an option globally)")
}
prefs = ctx.Preferences()
ctx, prefs, err := getPreferences(s.Config(), global)
if err != nil {
return err
}

key, values := args[0], args[1:]
opt, ok := config.Options[key]
if !ok || !opt.HasFlag(config.OptionFlagPreference) {
return fmt.Errorf("unknown preference: %s", key)
opt, err := getPreference(key)
if err != nil {
return err
}

val, _ := prefs.Get(key)

var removed []any
switch opt.T().(type) {
case []string:
before := util.AnyToStringSlice(val)
Expand Down
24 changes: 7 additions & 17 deletions internal/cmd/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,18 @@ func NewSetCommand(s state.State) *cobra.Command {
func runSet(s state.State, cmd *cobra.Command, args []string) error {
global, _ := cmd.Flags().GetBool("global")

var (
val any
ctx config.Context
prefs config.Preferences
)

if global {
prefs = s.Config().Preferences()
} else {
ctx = s.Config().ActiveContext()
if util.IsNil(ctx) {
return fmt.Errorf("no active context (use --global flag to set a global option)")
}
prefs = ctx.Preferences()
ctx, prefs, err := getPreferences(s.Config(), global)
if err != nil {
return err
}

key, values := args[0], args[1:]
opt, ok := config.Options[key]
if !ok || !opt.HasFlag(config.OptionFlagPreference) {
return fmt.Errorf("unknown preference: %s", key)
opt, err := getPreference(key)
if err != nil {
return err
}

var val any
switch t := opt.T().(type) {
case bool:
if len(values) != 1 {
Expand Down
25 changes: 6 additions & 19 deletions internal/cmd/config/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,17 @@ func NewUnsetCommand(s state.State) *cobra.Command {
func runUnset(s state.State, cmd *cobra.Command, args []string) error {
global, _ := cmd.Flags().GetBool("global")

var (
ctx config.Context
prefs config.Preferences
)

if global {
prefs = s.Config().Preferences()
} else {
ctx = s.Config().ActiveContext()
if util.IsNil(ctx) {
return fmt.Errorf("no active context (use --global flag to unset a global option)")
}
prefs = ctx.Preferences()
ctx, prefs, err := getPreferences(s.Config(), global)
if err != nil {
return err
}

key := args[0]
opt, ok := config.Options[key]
if !ok || !opt.HasFlag(config.OptionFlagPreference) {
return fmt.Errorf("unknown preference: %s", key)
if _, err = getPreference(key); err != nil {
return err
}

ok = prefs.Unset(key)

if !ok {
if !prefs.Unset(key) {
_, _ = fmt.Fprintf(os.Stderr, "Warning: key '%s' was not set\n", key)
}
if global {
Expand Down
29 changes: 29 additions & 0 deletions internal/cmd/config/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"fmt"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state/config"
)

func getPreferences(cfg config.Config, global bool) (ctx config.Context, prefs config.Preferences, _ error) {
if global {
prefs = cfg.Preferences()
} else {
ctx = cfg.ActiveContext()
if util.IsNil(ctx) {
return nil, nil, fmt.Errorf("no active context (use --global flag to set a global option)")
}
prefs = ctx.Preferences()
}
return
}

func getPreference(key string) (config.IOption, error) {
opt, ok := config.Options[key]
if !ok || !opt.HasFlag(config.OptionFlagPreference) {
return nil, fmt.Errorf("unknown preference: %s", key)
}
return opt, nil
}

0 comments on commit ce0e5d3

Please sign in to comment.