Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
37 changes: 19 additions & 18 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/fastly/cli/pkg/commands/profile"
"github.com/fastly/cli/pkg/commands/purge"
"github.com/fastly/cli/pkg/commands/secretstore"
"github.com/fastly/cli/pkg/commands/secretstoreentry"
"github.com/fastly/cli/pkg/commands/service"
"github.com/fastly/cli/pkg/commands/serviceauth"
"github.com/fastly/cli/pkg/commands/serviceversion"
Expand Down Expand Up @@ -317,16 +318,16 @@ func defineCommands(
profileToken := profile.NewTokenCommand(profileCmdRoot.CmdClause, g)
profileUpdate := profile.NewUpdateCommand(profileCmdRoot.CmdClause, profile.APIClientFactory(opts.APIClient), g)
purgeCmdRoot := purge.NewRootCommand(app, g, m)
secretstoreCmdRoot := secretstore.NewStoreRootCommand(app, g)
secretstoreCreateStore := secretstore.NewCreateStoreCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreGetStore := secretstore.NewDescribeStoreCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreDeleteStore := secretstore.NewDeleteStoreCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreListStores := secretstore.NewListStoresCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreSecretCmdRoot := secretstore.NewSecretRootCommand(app, g)
secretstoreCreateSecret := secretstore.NewCreateSecretCommand(secretstoreSecretCmdRoot.CmdClause, g, m)
secretstoreGetSecret := secretstore.NewDescribeSecretCommand(secretstoreSecretCmdRoot.CmdClause, g, m)
secretstoreDeleteSecret := secretstore.NewDeleteSecretCommand(secretstoreSecretCmdRoot.CmdClause, g, m)
secretstoreListSecrets := secretstore.NewListSecretsCommand(secretstoreSecretCmdRoot.CmdClause, g, m)
secretstoreCmdRoot := secretstore.NewRootCommand(app, g)
secretstoreCreate := secretstore.NewCreateCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreDescribe := secretstore.NewDescribeCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreDelete := secretstore.NewDeleteCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreList := secretstore.NewListCommand(secretstoreCmdRoot.CmdClause, g, m)
secretstoreentryCmdRoot := secretstoreentry.NewRootCommand(app, g)
secretstoreentryCreate := secretstoreentry.NewCreateCommand(secretstoreentryCmdRoot.CmdClause, g, m)
secretstoreentryDescribe := secretstoreentry.NewDescribeCommand(secretstoreentryCmdRoot.CmdClause, g, m)
secretstoreentryDelete := secretstoreentry.NewDeleteCommand(secretstoreentryCmdRoot.CmdClause, g, m)
secretstoreentryList := secretstoreentry.NewListCommand(secretstoreentryCmdRoot.CmdClause, g, m)
serviceCmdRoot := service.NewRootCommand(app, g)
serviceCreate := service.NewCreateCommand(serviceCmdRoot.CmdClause, g)
serviceDelete := service.NewDeleteCommand(serviceCmdRoot.CmdClause, g, m)
Expand Down Expand Up @@ -647,14 +648,14 @@ func defineCommands(
profileToken,
profileUpdate,
purgeCmdRoot,
secretstoreCreateStore,
secretstoreGetStore,
secretstoreDeleteStore,
secretstoreListStores,
secretstoreCreateSecret,
secretstoreGetSecret,
secretstoreDeleteSecret,
secretstoreListSecrets,
secretstoreCreate,
secretstoreDescribe,
secretstoreDelete,
secretstoreList,
secretstoreentryCreate,
secretstoreentryDescribe,
secretstoreentryDelete,
secretstoreentryList,
serviceCmdRoot,
serviceCreate,
serviceDelete,
Expand Down
29 changes: 29 additions & 0 deletions pkg/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,32 @@ var (

// PaginationDirection is a list of directions the page results can be displayed.
var PaginationDirection = []string{"ascend", "descend"}

func CursorFlag(dst *string) StringFlagOpts {
return StringFlagOpts{
Name: "cursor",
Short: 'c',
Description: "Pagination cursor (Use 'next_cursor' value from list output)",
Dst: dst,
}
}

func LimitFlag(dst *int) IntFlagOpts {
return IntFlagOpts{
Name: "limit",
Short: 'l',
Description: "Maximum number of items to list",
Default: 50,
Dst: dst,
}
}

func StoreIDFlag(dst *string) StringFlagOpts {
return StringFlagOpts{
Name: "store-id",
Short: 's',
Description: "Store ID",
Dst: dst,
Required: true,
}
}
29 changes: 29 additions & 0 deletions pkg/cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -317,3 +318,31 @@ func IsCompletion(args []string) bool {
}
return found
}

// JSONOutput is a helper for adding a `--json` flag and encoding
// values to JSON. It can be embedded into command structs.
type JSONOutput struct {
Enabled bool // Set via flag.
}

// JSONFlag creates a flag for enabling JSON output.
func (j *JSONOutput) JSONFlag() BoolFlagOpts {
return BoolFlagOpts{
Name: FlagJSONName,
Description: FlagJSONDesc,
Dst: &j.Enabled,
Short: 'j',
}
}

// WriteJSON checks whether the enabled flag is set or not. If set,
// then the given value is written as JSON to out. Otherwise, false is returned.
func (j *JSONOutput) WriteJSON(out io.Writer, value any) (bool, error) {
if !j.Enabled {
return false, nil
}

enc := json.NewEncoder(out)
enc.SetIndent("", " ")
return true, enc.Encode(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/fastly/go-fastly/v7/fastly"
)

// NewCreateStoreCommand returns a usable command registered under the parent.
func NewCreateStoreCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *CreateStoreCommand {
c := CreateStoreCommand{
// NewCreateCommand returns a usable command registered under the parent.
func NewCreateCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *CreateCommand {
c := CreateCommand{
Base: cmd.Base{
Globals: g,
},
Expand All @@ -26,23 +26,23 @@ func NewCreateStoreCommand(parent cmd.Registerer, g *global.Data, m manifest.Dat
c.RegisterFlag(storeNameFlag(&c.Input.Name)) // --name

// Optional.
c.RegisterFlagBool(c.jsonFlag()) // --json
c.RegisterFlagBool(c.JSONFlag()) // --json

return &c
}

// CreateStoreCommand calls the Fastly API to create an appropriate resource.
type CreateStoreCommand struct {
// CreateCommand calls the Fastly API to create an appropriate resource.
type CreateCommand struct {
cmd.Base
jsonOutput
cmd.JSONOutput

Input fastly.CreateSecretStoreInput
manifest manifest.Data
}

// Exec invokes the application logic for the command.
func (cmd *CreateStoreCommand) Exec(_ io.Reader, out io.Writer) error {
if cmd.Globals.Verbose() && cmd.jsonOutput.enabled {
func (cmd *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/fastly/go-fastly/v7/fastly"
)

// NewDeleteStoreCommand returns a usable command registered under the parent.
func NewDeleteStoreCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *DeleteStoreCommand {
c := DeleteStoreCommand{
// NewDeleteCommand returns a usable command registered under the parent.
func NewDeleteCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *DeleteCommand {
c := DeleteCommand{
Base: cmd.Base{
Globals: g,
},
Expand All @@ -23,26 +23,26 @@ func NewDeleteStoreCommand(parent cmd.Registerer, g *global.Data, m manifest.Dat
c.CmdClause = parent.Command("delete", "Delete a secret store")

// Required.
c.RegisterFlag(storeIDFlag(&c.Input.ID)) // --store-id
c.RegisterFlag(cmd.StoreIDFlag(&c.Input.ID)) // --store-id

// Optional.
c.RegisterFlagBool(c.jsonFlag()) // --json
c.RegisterFlagBool(c.JSONFlag()) // --json

return &c
}

// DeleteStoreCommand calls the Fastly API to delete an appropriate resource.
type DeleteStoreCommand struct {
// DeleteCommand calls the Fastly API to delete an appropriate resource.
type DeleteCommand struct {
cmd.Base
jsonOutput
cmd.JSONOutput

Input fastly.DeleteSecretStoreInput
manifest manifest.Data
}

// Exec invokes the application logic for the command.
func (cmd *DeleteStoreCommand) Exec(_ io.Reader, out io.Writer) error {
if cmd.Globals.Verbose() && cmd.jsonOutput.enabled {
func (cmd *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

Expand All @@ -52,7 +52,7 @@ func (cmd *DeleteStoreCommand) Exec(_ io.Reader, out io.Writer) error {
return err
}

if cmd.jsonOutput.enabled {
if cmd.JSONOutput.Enabled {
o := struct {
ID string `json:"id"`
Deleted bool `json:"deleted"`
Expand Down
73 changes: 0 additions & 73 deletions pkg/commands/secretstore/deletesecret.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/fastly/go-fastly/v7/fastly"
)

// NewDescribeStoreCommand returns a usable command registered under the parent.
func NewDescribeStoreCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *DescribeStoreCommand {
c := DescribeStoreCommand{
// NewDescribeCommand returns a usable command registered under the parent.
func NewDescribeCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *DescribeCommand {
c := DescribeCommand{
Base: cmd.Base{
Globals: g,
},
Expand All @@ -23,26 +23,26 @@ func NewDescribeStoreCommand(parent cmd.Registerer, g *global.Data, m manifest.D
c.CmdClause = parent.Command("describe", "Retrieve a single secret store").Alias("get")

// Required.
c.RegisterFlag(storeIDFlag(&c.Input.ID)) // --store-id
c.RegisterFlag(cmd.StoreIDFlag(&c.Input.ID)) // --store-id

// Optional.
c.RegisterFlagBool(c.jsonFlag()) // --json
c.RegisterFlagBool(c.JSONFlag()) // --json

return &c
}

// DescribeStoreCommand calls the Fastly API to describe an appropriate resource.
type DescribeStoreCommand struct {
// DescribeCommand calls the Fastly API to describe an appropriate resource.
type DescribeCommand struct {
cmd.Base
jsonOutput
cmd.JSONOutput

Input fastly.GetSecretStoreInput
manifest manifest.Data
}

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

Expand Down
Loading