diff --git a/CHANGELOG.md b/CHANGELOG.md index 0713467b0..f18dbec44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - feat(compute/hashfiles): remove hashsum subcommand ([#1608](https://github.com/fastly/cli/pull/1608)) - feat(commands/ngwaf/rules): add support for CRUD operations for NGWAF rules ([#1605](https://github.com/fastly/cli/pull/1605)) - feat(compute/deploy): added the `--no-default-domain` flag to allow for the skipping of automatic domain creation when deploying a Compute service([#1610](https://github.com/fastly/cli/pull/1610)) +- refactor(argparser/flags.go): add flag conversion utilities for converting string flags to bools and checking ascending and desecnding flags ([#1611](https://github.com/fastly/cli/pull/1611)) - feat(commands/service/purge): Add 'service purge' command as replacement for 'purge', with an unlisted and deprecated alias of 'purge'. ([#1612](https://github.com/fastly/cli/pull/1612)) ### Bug fixes: diff --git a/pkg/argparser/flags.go b/pkg/argparser/flags.go index c08a685d9..1b5b4fcc3 100644 --- a/pkg/argparser/flags.go +++ b/pkg/argparser/flags.go @@ -373,3 +373,25 @@ func (j *JSONOutput) WriteJSON(out io.Writer, value any) (bool, error) { enc.SetIndent("", " ") return true, enc.Encode(value) } + +func ConvertBoolFromStringFlag(value string, argName string) (*bool, error) { + switch value { + case "true": + return fastly.ToPointer(true), nil + case "false": + return fastly.ToPointer(false), nil + default: + return nil, fmt.Errorf("'%s' flag must be one of the following [true, false]", argName) + } +} + +func ConvertOrderFromStringFlag(value string, argName string) (string, error) { + switch value { + case "asc": + return "", nil + case "desc": + return "-", nil + default: + return "", fmt.Errorf("'%s' flag must be one of the following [asc, desc]", argName) + } +} diff --git a/pkg/commands/alerts/list.go b/pkg/commands/alerts/list.go index c82b36359..561d57fdb 100644 --- a/pkg/commands/alerts/list.go +++ b/pkg/commands/alerts/list.go @@ -2,7 +2,6 @@ package alerts import ( "context" - "errors" "io" "github.com/fastly/cli/pkg/text" @@ -114,13 +113,10 @@ func (c *ListCommand) constructInput() (*fastly.ListAlertDefinitionsInput, error input.ServiceID = &c.serviceID.Value } var sign string + var err error if c.order.WasSet { - switch c.order.Value { - case "asc": - case "desc": - sign = "-" - default: - err := errors.New("'order' flag must be one of the following [asc, desc]") + sign, err = argparser.ConvertOrderFromStringFlag(c.order.Value, "order") + if err != nil { c.Globals.ErrLog.Add(err) return nil, err } diff --git a/pkg/commands/alerts/list_history.go b/pkg/commands/alerts/list_history.go index 02d3a6a84..18d28134e 100644 --- a/pkg/commands/alerts/list_history.go +++ b/pkg/commands/alerts/list_history.go @@ -2,7 +2,6 @@ package alerts import ( "context" - "errors" "io" "github.com/fastly/cli/pkg/text" @@ -114,13 +113,10 @@ func (c *ListHistoryCommand) constructInput() (*fastly.ListAlertHistoryInput, er input.Limit = &c.limit.Value } var sign string + var err error if c.order.WasSet { - switch c.order.Value { - case "asc": - case "desc": - sign = "-" - default: - err := errors.New("'order' flag must be one of the following [asc, desc]") + sign, err = argparser.ConvertOrderFromStringFlag(c.order.Value, "order") + if err != nil { c.Globals.ErrLog.Add(err) return nil, err } diff --git a/pkg/commands/backend/create.go b/pkg/commands/backend/create.go index 878602683..a2037fa11 100644 --- a/pkg/commands/backend/create.go +++ b/pkg/commands/backend/create.go @@ -2,7 +2,6 @@ package backend import ( "context" - "errors" "io" "net" @@ -194,20 +193,13 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { input.OverrideHost = &c.overrideHost.Value } if c.preferIPv6.WasSet { - var preferIPv6 bool - - switch c.preferIPv6.Value { - case "true": - preferIPv6 = true - case "false": - preferIPv6 = false - default: - err := errors.New("'prefer-ipv6' flag must be one of the following [true, false]") + preferIPv6, err := argparser.ConvertBoolFromStringFlag(c.preferIPv6.Value, "prefer-ipv6") + if err != nil { c.Globals.ErrLog.Add(err) return err } - input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(preferIPv6)) + input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(*preferIPv6)) } if c.requestCondition.WasSet { input.RequestCondition = &c.requestCondition.Value @@ -238,20 +230,13 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { input.SSLSNIHostname = &c.sslSNIHostname.Value } if c.tcpKaEnable.WasSet { - var tcpKaEnable bool - - switch c.tcpKaEnable.Value { - case "true": - tcpKaEnable = true - case "false": - tcpKaEnable = false - default: - err := errors.New("'tcp-ka-enabled' flag must be one of the following [true, false]") + tcpKaEnable, err := argparser.ConvertBoolFromStringFlag(c.tcpKaEnable.Value, "tcp-ka-enabled") + if err != nil { c.Globals.ErrLog.Add(err) return err } - input.TCPKeepAliveEnable = &tcpKaEnable + input.TCPKeepAliveEnable = tcpKaEnable } if c.tcpKaInterval.WasSet { input.TCPKeepAliveIntvl = &c.tcpKaInterval.Value diff --git a/pkg/commands/backend/update.go b/pkg/commands/backend/update.go index 9d48db064..b3263f65b 100644 --- a/pkg/commands/backend/update.go +++ b/pkg/commands/backend/update.go @@ -2,7 +2,6 @@ package backend import ( "context" - "errors" "io" "github.com/fastly/go-fastly/v12/fastly" @@ -174,20 +173,12 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { } if c.preferIPv6.WasSet { - var preferIPv6 bool - - switch c.preferIPv6.Value { - case "true": - preferIPv6 = true - case "false": - preferIPv6 = false - default: - err := errors.New("'prefer-ipv6' flag must be one of the following [true, false]") + preferIPv6, err := argparser.ConvertBoolFromStringFlag(c.preferIPv6.Value, "prefer-ipv6") + if err != nil { c.Globals.ErrLog.Add(err) return err } - - input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(preferIPv6)) + input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(*preferIPv6)) } if c.ConnectTimeout.WasSet { @@ -272,20 +263,12 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { } if c.TCPKaEnable.WasSet { - var tcpKaEnable bool - - switch c.TCPKaEnable.Value { - case "true": - tcpKaEnable = true - case "false": - tcpKaEnable = false - default: - err := errors.New("'tcp-ka-enabled' flag must be one of the following [true, false]") + tcpKaEnable, err := argparser.ConvertBoolFromStringFlag(c.TCPKaEnable.Value, "tcp-ka-enabled") + if err != nil { c.Globals.ErrLog.Add(err) return err } - - input.TCPKeepAliveEnable = &tcpKaEnable + input.TCPKeepAliveEnable = tcpKaEnable } if c.TCPKaInterval.WasSet { input.TCPKeepAliveIntvl = &c.TCPKaInterval.Value diff --git a/pkg/commands/dashboard/list.go b/pkg/commands/dashboard/list.go index 8bff745f2..678c12986 100644 --- a/pkg/commands/dashboard/list.go +++ b/pkg/commands/dashboard/list.go @@ -2,7 +2,6 @@ package dashboard import ( "context" - "errors" "io" "github.com/fastly/go-fastly/v12/fastly" @@ -121,13 +120,10 @@ func (c *ListCommand) constructInput() (*fastly.ListObservabilityCustomDashboard input.Limit = &c.limit.Value } var sign string + var err error if c.order.WasSet { - switch c.order.Value { - case "asc": - case "desc": - sign = "-" - default: - err := errors.New("'order' flag must be one of the following [asc, desc]") + sign, err = argparser.ConvertOrderFromStringFlag(c.order.Value, "order") + if err != nil { c.Globals.ErrLog.Add(err) return nil, err } diff --git a/pkg/commands/imageoptimizerdefaults/update.go b/pkg/commands/imageoptimizerdefaults/update.go index 826db09ce..5df3bc5b4 100644 --- a/pkg/commands/imageoptimizerdefaults/update.go +++ b/pkg/commands/imageoptimizerdefaults/update.go @@ -159,16 +159,12 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { } } if c.webp.WasSet { - var webp bool - switch c.webp.Value { - case "true": - webp = true - case "false": - webp = false - default: - return fmt.Errorf("'webp' flag must be one of the following [true, false]") + webp, err := argparser.ConvertBoolFromStringFlag(c.webp.Value, "webp") + if err != nil { + c.Globals.ErrLog.Add(err) + return err } - c.Input.Webp = &webp + c.Input.Webp = webp } if c.webpQuality.WasSet { c.Input.WebpQuality = &c.webpQuality.Value @@ -193,28 +189,20 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { c.Input.JpegQuality = &c.jpegQuality.Value } if c.upscale.WasSet { - var upscale bool - switch c.upscale.Value { - case "true": - upscale = true - case "false": - upscale = false - default: - return fmt.Errorf("'upscale' flag must be one of the following [true, false]") + upscale, err := argparser.ConvertBoolFromStringFlag(c.upscale.Value, "upscale") + if err != nil { + c.Globals.ErrLog.Add(err) + return err } - c.Input.Upscale = &upscale + c.Input.Upscale = upscale } if c.allowVideo.WasSet { - var allowVideo bool - switch c.allowVideo.Value { - case "true": - allowVideo = true - case "false": - allowVideo = false - default: - return fmt.Errorf("'allow-video' flag must be one of the following [true, false]") + allowVideo, err := argparser.ConvertBoolFromStringFlag(c.allowVideo.Value, "allow-video") + if err != nil { + c.Globals.ErrLog.Add(err) + return err } - c.Input.AllowVideo = &allowVideo + c.Input.AllowVideo = allowVideo } o, err := c.Globals.APIClient.UpdateImageOptimizerDefaultSettings(context.TODO(), &c.Input) diff --git a/pkg/commands/ngwaf/workspace/threshold/create.go b/pkg/commands/ngwaf/workspace/threshold/create.go index 43645bb72..798b41d75 100644 --- a/pkg/commands/ngwaf/workspace/threshold/create.go +++ b/pkg/commands/ngwaf/workspace/threshold/create.go @@ -75,26 +75,14 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { return err } - var enabled bool - switch c.enabled.Value { - case "true": - enabled = true - case "false": - enabled = false - default: - err := errors.New("'enabled' flag must be one of the following [true, false]") + enabled, err := argparser.ConvertBoolFromStringFlag(c.enabled.Value, "enabled") + if err != nil { c.Globals.ErrLog.Add(err) return err } - var dontNotify bool - switch c.dontNotify.Value { - case "true": - dontNotify = true - case "false": - dontNotify = false - default: - err := errors.New("'do-not-notify' flag must be one of the following [true, false]") + dontNotify, err := argparser.ConvertBoolFromStringFlag(c.dontNotify.Value, "do-not-notify") + if err != nil { c.Globals.ErrLog.Add(err) return err } @@ -102,11 +90,11 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { input := &thresholds.CreateInput{ Action: &c.action, Duration: &c.duration, - Enabled: &enabled, + Enabled: enabled, Interval: &c.interval, Limit: &c.limit, Name: &c.name, - DontNotify: &dontNotify, + DontNotify: dontNotify, Signal: &c.signal, WorkspaceID: &c.workspaceID.Value, } diff --git a/pkg/commands/ngwaf/workspace/threshold/update.go b/pkg/commands/ngwaf/workspace/threshold/update.go index d9626e95b..451d669a1 100644 --- a/pkg/commands/ngwaf/workspace/threshold/update.go +++ b/pkg/commands/ngwaf/workspace/threshold/update.go @@ -84,35 +84,23 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { input.Action = &c.action.Value } if c.dontNotify.WasSet { - var dontNotify bool - switch c.dontNotify.Value { - case "true": - dontNotify = true - case "false": - dontNotify = false - default: - err := errors.New("'do-not-notify' flag must be one of the following [true, false]") + dontNotify, err := argparser.ConvertBoolFromStringFlag(c.dontNotify.Value, "do-not-notify") + if err != nil { c.Globals.ErrLog.Add(err) return err } - input.DontNotify = &dontNotify + input.DontNotify = dontNotify } if c.duration.WasSet { input.Duration = &c.duration.Value } if c.enabled.WasSet { - var enabled bool - switch c.enabled.Value { - case "true": - enabled = true - case "false": - enabled = false - default: - err := errors.New("'enabled' flag must be one of the following [true, false]") + enabled, err := argparser.ConvertBoolFromStringFlag(c.enabled.Value, "enabled") + if err != nil { c.Globals.ErrLog.Add(err) return err } - input.Enabled = &enabled + input.Enabled = enabled } if c.interval.WasSet { input.Interval = &c.interval.Value diff --git a/pkg/commands/ngwaf/workspace/virtualpatch/update.go b/pkg/commands/ngwaf/workspace/virtualpatch/update.go index 8270b95c5..d4d334c4f 100644 --- a/pkg/commands/ngwaf/workspace/virtualpatch/update.go +++ b/pkg/commands/ngwaf/workspace/virtualpatch/update.go @@ -3,7 +3,6 @@ package virtualpatch import ( "context" "errors" - "fmt" "io" "github.com/fastly/go-fastly/v12/fastly" @@ -67,16 +66,12 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { WorkspaceID: &c.workspaceID, } if c.enabled.WasSet { - var enableToggle bool - switch c.enabled.Value { - case "true": - enableToggle = true - case "false": - enableToggle = false - default: - return fmt.Errorf("'enabled' flag must be one of the following [true, false]") + enabled, err := argparser.ConvertBoolFromStringFlag(c.enabled.Value, "enabled") + if err != nil { + c.Globals.ErrLog.Add(err) + return err } - input.Enabled = &enableToggle + input.Enabled = enabled } if c.mode.WasSet { input.Mode = &c.mode.Value