Skip to content

Commit c22a60b

Browse files
Add flag conversion utilities for converting string flags to bools and checking ascending and desecnding flags (#1611)
### Change summary Adds 2 new utilities, one for converting string command line flags to booleans and one for checking `asc` or `desc` ordering flags All Submissions: * [x] Have you followed the guidelines in our Contributing document? * [x] Have you checked to ensure there aren't other open [Pull Requests](https://github.com/fastly/cli/pulls) for the same update/change? <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### New Feature Submissions: * [ ] Does your submission pass tests?
1 parent f0529c9 commit c22a60b

11 files changed

Lines changed: 76 additions & 138 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- feat(compute/hashfiles): remove hashsum subcommand ([#1608](https://github.com/fastly/cli/pull/1608))
1010
- feat(commands/ngwaf/rules): add support for CRUD operations for NGWAF rules ([#1605](https://github.com/fastly/cli/pull/1605))
1111
- 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))
12+
- 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))
1213
- 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))
1314

1415
### Bug fixes:

pkg/argparser/flags.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,25 @@ func (j *JSONOutput) WriteJSON(out io.Writer, value any) (bool, error) {
373373
enc.SetIndent("", " ")
374374
return true, enc.Encode(value)
375375
}
376+
377+
func ConvertBoolFromStringFlag(value string, argName string) (*bool, error) {
378+
switch value {
379+
case "true":
380+
return fastly.ToPointer(true), nil
381+
case "false":
382+
return fastly.ToPointer(false), nil
383+
default:
384+
return nil, fmt.Errorf("'%s' flag must be one of the following [true, false]", argName)
385+
}
386+
}
387+
388+
func ConvertOrderFromStringFlag(value string, argName string) (string, error) {
389+
switch value {
390+
case "asc":
391+
return "", nil
392+
case "desc":
393+
return "-", nil
394+
default:
395+
return "", fmt.Errorf("'%s' flag must be one of the following [asc, desc]", argName)
396+
}
397+
}

pkg/commands/alerts/list.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package alerts
22

33
import (
44
"context"
5-
"errors"
65
"io"
76

87
"github.com/fastly/cli/pkg/text"
@@ -114,13 +113,10 @@ func (c *ListCommand) constructInput() (*fastly.ListAlertDefinitionsInput, error
114113
input.ServiceID = &c.serviceID.Value
115114
}
116115
var sign string
116+
var err error
117117
if c.order.WasSet {
118-
switch c.order.Value {
119-
case "asc":
120-
case "desc":
121-
sign = "-"
122-
default:
123-
err := errors.New("'order' flag must be one of the following [asc, desc]")
118+
sign, err = argparser.ConvertOrderFromStringFlag(c.order.Value, "order")
119+
if err != nil {
124120
c.Globals.ErrLog.Add(err)
125121
return nil, err
126122
}

pkg/commands/alerts/list_history.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package alerts
22

33
import (
44
"context"
5-
"errors"
65
"io"
76

87
"github.com/fastly/cli/pkg/text"
@@ -114,13 +113,10 @@ func (c *ListHistoryCommand) constructInput() (*fastly.ListAlertHistoryInput, er
114113
input.Limit = &c.limit.Value
115114
}
116115
var sign string
116+
var err error
117117
if c.order.WasSet {
118-
switch c.order.Value {
119-
case "asc":
120-
case "desc":
121-
sign = "-"
122-
default:
123-
err := errors.New("'order' flag must be one of the following [asc, desc]")
118+
sign, err = argparser.ConvertOrderFromStringFlag(c.order.Value, "order")
119+
if err != nil {
124120
c.Globals.ErrLog.Add(err)
125121
return nil, err
126122
}

pkg/commands/backend/create.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package backend
22

33
import (
44
"context"
5-
"errors"
65
"io"
76
"net"
87

@@ -194,20 +193,13 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
194193
input.OverrideHost = &c.overrideHost.Value
195194
}
196195
if c.preferIPv6.WasSet {
197-
var preferIPv6 bool
198-
199-
switch c.preferIPv6.Value {
200-
case "true":
201-
preferIPv6 = true
202-
case "false":
203-
preferIPv6 = false
204-
default:
205-
err := errors.New("'prefer-ipv6' flag must be one of the following [true, false]")
196+
preferIPv6, err := argparser.ConvertBoolFromStringFlag(c.preferIPv6.Value, "prefer-ipv6")
197+
if err != nil {
206198
c.Globals.ErrLog.Add(err)
207199
return err
208200
}
209201

210-
input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(preferIPv6))
202+
input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(*preferIPv6))
211203
}
212204
if c.requestCondition.WasSet {
213205
input.RequestCondition = &c.requestCondition.Value
@@ -238,20 +230,13 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
238230
input.SSLSNIHostname = &c.sslSNIHostname.Value
239231
}
240232
if c.tcpKaEnable.WasSet {
241-
var tcpKaEnable bool
242-
243-
switch c.tcpKaEnable.Value {
244-
case "true":
245-
tcpKaEnable = true
246-
case "false":
247-
tcpKaEnable = false
248-
default:
249-
err := errors.New("'tcp-ka-enabled' flag must be one of the following [true, false]")
233+
tcpKaEnable, err := argparser.ConvertBoolFromStringFlag(c.tcpKaEnable.Value, "tcp-ka-enabled")
234+
if err != nil {
250235
c.Globals.ErrLog.Add(err)
251236
return err
252237
}
253238

254-
input.TCPKeepAliveEnable = &tcpKaEnable
239+
input.TCPKeepAliveEnable = tcpKaEnable
255240
}
256241
if c.tcpKaInterval.WasSet {
257242
input.TCPKeepAliveIntvl = &c.tcpKaInterval.Value

pkg/commands/backend/update.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package backend
22

33
import (
44
"context"
5-
"errors"
65
"io"
76

87
"github.com/fastly/go-fastly/v12/fastly"
@@ -174,20 +173,12 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
174173
}
175174

176175
if c.preferIPv6.WasSet {
177-
var preferIPv6 bool
178-
179-
switch c.preferIPv6.Value {
180-
case "true":
181-
preferIPv6 = true
182-
case "false":
183-
preferIPv6 = false
184-
default:
185-
err := errors.New("'prefer-ipv6' flag must be one of the following [true, false]")
176+
preferIPv6, err := argparser.ConvertBoolFromStringFlag(c.preferIPv6.Value, "prefer-ipv6")
177+
if err != nil {
186178
c.Globals.ErrLog.Add(err)
187179
return err
188180
}
189-
190-
input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(preferIPv6))
181+
input.PreferIPv6 = fastly.ToPointer(fastly.Compatibool(*preferIPv6))
191182
}
192183

193184
if c.ConnectTimeout.WasSet {
@@ -272,20 +263,12 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
272263
}
273264

274265
if c.TCPKaEnable.WasSet {
275-
var tcpKaEnable bool
276-
277-
switch c.TCPKaEnable.Value {
278-
case "true":
279-
tcpKaEnable = true
280-
case "false":
281-
tcpKaEnable = false
282-
default:
283-
err := errors.New("'tcp-ka-enabled' flag must be one of the following [true, false]")
266+
tcpKaEnable, err := argparser.ConvertBoolFromStringFlag(c.TCPKaEnable.Value, "tcp-ka-enabled")
267+
if err != nil {
284268
c.Globals.ErrLog.Add(err)
285269
return err
286270
}
287-
288-
input.TCPKeepAliveEnable = &tcpKaEnable
271+
input.TCPKeepAliveEnable = tcpKaEnable
289272
}
290273
if c.TCPKaInterval.WasSet {
291274
input.TCPKeepAliveIntvl = &c.TCPKaInterval.Value

pkg/commands/dashboard/list.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dashboard
22

33
import (
44
"context"
5-
"errors"
65
"io"
76

87
"github.com/fastly/go-fastly/v12/fastly"
@@ -121,13 +120,10 @@ func (c *ListCommand) constructInput() (*fastly.ListObservabilityCustomDashboard
121120
input.Limit = &c.limit.Value
122121
}
123122
var sign string
123+
var err error
124124
if c.order.WasSet {
125-
switch c.order.Value {
126-
case "asc":
127-
case "desc":
128-
sign = "-"
129-
default:
130-
err := errors.New("'order' flag must be one of the following [asc, desc]")
125+
sign, err = argparser.ConvertOrderFromStringFlag(c.order.Value, "order")
126+
if err != nil {
131127
c.Globals.ErrLog.Add(err)
132128
return nil, err
133129
}

pkg/commands/imageoptimizerdefaults/update.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,12 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
159159
}
160160
}
161161
if c.webp.WasSet {
162-
var webp bool
163-
switch c.webp.Value {
164-
case "true":
165-
webp = true
166-
case "false":
167-
webp = false
168-
default:
169-
return fmt.Errorf("'webp' flag must be one of the following [true, false]")
162+
webp, err := argparser.ConvertBoolFromStringFlag(c.webp.Value, "webp")
163+
if err != nil {
164+
c.Globals.ErrLog.Add(err)
165+
return err
170166
}
171-
c.Input.Webp = &webp
167+
c.Input.Webp = webp
172168
}
173169
if c.webpQuality.WasSet {
174170
c.Input.WebpQuality = &c.webpQuality.Value
@@ -193,28 +189,20 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
193189
c.Input.JpegQuality = &c.jpegQuality.Value
194190
}
195191
if c.upscale.WasSet {
196-
var upscale bool
197-
switch c.upscale.Value {
198-
case "true":
199-
upscale = true
200-
case "false":
201-
upscale = false
202-
default:
203-
return fmt.Errorf("'upscale' flag must be one of the following [true, false]")
192+
upscale, err := argparser.ConvertBoolFromStringFlag(c.upscale.Value, "upscale")
193+
if err != nil {
194+
c.Globals.ErrLog.Add(err)
195+
return err
204196
}
205-
c.Input.Upscale = &upscale
197+
c.Input.Upscale = upscale
206198
}
207199
if c.allowVideo.WasSet {
208-
var allowVideo bool
209-
switch c.allowVideo.Value {
210-
case "true":
211-
allowVideo = true
212-
case "false":
213-
allowVideo = false
214-
default:
215-
return fmt.Errorf("'allow-video' flag must be one of the following [true, false]")
200+
allowVideo, err := argparser.ConvertBoolFromStringFlag(c.allowVideo.Value, "allow-video")
201+
if err != nil {
202+
c.Globals.ErrLog.Add(err)
203+
return err
216204
}
217-
c.Input.AllowVideo = &allowVideo
205+
c.Input.AllowVideo = allowVideo
218206
}
219207

220208
o, err := c.Globals.APIClient.UpdateImageOptimizerDefaultSettings(context.TODO(), &c.Input)

pkg/commands/ngwaf/workspace/threshold/create.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,38 +75,26 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
7575
return err
7676
}
7777

78-
var enabled bool
79-
switch c.enabled.Value {
80-
case "true":
81-
enabled = true
82-
case "false":
83-
enabled = false
84-
default:
85-
err := errors.New("'enabled' flag must be one of the following [true, false]")
78+
enabled, err := argparser.ConvertBoolFromStringFlag(c.enabled.Value, "enabled")
79+
if err != nil {
8680
c.Globals.ErrLog.Add(err)
8781
return err
8882
}
8983

90-
var dontNotify bool
91-
switch c.dontNotify.Value {
92-
case "true":
93-
dontNotify = true
94-
case "false":
95-
dontNotify = false
96-
default:
97-
err := errors.New("'do-not-notify' flag must be one of the following [true, false]")
84+
dontNotify, err := argparser.ConvertBoolFromStringFlag(c.dontNotify.Value, "do-not-notify")
85+
if err != nil {
9886
c.Globals.ErrLog.Add(err)
9987
return err
10088
}
10189

10290
input := &thresholds.CreateInput{
10391
Action: &c.action,
10492
Duration: &c.duration,
105-
Enabled: &enabled,
93+
Enabled: enabled,
10694
Interval: &c.interval,
10795
Limit: &c.limit,
10896
Name: &c.name,
109-
DontNotify: &dontNotify,
97+
DontNotify: dontNotify,
11098
Signal: &c.signal,
11199
WorkspaceID: &c.workspaceID.Value,
112100
}

pkg/commands/ngwaf/workspace/threshold/update.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,23 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
8484
input.Action = &c.action.Value
8585
}
8686
if c.dontNotify.WasSet {
87-
var dontNotify bool
88-
switch c.dontNotify.Value {
89-
case "true":
90-
dontNotify = true
91-
case "false":
92-
dontNotify = false
93-
default:
94-
err := errors.New("'do-not-notify' flag must be one of the following [true, false]")
87+
dontNotify, err := argparser.ConvertBoolFromStringFlag(c.dontNotify.Value, "do-not-notify")
88+
if err != nil {
9589
c.Globals.ErrLog.Add(err)
9690
return err
9791
}
98-
input.DontNotify = &dontNotify
92+
input.DontNotify = dontNotify
9993
}
10094
if c.duration.WasSet {
10195
input.Duration = &c.duration.Value
10296
}
10397
if c.enabled.WasSet {
104-
var enabled bool
105-
switch c.enabled.Value {
106-
case "true":
107-
enabled = true
108-
case "false":
109-
enabled = false
110-
default:
111-
err := errors.New("'enabled' flag must be one of the following [true, false]")
98+
enabled, err := argparser.ConvertBoolFromStringFlag(c.enabled.Value, "enabled")
99+
if err != nil {
112100
c.Globals.ErrLog.Add(err)
113101
return err
114102
}
115-
input.Enabled = &enabled
103+
input.Enabled = enabled
116104
}
117105
if c.interval.WasSet {
118106
input.Interval = &c.interval.Value

0 commit comments

Comments
 (0)