Skip to content

Commit

Permalink
Add describe subcommand under object-store
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsch committed Feb 6, 2023
1 parent fa99e7c commit 8856b3e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -631,6 +632,7 @@ func defineCommands(
objectstoreCreate,
objectstoreList,
objectstoreDelete,
objectstoreDescribe,
objectstoreListKeys,
objectstoreInsertKey,
objectstoreGetKey,
Expand Down
73 changes: 73 additions & 0 deletions pkg/commands/objectstore/describestore.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8856b3e

Please sign in to comment.