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

Rework objectstore subcommand #792

Merged
merged 16 commits into from
Feb 6, 2023
2 changes: 2 additions & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ 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
InsertObjectStoreKey(i *fastly.InsertObjectStoreKeyInput) error

CreateSecretStore(i *fastly.CreateSecretStoreInput) (*fastly.SecretStore, error)
Expand Down
16 changes: 11 additions & 5 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/fastly/cli/pkg/commands/logging/syslog"
"github.com/fastly/cli/pkg/commands/logtail"
"github.com/fastly/cli/pkg/commands/objectstore"
"github.com/fastly/cli/pkg/commands/objectstorekeys"
"github.com/fastly/cli/pkg/commands/pop"
"github.com/fastly/cli/pkg/commands/profile"
"github.com/fastly/cli/pkg/commands/purge"
Expand Down Expand Up @@ -299,11 +300,14 @@ func defineCommands(
loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.CmdClause, globals, data)
objectstoreCmdRoot := objectstore.NewRootCommand(app, globals)
objectstoreCreate := objectstore.NewCreateCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreDelete := objectstore.NewDeleteCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreKeys := objectstore.NewKeysCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreDescribe := objectstore.NewDescribeCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreKeysCmdRoot := objectstorekeys.NewRootCommand(app, globals)
objectstoreDeleteKey := objectstorekeys.NewDeleteCommand(objectstoreKeysCmdRoot.CmdClause, globals, data)
objectstoreGetKey := objectstorekeys.NewGetCommand(objectstoreKeysCmdRoot.CmdClause, globals, data)
objectstoreInsertKey := objectstorekeys.NewInsertCommand(objectstoreKeysCmdRoot.CmdClause, globals, data)
objectstoreListKeys := objectstorekeys.NewListCommand(objectstoreKeysCmdRoot.CmdClause, globals, data)
popCmdRoot := pop.NewRootCommand(app, globals)
profileCmdRoot := profile.NewRootCommand(app, globals)
profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, profile.APIClientFactory(opts.APIClient), globals)
Expand Down Expand Up @@ -629,9 +633,11 @@ func defineCommands(
objectstoreCreate,
objectstoreList,
objectstoreDelete,
objectstoreKeys,
objectstoreDescribe,
objectstoreListKeys,
objectstoreInsertKey,
objectstoreGetKey,
objectstoreDeleteKey,
popCmdRoot,
profileCmdRoot,
profileCreate,
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ healthcheck
ip-list
log-tail
logging
objectstore
object-store
object-store-keys
pops
profile
purge
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/objectstore/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest
},
manifest: data,
}
c.CmdClause = parent.Command("create", "Create a Fastly object store")
c.CmdClause = parent.Command("create", "Create an object store")
c.CmdClause.Flag("name", "Name of Object Store").Short('n').Required().StringVar(&c.Input.Name)
return &c
}
Expand All @@ -38,6 +38,6 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
return err
}

text.Success(out, "Created object store %s", d.Name)
text.Success(out, "Created object store %s (name %s)", d.ID, d.Name)
return nil
}
6 changes: 3 additions & 3 deletions pkg/commands/objectstore/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest
},
manifest: data,
}
c.CmdClause = parent.Command("delete", "Delete a Fastly object store")
c.CmdClause.Flag("id", "ID of object store").Required().StringVar(&c.Input.ID)
c.CmdClause = parent.Command("delete", "Delete an object store")
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)
return &c
}

Expand All @@ -38,6 +38,6 @@ func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
return err
}

text.Success(out, "Deleted object store ID:%s", c.Input.ID)
text.Success(out, "Deleted object store ID %s", c.Input.ID)
return nil
}
73 changes: 73 additions & 0 deletions pkg/commands/objectstore/describe.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"
)

// DescribeCommand calls the Fastly API to fetch the value of a key from an object store.
type DescribeCommand struct {
cmd.Base
json bool
manifest manifest.Data
Input fastly.GetObjectStoreInput
}

// NewDescribeCommand returns a usable command registered under the parent.
func NewDescribeCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DescribeCommand {
c := DescribeCommand{
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 *DescribeCommand) 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
}
43 changes: 0 additions & 43 deletions pkg/commands/objectstore/insert.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.D
},
manifest: data,
}
c.CmdClause = parent.Command("list", "List Fastly object stores")
c.CmdClause = parent.Command("list", "List object stores")
fgsch marked this conversation as resolved.
Show resolved Hide resolved

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/objectstore/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RootCommand struct {
func NewRootCommand(parent cmd.Registerer, globals *config.Data) *RootCommand {
var c RootCommand
c.Globals = globals
c.CmdClause = parent.Command("objectstore", "Manipulate Fastly object stores")
c.CmdClause = parent.Command("object-store", "Manipulate Fastly Object Stores")
return &c
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/commands/objectstorekeys/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package objectstorekeys

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
)

// DeleteCommand calls the Fastly API to delete an object store.
type DeleteCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.DeleteObjectStoreKeyInput
}

// NewDeleteCommand returns a usable command registered under the parent.
func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteCommand {
c := DeleteCommand{
Base: cmd.Base{
Globals: globals,
},
manifest: data,
}
c.CmdClause = parent.Command("delete", "Delete a key")
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("key-name", "Key name").Short('k').Required().StringVar(&c.Input.Key)
return &c
}

// Exec invokes the application logic for the command.
func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
err := c.Globals.APIClient.DeleteObjectStoreKey(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.Success(out, "Deleted key %s from store ID %s", c.Input.Key, c.Input.ID)
return nil
}
3 changes: 3 additions & 0 deletions pkg/commands/objectstorekeys/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package objectstorekeys contains commands to inspect and manipulate Fastly edge
// object stores keys.
package objectstorekeys
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package objectstore
package objectstorekeys

import (
"io"
Expand All @@ -11,24 +11,25 @@ import (
"github.com/fastly/go-fastly/v7/fastly"
)

// GetKeyCommand calls the Fastly API to fetch the value of a key from an object store.
type GetKeyCommand struct {
// GetCommand calls the Fastly API to fetch the value of a key from an object store.
type GetCommand struct {
cmd.Base
json bool
manifest manifest.Data
Input fastly.GetObjectStoreKeyInput
}

// NewGetKeyCommand returns a usable command registered under the parent.
func NewGetKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *GetKeyCommand {
var c GetKeyCommand
c.Globals = globals
c.manifest = data
c.CmdClause = parent.Command("get", "Get Fastly object store key")

// required
c.CmdClause.Flag("id", "ID of object store").Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("key", "Key to fetch").Short('k').Required().StringVar(&c.Input.Key)
// NewGetCommand returns a usable command registered under the parent.
func NewGetCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *GetCommand {
c := GetCommand{
Base: cmd.Base{
Globals: globals,
},
manifest: data,
}
c.CmdClause = parent.Command("get", "Get the value associated with a key")
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("key-name", "Key name").Short('k').Required().StringVar(&c.Input.Key)

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Expand All @@ -42,7 +43,7 @@ func NewGetKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest
}

// Exec invokes the application logic for the command.
func (c *GetKeyCommand) Exec(_ io.Reader, out io.Writer) error {
func (c *GetCommand) Exec(_ io.Reader, out io.Writer) error {
if c.Globals.Verbose() && c.json {
return fsterr.ErrInvalidVerboseJSONCombo
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/commands/objectstorekeys/insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package objectstorekeys
fgsch marked this conversation as resolved.
Show resolved Hide resolved

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
)

// InsertCommand calls the Fastly API to insert a key into an object store.
fgsch marked this conversation as resolved.
Show resolved Hide resolved
type InsertCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.InsertObjectStoreKeyInput
}

// NewInsertCommand returns a usable command registered under the parent.
func NewInsertCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *InsertCommand {
c := InsertCommand{
Base: cmd.Base{
Globals: globals,
},
manifest: data,
}
c.CmdClause = parent.Command("insert", "Insert a key-value pair")
fgsch marked this conversation as resolved.
Show resolved Hide resolved
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("key-name", "Key name").Short('k').Required().StringVar(&c.Input.Key)
c.CmdClause.Flag("value", "Value").Required().StringVar(&c.Input.Value)
return &c
}

// Exec invokes the application logic for the command.
func (c *InsertCommand) Exec(_ io.Reader, out io.Writer) error {
err := c.Globals.APIClient.InsertObjectStoreKey(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.Success(out, "Inserted key %s into object store %s", c.Input.Key, c.Input.ID)
return nil
}
Loading