Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: consistent --json implementation #912

Merged
merged 2 commits into from
Apr 18, 2023
Merged
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
33 changes: 9 additions & 24 deletions pkg/commands/acl/describe.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package acl

import (
"encoding/json"
"fmt"
"io"

Expand Down Expand Up @@ -32,12 +31,7 @@ func NewDescribeCommand(parent cmd.Registerer, g *global.Data, m manifest.Data)
})

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Name: cmd.FlagJSONName,
Description: cmd.FlagJSONDesc,
Dst: &c.json,
Short: 'j',
})
c.RegisterFlagBool(c.JSONFlag()) // --json
c.RegisterFlag(cmd.StringFlagOpts{
Name: cmd.FlagServiceIDName,
Description: cmd.FlagServiceIDDesc,
Expand All @@ -57,8 +51,8 @@ func NewDescribeCommand(parent cmd.Registerer, g *global.Data, m manifest.Data)
// DescribeCommand calls the Fastly API to describe an appropriate resource.
type DescribeCommand struct {
cmd.Base
cmd.JSONOutput

json bool
manifest manifest.Data
name string
serviceName cmd.OptionalServiceNameID
Expand All @@ -67,7 +61,7 @@ type DescribeCommand struct {

// Exec invokes the application logic for the command.
func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {
if c.Globals.Verbose() && c.json {
if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

Expand All @@ -90,7 +84,7 @@ func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {

input := c.constructInput(serviceID, serviceVersion.Number)

a, err := c.Globals.APIClient.GetACL(input)
o, err := c.Globals.APIClient.GetACL(input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
Expand All @@ -99,7 +93,11 @@ func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {
return err
}

return c.print(out, a)
if ok, err := c.WriteJSON(out, o); ok {
return err
}

return c.print(out, o)
}

// constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
Expand All @@ -115,19 +113,6 @@ func (c *DescribeCommand) constructInput(serviceID string, serviceVersion int) *

// print displays the information returned from the API.
func (c *DescribeCommand) print(out io.Writer, a *fastly.ACL) error {
if c.json {
data, err := json.Marshal(a)
if err != nil {
return err
}
_, err = out.Write(data)
if err != nil {
c.Globals.ErrLog.Add(err)
return fmt.Errorf("error: unable to write data to stdout: %w", err)
}
return nil
}

if !c.Globals.Verbose() {
fmt.Fprintf(out, "\nService ID: %s\n", a.ServiceID)
}
Expand Down
35 changes: 10 additions & 25 deletions pkg/commands/acl/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package acl

import (
"encoding/json"
"fmt"
"io"

Expand Down Expand Up @@ -32,12 +31,7 @@ func NewListCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *Lis
})

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Name: cmd.FlagJSONName,
Description: cmd.FlagJSONDesc,
Dst: &c.json,
Short: 'j',
})
c.RegisterFlagBool(c.JSONFlag()) // --json
c.RegisterFlag(cmd.StringFlagOpts{
Name: cmd.FlagServiceIDName,
Description: cmd.FlagServiceIDDesc,
Expand All @@ -57,16 +51,16 @@ func NewListCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *Lis
// ListCommand calls the Fastly API to list appropriate resources.
type ListCommand struct {
cmd.Base
cmd.JSONOutput

json bool
manifest manifest.Data
serviceName cmd.OptionalServiceNameID
serviceVersion cmd.OptionalServiceVersion
}

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
if c.Globals.Verbose() && c.json {
if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

Expand All @@ -89,7 +83,7 @@ func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {

input := c.constructInput(serviceID, serviceVersion.Number)

as, err := c.Globals.APIClient.ListACLs(input)
o, err := c.Globals.APIClient.ListACLs(input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
Expand All @@ -98,10 +92,14 @@ func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
return err
}

if ok, err := c.WriteJSON(out, o); ok {
return err
}

if c.Globals.Verbose() {
c.printVerbose(out, serviceVersion.Number, as)
c.printVerbose(out, serviceVersion.Number, o)
} else {
err = c.printSummary(out, as)
err = c.printSummary(out, o)
if err != nil {
return err
}
Expand Down Expand Up @@ -145,19 +143,6 @@ func (c *ListCommand) printVerbose(out io.Writer, serviceVersion int, as []*fast
// printSummary displays the information returned from the API in a summarised
// format.
func (c *ListCommand) printSummary(out io.Writer, as []*fastly.ACL) error {
if c.json {
data, err := json.Marshal(as)
if err != nil {
return err
}
_, err = out.Write(data)
if err != nil {
c.Globals.ErrLog.Add(err)
return fmt.Errorf("error: unable to write data to stdout: %w", err)
}
return nil
}

t := text.NewTable(out)
t.AddHeader("SERVICE ID", "VERSION", "NAME", "ID")
for _, a := range as {
Expand Down
33 changes: 9 additions & 24 deletions pkg/commands/aclentry/describe.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aclentry

import (
"encoding/json"
"fmt"
"io"

Expand All @@ -27,12 +26,7 @@ func NewDescribeCommand(parent cmd.Registerer, g *global.Data, m manifest.Data)
c.CmdClause.Flag("id", "Alphanumeric string identifying an ACL Entry").Required().StringVar(&c.id)

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Name: cmd.FlagJSONName,
Description: cmd.FlagJSONDesc,
Dst: &c.json,
Short: 'j',
})
c.RegisterFlagBool(c.JSONFlag()) // --json
c.RegisterFlag(cmd.StringFlagOpts{
Name: cmd.FlagServiceIDName,
Description: cmd.FlagServiceIDDesc,
Expand All @@ -52,17 +46,17 @@ func NewDescribeCommand(parent cmd.Registerer, g *global.Data, m manifest.Data)
// DescribeCommand calls the Fastly API to describe an appropriate resource.
type DescribeCommand struct {
cmd.Base
cmd.JSONOutput

aclID string
id string
json bool
manifest manifest.Data
serviceName cmd.OptionalServiceNameID
}

// Exec invokes the application logic for the command.
func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {
if c.Globals.Verbose() && c.json {
if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

Expand All @@ -76,15 +70,19 @@ func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {

input := c.constructInput(serviceID)

a, err := c.Globals.APIClient.GetACLEntry(input)
o, err := c.Globals.APIClient.GetACLEntry(input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
})
return err
}

return c.print(out, a)
if ok, err := c.WriteJSON(out, o); ok {
return err
}

return c.print(out, o)
}

// constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
Expand All @@ -100,19 +98,6 @@ func (c *DescribeCommand) constructInput(serviceID string) *fastly.GetACLEntryIn

// print displays the information returned from the API.
func (c *DescribeCommand) print(out io.Writer, a *fastly.ACLEntry) error {
if c.json {
data, err := json.Marshal(a)
if err != nil {
return err
}
_, err = out.Write(data)
if err != nil {
c.Globals.ErrLog.Add(err)
return fmt.Errorf("error: unable to write data to stdout: %w", err)
}
return nil
}

if !c.Globals.Verbose() {
fmt.Fprintf(out, "\nService ID: %s\n", a.ServiceID)
}
Expand Down
37 changes: 11 additions & 26 deletions pkg/commands/aclentry/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aclentry

import (
"encoding/json"
"fmt"
"io"

Expand All @@ -27,12 +26,7 @@ func NewListCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *Lis
c.CmdClause.Flag("acl-id", "Alphanumeric string identifying a ACL").Required().StringVar(&c.aclID)

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Name: cmd.FlagJSONName,
Description: cmd.FlagJSONDesc,
Dst: &c.json,
Short: 'j',
})
c.RegisterFlagBool(c.JSONFlag()) // --json
c.RegisterFlag(cmd.StringFlagOpts{
Name: cmd.FlagServiceIDName,
Description: cmd.FlagServiceIDDesc,
Expand All @@ -57,10 +51,10 @@ func NewListCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *Lis
// ListCommand calls the Fastly API to list appropriate resources.
type ListCommand struct {
cmd.Base
cmd.JSONOutput

aclID string
direction string
json bool
manifest manifest.Data
page int
perPage int
Expand All @@ -70,7 +64,7 @@ type ListCommand struct {

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
if c.Globals.Verbose() && c.json {
if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

Expand All @@ -87,7 +81,7 @@ func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {

// TODO: Use generics support in go 1.18 to replace this almost identical
// logic inside of 'dictionary-item list' and 'service list'.
var as []*fastly.ACLEntry
var o []*fastly.ACLEntry
for paginator.HasNext() {
data, err := paginator.GetNext()
if err != nil {
Expand All @@ -98,13 +92,17 @@ func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
})
return err
}
as = append(as, data...)
o = append(o, data...)
}

if ok, err := c.WriteJSON(out, o); ok {
return err
}

if c.Globals.Verbose() {
c.printVerbose(out, as)
c.printVerbose(out, o)
} else {
err = c.printSummary(out, as)
err = c.printSummary(out, o)
if err != nil {
return err
}
Expand Down Expand Up @@ -154,19 +152,6 @@ func (c *ListCommand) printVerbose(out io.Writer, as []*fastly.ACLEntry) {
// printSummary displays the information returned from the API in a summarised
// format.
func (c *ListCommand) printSummary(out io.Writer, as []*fastly.ACLEntry) error {
if c.json {
data, err := json.Marshal(as)
if err != nil {
return err
}
_, err = out.Write(data)
if err != nil {
c.Globals.ErrLog.Add(err)
return fmt.Errorf("error: unable to write data to stdout: %w", err)
}
return nil
}

t := text.NewTable(out)
t.AddHeader("SERVICE ID", "ID", "IP", "SUBNET", "NEGATED")
for _, a := range as {
Expand Down
Loading