Skip to content

Commit

Permalink
feat(product_enablement): support json output
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Oct 9, 2023
1 parent 27901b8 commit 8ce9a2e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
41 changes: 40 additions & 1 deletion pkg/commands/products/products_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestProductEnablement(t *testing.T) {
WantError: "failed to identify Service ID: error reading service: no service ID found",
},
{
Name: "validate invalid flag combo",
Name: "validate invalid enable/disable flag combo",
Args: args("products --enable fanout --disable fanout"),
WantError: "invalid flag combination: --enable and --disable",
},
Expand Down Expand Up @@ -88,6 +88,45 @@ Web Sockets true
Args: args("products --service-id 123 --disable brotli_compression"),
WantOutput: "SUCCESS: Successfully disabled product 'brotli_compression'",
},
{
Name: "validate invalid json/verbose flag combo",
Args: args("products --service-id 123 --json --verbose"),
WantError: "invalid flag combination, --verbose and --json",
},
{
Name: "validate API error for product status with --json output",
API: mock.API{
GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) {
return nil, testutil.Err
},
},
Args: args("products --service-id 123 --json"),
WantOutput: `{
"brotli_compression": false,
"domain_inspector": false,
"fanout": false,
"image_optimizer": false,
"origin_inspector": false,
"websockets": false
}`,
},
{
Name: "validate API success for product status with --json output",
API: mock.API{
GetProductFn: func(i *fastly.ProductEnablementInput) (*fastly.ProductEnablement, error) {
return nil, nil
},
},
Args: args("products --service-id 123 --json"),
WantOutput: `{
"brotli_compression": true,
"domain_inspector": true,
"fanout": true,
"image_optimizer": true,
"origin_inspector": true,
"websockets": true
}`,
},
}

for testcaseIdx := range scenarios {
Expand Down
31 changes: 28 additions & 3 deletions pkg/commands/products/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
// It should be installed under the primary root command.
type RootCommand struct {
cmd.Base
manifest manifest.Data
cmd.JSONOutput

// Optional.
disableProduct string
enableProduct string
manifest manifest.Data
serviceName cmd.OptionalServiceNameID
}

Expand All @@ -41,8 +41,11 @@ func NewRootCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *Roo
c.Globals = g
c.manifest = m
c.CmdClause = parent.Command("products", "Enable, disable, and check the enablement status of products on your services")
c.CmdClause.Flag("enable", "Enable product").HintOptions(ProductEnablementOptions...).EnumVar(&c.enableProduct, ProductEnablementOptions...)

// Optional.
c.CmdClause.Flag("disable", "Disable product").HintOptions(ProductEnablementOptions...).EnumVar(&c.disableProduct, ProductEnablementOptions...)
c.CmdClause.Flag("enable", "Enable product").HintOptions(ProductEnablementOptions...).EnumVar(&c.enableProduct, ProductEnablementOptions...)
c.RegisterFlagBool(c.JSONFlag()) // --json
c.RegisterFlag(cmd.StringFlagOpts{
Name: cmd.FlagServiceIDName,
Description: cmd.FlagServiceIDDesc,
Expand All @@ -66,6 +69,10 @@ func (c *RootCommand) Exec(_ io.Reader, out io.Writer) error {
return fsterr.ErrInvalidProductEnablementFlagCombo
}

if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

serviceID, _, _, err := cmd.ServiceID(c.serviceName, c.manifest, c.Globals.APIClient, c.Globals.ErrLog)
if err != nil {
return fmt.Errorf("failed to identify Service ID: %w", err)
Expand Down Expand Up @@ -132,6 +139,24 @@ func (c *RootCommand) Exec(_ io.Reader, out io.Writer) error {
wsEnabled = true
}

if ok, err := c.WriteJSON(out, struct {
BrotliCompression bool `json:"brotli_compression"`
DomainInspector bool `json:"domain_inspector"`
Fanout bool `json:"fanout"`
ImageOptimizer bool `json:"image_optimizer"`
OriginInspector bool `json:"origin_inspector"`
WebSockets bool `json:"websockets"`
}{
BrotliCompression: brotliEnabled,
DomainInspector: brotliEnabled,
Fanout: brotliEnabled,
ImageOptimizer: brotliEnabled,
OriginInspector: brotliEnabled,
WebSockets: brotliEnabled,
}); ok {
return err
}

t := text.NewTable(out)
t.AddHeader("PRODUCT", "ENABLED")
t.AddLine("Brotli Compression", brotliEnabled)
Expand Down

0 comments on commit 8ce9a2e

Please sign in to comment.