diff --git a/pkg/api/interface.go b/pkg/api/interface.go index aa381c867..d20c674d3 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -331,6 +331,7 @@ type Interface interface { CreateObjectStore(i *fastly.CreateObjectStoreInput) (*fastly.ObjectStore, error) ListObjectStores(i *fastly.ListObjectStoresInput) (*fastly.ListObjectStoresResponse, error) DeleteObjectStore(i *fastly.DeleteObjectStoreInput) error + GetObjectStore(i *fastly.GetObjectStoreInput) (*fastly.ObjectStore, error) ListObjectStoreKeys(i *fastly.ListObjectStoreKeysInput) (*fastly.ListObjectStoreKeysResponse, error) GetObjectStoreKey(i *fastly.GetObjectStoreKeyInput) (string, error) DeleteObjectStoreKey(i *fastly.DeleteObjectStoreKeyInput) error diff --git a/pkg/app/commands.go b/pkg/app/commands.go index 0f5676219..68d487ede 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -300,6 +300,7 @@ func defineCommands( objectstoreCmdRoot := objectstore.NewRootCommand(app, globals) objectstoreCreate := objectstore.NewCreateStoreCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreDelete := objectstore.NewDeleteStoreCommand(objectstoreCmdRoot.CmdClause, globals, data) + objectstoreDescribe := objectstore.NewDescribeStoreCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreList := objectstore.NewListStoreCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreKeysCmdRoot := objectstore.NewKeysRootCommand(app, globals) objectstoreDeleteKey := objectstore.NewDeleteKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) @@ -631,6 +632,7 @@ func defineCommands( objectstoreCreate, objectstoreList, objectstoreDelete, + objectstoreDescribe, objectstoreListKeys, objectstoreInsertKey, objectstoreGetKey, diff --git a/pkg/commands/objectstore/describestore.go b/pkg/commands/objectstore/describestore.go new file mode 100644 index 000000000..5cb4dc97d --- /dev/null +++ b/pkg/commands/objectstore/describestore.go @@ -0,0 +1,73 @@ +package objectstore + +import ( + "encoding/json" + "fmt" + "io" + + "github.com/fastly/cli/pkg/cmd" + "github.com/fastly/cli/pkg/config" + fsterr "github.com/fastly/cli/pkg/errors" + "github.com/fastly/cli/pkg/manifest" + "github.com/fastly/cli/pkg/text" + "github.com/fastly/go-fastly/v7/fastly" +) + +// DescribeStoreCommand calls the Fastly API to fetch the value of a key from an object store. +type DescribeStoreCommand struct { + cmd.Base + json bool + manifest manifest.Data + Input fastly.GetObjectStoreInput +} + +// NewDescribeStoreCommand returns a usable command registered under the parent. +func NewDescribeStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DescribeStoreCommand { + c := DescribeStoreCommand{ + Base: cmd.Base{ + Globals: globals, + }, + manifest: data, + } + c.CmdClause = parent.Command("describe", "Describe an object store").Alias("get") + c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID) + + // optional + c.RegisterFlagBool(cmd.BoolFlagOpts{ + Name: cmd.FlagJSONName, + Description: cmd.FlagJSONDesc, + Dst: &c.json, + Short: 'j', + }) + + return &c +} + +// Exec invokes the application logic for the command. +func (c *DescribeStoreCommand) Exec(_ io.Reader, out io.Writer) error { + if c.Globals.Verbose() && c.json { + return fsterr.ErrInvalidVerboseJSONCombo + } + + o, err := c.Globals.APIClient.GetObjectStore(&c.Input) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + + if c.json { + data, err := json.Marshal(o) + 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 + } + + text.PrintObjectStore(out, "", o) + return nil +}