From ce0e5d3e00230ab4208c6b4d7f63314849eada6b Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:31:59 +0200 Subject: [PATCH] refactor, remove duplicate code --- internal/cmd/config/add.go | 24 +++++++----------------- internal/cmd/config/remove.go | 24 +++++++----------------- internal/cmd/config/set.go | 24 +++++++----------------- internal/cmd/config/unset.go | 25 ++++++------------------- internal/cmd/config/util.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 70 deletions(-) create mode 100644 internal/cmd/config/util.go diff --git a/internal/cmd/config/add.go b/internal/cmd/config/add.go index 96a23577..adeeac4b 100644 --- a/internal/cmd/config/add.go +++ b/internal/cmd/config/add.go @@ -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: diff --git a/internal/cmd/config/remove.go b/internal/cmd/config/remove.go index 1825098a..e908d248 100644 --- a/internal/cmd/config/remove.go +++ b/internal/cmd/config/remove.go @@ -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) diff --git a/internal/cmd/config/set.go b/internal/cmd/config/set.go index c5a98077..4b2bb09f 100644 --- a/internal/cmd/config/set.go +++ b/internal/cmd/config/set.go @@ -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 { diff --git a/internal/cmd/config/unset.go b/internal/cmd/config/unset.go index 02db1708..0a1ad2d9 100644 --- a/internal/cmd/config/unset.go +++ b/internal/cmd/config/unset.go @@ -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 { diff --git a/internal/cmd/config/util.go b/internal/cmd/config/util.go new file mode 100644 index 00000000..6a7a1bab --- /dev/null +++ b/internal/cmd/config/util.go @@ -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 +}