Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([#1578](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))

### Bug fixes:

Expand Down
22 changes: 22 additions & 0 deletions pkg/argparser/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
10 changes: 3 additions & 7 deletions pkg/commands/alerts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package alerts

import (
"context"
"errors"
"io"

"github.com/fastly/cli/pkg/text"
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/commands/alerts/list_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package alerts

import (
"context"
"errors"
"io"

"github.com/fastly/cli/pkg/text"
Expand Down Expand Up @@ -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
}
Expand Down
27 changes: 6 additions & 21 deletions pkg/commands/backend/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backend

import (
"context"
"errors"
"io"
"net"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 6 additions & 23 deletions pkg/commands/backend/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backend

import (
"context"
"errors"
"io"

"github.com/fastly/go-fastly/v12/fastly"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions pkg/commands/dashboard/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dashboard

import (
"context"
"errors"
"io"

"github.com/fastly/go-fastly/v12/fastly"
Expand Down Expand Up @@ -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
}
Expand Down
42 changes: 15 additions & 27 deletions pkg/commands/imageoptimizerdefaults/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
24 changes: 6 additions & 18 deletions pkg/commands/ngwaf/workspace/threshold/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,26 @@ 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
}

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,
}
Expand Down
24 changes: 6 additions & 18 deletions pkg/commands/ngwaf/workspace/threshold/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions pkg/commands/ngwaf/workspace/virtualpatch/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package virtualpatch
import (
"context"
"errors"
"fmt"
"io"

"github.com/fastly/go-fastly/v12/fastly"
Expand Down Expand Up @@ -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
Expand Down