From f591bfd4417b3dfaca548ed7dd6358db656e5587 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Fri, 3 Feb 2023 22:17:42 +0000 Subject: [PATCH 01/16] Rename objectstore to object-store Follows pattern of other commands. --- pkg/commands/objectstore/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/objectstore/root.go b/pkg/commands/objectstore/root.go index afef6f372..b3d4aee4c 100644 --- a/pkg/commands/objectstore/root.go +++ b/pkg/commands/objectstore/root.go @@ -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 } From 7bdcea03f398cf2fcaf172c4ba7aa1dced6b1f0b Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Fri, 3 Feb 2023 22:41:10 +0000 Subject: [PATCH 02/16] Move some commands under object-store-keys This follows existing patterns, the API and is needed for upcoming work. --- pkg/app/commands.go | 7 +++-- pkg/app/run_test.go | 3 +- pkg/commands/objectstore/root_keys.go | 28 +++++++++++++++++++ .../objectstore/{root.go => root_store.go} | 0 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 pkg/commands/objectstore/root_keys.go rename pkg/commands/objectstore/{root.go => root_store.go} (100%) diff --git a/pkg/app/commands.go b/pkg/app/commands.go index 80902060c..3f6cc89cf 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -301,9 +301,10 @@ func defineCommands( 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) + objectstoreKeysCmdRoot := objectstore.NewKeysRootCommand(app, globals) + objectstoreKeys := objectstore.NewKeysCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) popCmdRoot := pop.NewRootCommand(app, globals) profileCmdRoot := profile.NewRootCommand(app, globals) profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, profile.APIClientFactory(opts.APIClient), globals) diff --git a/pkg/app/run_test.go b/pkg/app/run_test.go index cb536549b..5d61dcb48 100644 --- a/pkg/app/run_test.go +++ b/pkg/app/run_test.go @@ -70,7 +70,8 @@ healthcheck ip-list log-tail logging -objectstore +object-store +object-store-keys pops profile purge diff --git a/pkg/commands/objectstore/root_keys.go b/pkg/commands/objectstore/root_keys.go new file mode 100644 index 000000000..6fd89557b --- /dev/null +++ b/pkg/commands/objectstore/root_keys.go @@ -0,0 +1,28 @@ +package objectstore + +import ( + "io" + + "github.com/fastly/cli/pkg/cmd" + "github.com/fastly/cli/pkg/config" +) + +// KeysRootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type KeysRootCommand struct { + cmd.Base + // no flags +} + +// NewKeysRootCommand returns a new command registered in the parent. +func NewKeysRootCommand(parent cmd.Registerer, globals *config.Data) *KeysRootCommand { + var c KeysRootCommand + c.Globals = globals + c.CmdClause = parent.Command("object-store-keys", "Manipulate Fastly Object Store keys") + return &c +} + +// Exec implements the command interface. +func (c *KeysRootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/objectstore/root.go b/pkg/commands/objectstore/root_store.go similarity index 100% rename from pkg/commands/objectstore/root.go rename to pkg/commands/objectstore/root_store.go From a2533978a636c911c4d18889429412519ed0f108 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Fri, 3 Feb 2023 22:49:15 +0000 Subject: [PATCH 03/16] Rename keys to list Now that we have object-store-keys, `list' is more natural. --- pkg/app/commands.go | 4 ++-- pkg/commands/objectstore/keys.go | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/app/commands.go b/pkg/app/commands.go index 3f6cc89cf..9a5fe009e 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -302,7 +302,7 @@ func defineCommands( objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreDelete := objectstore.NewDeleteCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreKeysCmdRoot := objectstore.NewKeysRootCommand(app, globals) - objectstoreKeys := objectstore.NewKeysCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreListKeys := objectstore.NewListKeysCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) popCmdRoot := pop.NewRootCommand(app, globals) @@ -630,7 +630,7 @@ func defineCommands( objectstoreCreate, objectstoreList, objectstoreDelete, - objectstoreKeys, + objectstoreListKeys, objectstoreInsertKey, objectstoreGetKey, popCmdRoot, diff --git a/pkg/commands/objectstore/keys.go b/pkg/commands/objectstore/keys.go index 8c7482a78..c19af4ec4 100644 --- a/pkg/commands/objectstore/keys.go +++ b/pkg/commands/objectstore/keys.go @@ -13,20 +13,20 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// KeysCommand calls the Fastly API to list the keys for a given object store. -type KeysCommand struct { +// ListKeysCommand calls the Fastly API to list the keys for a given object store. +type ListKeysCommand struct { cmd.Base json bool manifest manifest.Data Input fastly.ListObjectStoreKeysInput } -// NewKeysCommand returns a usable command registered under the parent. -func NewKeysCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *KeysCommand { - var c KeysCommand +// NewListKeysCommand returns a usable command registered under the parent. +func NewListKeysCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListKeysCommand { + var c ListKeysCommand c.Globals = globals c.manifest = data - c.CmdClause = parent.Command("keys", "List Fastly object store keys") + c.CmdClause = parent.Command("list", "List Fastly object store keys") // required c.CmdClause.Flag("id", "ID of object store").Required().StringVar(&c.Input.ID) @@ -42,7 +42,7 @@ func NewKeysCommand(parent cmd.Registerer, globals *config.Data, data manifest.D } // Exec invokes the application logic for the command. -func (c *KeysCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *ListKeysCommand) Exec(_ io.Reader, out io.Writer) error { if c.Globals.Verbose() && c.json { return fsterr.ErrInvalidVerboseJSONCombo } From b852dc241c93e16f3297888923aa16955efdb2b9 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 12:01:54 +0000 Subject: [PATCH 04/16] Add subcommand to delete object store keys --- pkg/api/interface.go | 1 + pkg/app/commands.go | 2 ++ pkg/commands/objectstore/deletekey.go | 44 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 pkg/commands/objectstore/deletekey.go diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 3e5315dff..aa381c867 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -333,6 +333,7 @@ type Interface interface { DeleteObjectStore(i *fastly.DeleteObjectStoreInput) 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) diff --git a/pkg/app/commands.go b/pkg/app/commands.go index 9a5fe009e..d25fbaa90 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -305,6 +305,7 @@ func defineCommands( objectstoreListKeys := objectstore.NewListKeysCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreDeleteKey := objectstore.NewDeleteKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) popCmdRoot := pop.NewRootCommand(app, globals) profileCmdRoot := profile.NewRootCommand(app, globals) profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, profile.APIClientFactory(opts.APIClient), globals) @@ -633,6 +634,7 @@ func defineCommands( objectstoreListKeys, objectstoreInsertKey, objectstoreGetKey, + objectstoreDeleteKey, popCmdRoot, profileCmdRoot, profileCreate, diff --git a/pkg/commands/objectstore/deletekey.go b/pkg/commands/objectstore/deletekey.go new file mode 100644 index 000000000..c6cb2098b --- /dev/null +++ b/pkg/commands/objectstore/deletekey.go @@ -0,0 +1,44 @@ +package objectstore + +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" +) + +// DeleteKeyCommand calls the Fastly API to delete an object store. +type DeleteKeyCommand struct { + cmd.Base + manifest manifest.Data + Input fastly.DeleteObjectStoreKeyInput +} + +// NewDeleteKeyCommand returns a usable command registered under the parent. +func NewDeleteKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteKeyCommand { + c := DeleteKeyCommand{ + Base: cmd.Base{ + Globals: globals, + }, + manifest: data, + } + c.CmdClause = parent.Command("delete", "Delete a key") + c.CmdClause.Flag("key-name", "Key name").Short('n').Required().StringVar(&c.Input.Key) + c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID) + return &c +} + +// Exec invokes the application logic for the command. +func (c *DeleteKeyCommand) 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 +} From 4bca9ef3154e1ae66b59602252680b1d9932f52e Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 12:02:29 +0000 Subject: [PATCH 05/16] Rename for consistency --- pkg/commands/objectstore/{delete.go => deletestore.go} | 0 pkg/commands/objectstore/{keys.go => listkeys.go} | 0 pkg/commands/objectstore/{list.go => liststore.go} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename pkg/commands/objectstore/{delete.go => deletestore.go} (100%) rename pkg/commands/objectstore/{keys.go => listkeys.go} (100%) rename pkg/commands/objectstore/{list.go => liststore.go} (100%) diff --git a/pkg/commands/objectstore/delete.go b/pkg/commands/objectstore/deletestore.go similarity index 100% rename from pkg/commands/objectstore/delete.go rename to pkg/commands/objectstore/deletestore.go diff --git a/pkg/commands/objectstore/keys.go b/pkg/commands/objectstore/listkeys.go similarity index 100% rename from pkg/commands/objectstore/keys.go rename to pkg/commands/objectstore/listkeys.go diff --git a/pkg/commands/objectstore/list.go b/pkg/commands/objectstore/liststore.go similarity index 100% rename from pkg/commands/objectstore/list.go rename to pkg/commands/objectstore/liststore.go From 7fd16d78e26030a10de6b84d19d725d4317de807 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 13:51:09 +0000 Subject: [PATCH 06/16] Sort commands alphabetically --- pkg/app/commands.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/app/commands.go b/pkg/app/commands.go index d25fbaa90..db0069b65 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -299,13 +299,13 @@ 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) + objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreKeysCmdRoot := objectstore.NewKeysRootCommand(app, globals) - objectstoreListKeys := objectstore.NewListKeysCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) - objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) - objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) objectstoreDeleteKey := objectstore.NewDeleteKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreListKeys := objectstore.NewListKeysCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) popCmdRoot := pop.NewRootCommand(app, globals) profileCmdRoot := profile.NewRootCommand(app, globals) profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, profile.APIClientFactory(opts.APIClient), globals) From c5e482aa9fb6bfa6c08b30d2de14d37b829eb3bb Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 14:01:31 +0000 Subject: [PATCH 07/16] Rework description, parameters and output --- pkg/commands/objectstore/create.go | 2 +- pkg/commands/objectstore/deletekey.go | 2 +- pkg/commands/objectstore/deletestore.go | 6 +++--- pkg/commands/objectstore/getkey.go | 17 +++++++++-------- pkg/commands/objectstore/insert.go | 18 ++++++++++-------- pkg/commands/objectstore/listkeys.go | 23 +++++++++++++++-------- pkg/commands/objectstore/liststore.go | 2 +- 7 files changed, 40 insertions(+), 30 deletions(-) diff --git a/pkg/commands/objectstore/create.go b/pkg/commands/objectstore/create.go index 209845f95..4b36e1bba 100644 --- a/pkg/commands/objectstore/create.go +++ b/pkg/commands/objectstore/create.go @@ -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 } diff --git a/pkg/commands/objectstore/deletekey.go b/pkg/commands/objectstore/deletekey.go index c6cb2098b..b6ebeb3d8 100644 --- a/pkg/commands/objectstore/deletekey.go +++ b/pkg/commands/objectstore/deletekey.go @@ -26,8 +26,8 @@ func NewDeleteKeyCommand(parent cmd.Registerer, globals *config.Data, data manif manifest: data, } c.CmdClause = parent.Command("delete", "Delete a key") - c.CmdClause.Flag("key-name", "Key name").Short('n').Required().StringVar(&c.Input.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 } diff --git a/pkg/commands/objectstore/deletestore.go b/pkg/commands/objectstore/deletestore.go index 4567ed9cd..6076e47a3 100644 --- a/pkg/commands/objectstore/deletestore.go +++ b/pkg/commands/objectstore/deletestore.go @@ -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 } @@ -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 } diff --git a/pkg/commands/objectstore/getkey.go b/pkg/commands/objectstore/getkey.go index 4381604d2..aca4c0664 100644 --- a/pkg/commands/objectstore/getkey.go +++ b/pkg/commands/objectstore/getkey.go @@ -21,14 +21,15 @@ type GetKeyCommand struct { // 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) + c := GetKeyCommand{ + 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{ diff --git a/pkg/commands/objectstore/insert.go b/pkg/commands/objectstore/insert.go index 78c4f414e..5e41c32be 100644 --- a/pkg/commands/objectstore/insert.go +++ b/pkg/commands/objectstore/insert.go @@ -19,14 +19,16 @@ type InsertKeyCommand struct { // NewInsertKeyCommand returns a usable command registered under the parent. func NewInsertKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *InsertKeyCommand { - var c InsertKeyCommand - c.Globals = globals - c.manifest = data - c.CmdClause = parent.Command("insert", "Insert key/value pair into a Fastly object store") - c.CmdClause.Flag("id", "ID of Object Store").Short('n').Required().StringVar(&c.Input.ID) - c.CmdClause.Flag("key", "Key to insert").Short('k').Required().StringVar(&c.Input.Key) - c.CmdClause.Flag("value", "Value to insert").Required().StringVar(&c.Input.Value) - + c := InsertKeyCommand{ + Base: cmd.Base{ + Globals: globals, + }, + manifest: data, + } + c.CmdClause = parent.Command("insert", "Insert a key-value pair") + 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 } diff --git a/pkg/commands/objectstore/listkeys.go b/pkg/commands/objectstore/listkeys.go index c19af4ec4..5586ad246 100644 --- a/pkg/commands/objectstore/listkeys.go +++ b/pkg/commands/objectstore/listkeys.go @@ -23,13 +23,14 @@ type ListKeysCommand struct { // NewListKeysCommand returns a usable command registered under the parent. func NewListKeysCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListKeysCommand { - var c ListKeysCommand - c.Globals = globals - c.manifest = data - c.CmdClause = parent.Command("list", "List Fastly object store keys") - - // required - c.CmdClause.Flag("id", "ID of object store").Required().StringVar(&c.Input.ID) + c := ListKeysCommand{ + Base: cmd.Base{ + Globals: globals, + }, + manifest: data, + } + c.CmdClause = parent.Command("list", "List keys") + c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID) // optional c.RegisterFlagBool(cmd.BoolFlagOpts{ @@ -66,7 +67,13 @@ func (c *ListKeysCommand) Exec(_ io.Reader, out io.Writer) error { return nil } - text.PrintObjectStoreKeys(out, "", o.Data) + if c.Globals.Flag.Verbose { + text.PrintObjectStoreKeys(out, "", o.Data) + return nil + } + for _, k := range o.Data { + text.Output(out, k) + } return nil } diff --git a/pkg/commands/objectstore/liststore.go b/pkg/commands/objectstore/liststore.go index 8317ae32d..871deec61 100644 --- a/pkg/commands/objectstore/liststore.go +++ b/pkg/commands/objectstore/liststore.go @@ -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") // optional c.RegisterFlagBool(cmd.BoolFlagOpts{ From c71b7d7556dc4a190d04420a9ffb9ad58844733b Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 14:03:40 +0000 Subject: [PATCH 08/16] More rename for consistency --- pkg/commands/objectstore/{create.go => createstore.go} | 0 pkg/commands/objectstore/{insert.go => insertkey.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename pkg/commands/objectstore/{create.go => createstore.go} (100%) rename pkg/commands/objectstore/{insert.go => insertkey.go} (100%) diff --git a/pkg/commands/objectstore/create.go b/pkg/commands/objectstore/createstore.go similarity index 100% rename from pkg/commands/objectstore/create.go rename to pkg/commands/objectstore/createstore.go diff --git a/pkg/commands/objectstore/insert.go b/pkg/commands/objectstore/insertkey.go similarity index 100% rename from pkg/commands/objectstore/insert.go rename to pkg/commands/objectstore/insertkey.go From 1bf6746ba404218c47cb200f8e3f22a39a32efe8 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 14:17:17 +0000 Subject: [PATCH 09/16] More consistency changes Add `Store' to functions and structs. --- pkg/app/commands.go | 6 +++--- pkg/commands/objectstore/createstore.go | 12 ++++++------ pkg/commands/objectstore/deletestore.go | 12 ++++++------ pkg/commands/objectstore/liststore.go | 12 ++++++------ 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/app/commands.go b/pkg/app/commands.go index db0069b65..0f5676219 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -298,9 +298,9 @@ func defineCommands( loggingSyslogList := syslog.NewListCommand(loggingSyslogCmdRoot.CmdClause, globals, data) loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.CmdClause, globals, data) objectstoreCmdRoot := objectstore.NewRootCommand(app, globals) - objectstoreCreate := objectstore.NewCreateCommand(objectstoreCmdRoot.CmdClause, globals, data) - objectstoreDelete := objectstore.NewDeleteCommand(objectstoreCmdRoot.CmdClause, globals, data) - objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data) + objectstoreCreate := objectstore.NewCreateStoreCommand(objectstoreCmdRoot.CmdClause, globals, data) + objectstoreDelete := objectstore.NewDeleteStoreCommand(objectstoreCmdRoot.CmdClause, globals, data) + objectstoreList := objectstore.NewListStoreCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreKeysCmdRoot := objectstore.NewKeysRootCommand(app, globals) objectstoreDeleteKey := objectstore.NewDeleteKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) diff --git a/pkg/commands/objectstore/createstore.go b/pkg/commands/objectstore/createstore.go index 4b36e1bba..e877658a9 100644 --- a/pkg/commands/objectstore/createstore.go +++ b/pkg/commands/objectstore/createstore.go @@ -10,16 +10,16 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// CreateCommand calls the Fastly API to create an object store. -type CreateCommand struct { +// CreateStoreCommand calls the Fastly API to create an object store. +type CreateStoreCommand struct { cmd.Base manifest manifest.Data Input fastly.CreateObjectStoreInput } -// NewCreateCommand returns a usable command registered under the parent. -func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateCommand { - c := CreateCommand{ +// NewCreateStoreCommand returns a usable command registered under the parent. +func NewCreateStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateStoreCommand { + c := CreateStoreCommand{ Base: cmd.Base{ Globals: globals, }, @@ -31,7 +31,7 @@ func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest } // Exec invokes the application logic for the command. -func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *CreateStoreCommand) Exec(_ io.Reader, out io.Writer) error { d, err := c.Globals.APIClient.CreateObjectStore(&c.Input) if err != nil { c.Globals.ErrLog.Add(err) diff --git a/pkg/commands/objectstore/deletestore.go b/pkg/commands/objectstore/deletestore.go index 6076e47a3..5830e7bfd 100644 --- a/pkg/commands/objectstore/deletestore.go +++ b/pkg/commands/objectstore/deletestore.go @@ -10,16 +10,16 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// DeleteCommand calls the Fastly API to delete an object store. -type DeleteCommand struct { +// DeleteStoreCommand calls the Fastly API to delete an object store. +type DeleteStoreCommand struct { cmd.Base manifest manifest.Data Input fastly.DeleteObjectStoreInput } -// NewDeleteCommand returns a usable command registered under the parent. -func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteCommand { - c := DeleteCommand{ +// NewDeleteStoreCommand returns a usable command registered under the parent. +func NewDeleteStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteStoreCommand { + c := DeleteStoreCommand{ Base: cmd.Base{ Globals: globals, }, @@ -31,7 +31,7 @@ func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest } // Exec invokes the application logic for the command. -func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *DeleteStoreCommand) Exec(_ io.Reader, out io.Writer) error { err := c.Globals.APIClient.DeleteObjectStore(&c.Input) if err != nil { c.Globals.ErrLog.Add(err) diff --git a/pkg/commands/objectstore/liststore.go b/pkg/commands/objectstore/liststore.go index 871deec61..b65363b84 100644 --- a/pkg/commands/objectstore/liststore.go +++ b/pkg/commands/objectstore/liststore.go @@ -13,17 +13,17 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// ListCommand calls the Fastly API to list the available object stores. -type ListCommand struct { +// ListStoreCommand calls the Fastly API to list the available object stores. +type ListStoreCommand struct { cmd.Base json bool manifest manifest.Data Input fastly.ListObjectStoresInput } -// NewListCommand returns a usable command registered under the parent. -func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListCommand { - c := ListCommand{ +// NewListStoreCommand returns a usable command registered under the parent. +func NewListStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListStoreCommand { + c := ListStoreCommand{ Base: cmd.Base{ Globals: globals, }, @@ -43,7 +43,7 @@ func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.D } // Exec invokes the application logic for the command. -func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *ListStoreCommand) Exec(_ io.Reader, out io.Writer) error { if c.Globals.Verbose() && c.json { return fsterr.ErrInvalidVerboseJSONCombo } From b12b3f1dd8d1ca7c45b2435bb5ea31fd36913ddf Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 14:26:59 +0000 Subject: [PATCH 10/16] Add describe subcommand under object-store --- pkg/api/interface.go | 1 + pkg/app/commands.go | 2 + pkg/commands/objectstore/describestore.go | 73 +++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 pkg/commands/objectstore/describestore.go 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 +} From 06cc805be5e9ef908043e64ab5d7164c3af86eda Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 14:59:53 +0000 Subject: [PATCH 11/16] Move object-store-keys into its own package Rename things accordingly. Requested by @integralist. --- pkg/app/commands.go | 19 +++++++------ .../objectstore/{createstore.go => create.go} | 12 ++++---- .../objectstore/{deletestore.go => delete.go} | 12 ++++---- .../{describestore.go => describe.go} | 12 ++++---- pkg/commands/objectstore/liststore.go | 12 ++++---- .../objectstore/{root_store.go => root.go} | 0 pkg/commands/objectstore/root_keys.go | 28 ------------------- .../delete.go} | 14 +++++----- pkg/commands/objectstorekeys/doc.go | 3 ++ .../getkey.go => objectstorekeys/get.go} | 14 +++++----- .../insert.go} | 14 +++++----- .../listkeys.go => objectstorekeys/list.go} | 14 +++++----- pkg/commands/objectstorekeys/root.go | 28 +++++++++++++++++++ 13 files changed, 93 insertions(+), 89 deletions(-) rename pkg/commands/objectstore/{createstore.go => create.go} (66%) rename pkg/commands/objectstore/{deletestore.go => delete.go} (66%) rename pkg/commands/objectstore/{describestore.go => describe.go} (75%) rename pkg/commands/objectstore/{root_store.go => root.go} (100%) delete mode 100644 pkg/commands/objectstore/root_keys.go rename pkg/commands/{objectstore/deletekey.go => objectstorekeys/delete.go} (68%) create mode 100644 pkg/commands/objectstorekeys/doc.go rename pkg/commands/{objectstore/getkey.go => objectstorekeys/get.go} (76%) rename pkg/commands/{objectstore/insertkey.go => objectstorekeys/insert.go} (69%) rename pkg/commands/{objectstore/listkeys.go => objectstorekeys/list.go} (77%) create mode 100644 pkg/commands/objectstorekeys/root.go diff --git a/pkg/app/commands.go b/pkg/app/commands.go index 68d487ede..2a3bad3ad 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -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" @@ -298,15 +299,15 @@ func defineCommands( loggingSyslogList := syslog.NewListCommand(loggingSyslogCmdRoot.CmdClause, globals, data) loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.CmdClause, globals, data) 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) - objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) - objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) - objectstoreListKeys := objectstore.NewListKeysCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) + objectstoreCreate := objectstore.NewCreateCommand(objectstoreCmdRoot.CmdClause, globals, data) + objectstoreDelete := objectstore.NewDeleteCommand(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) diff --git a/pkg/commands/objectstore/createstore.go b/pkg/commands/objectstore/create.go similarity index 66% rename from pkg/commands/objectstore/createstore.go rename to pkg/commands/objectstore/create.go index e877658a9..4b36e1bba 100644 --- a/pkg/commands/objectstore/createstore.go +++ b/pkg/commands/objectstore/create.go @@ -10,16 +10,16 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// CreateStoreCommand calls the Fastly API to create an object store. -type CreateStoreCommand struct { +// CreateCommand calls the Fastly API to create an object store. +type CreateCommand struct { cmd.Base manifest manifest.Data Input fastly.CreateObjectStoreInput } -// NewCreateStoreCommand returns a usable command registered under the parent. -func NewCreateStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateStoreCommand { - c := CreateStoreCommand{ +// NewCreateCommand returns a usable command registered under the parent. +func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateCommand { + c := CreateCommand{ Base: cmd.Base{ Globals: globals, }, @@ -31,7 +31,7 @@ func NewCreateStoreCommand(parent cmd.Registerer, globals *config.Data, data man } // Exec invokes the application logic for the command. -func (c *CreateStoreCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { d, err := c.Globals.APIClient.CreateObjectStore(&c.Input) if err != nil { c.Globals.ErrLog.Add(err) diff --git a/pkg/commands/objectstore/deletestore.go b/pkg/commands/objectstore/delete.go similarity index 66% rename from pkg/commands/objectstore/deletestore.go rename to pkg/commands/objectstore/delete.go index 5830e7bfd..6076e47a3 100644 --- a/pkg/commands/objectstore/deletestore.go +++ b/pkg/commands/objectstore/delete.go @@ -10,16 +10,16 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// DeleteStoreCommand calls the Fastly API to delete an object store. -type DeleteStoreCommand struct { +// DeleteCommand calls the Fastly API to delete an object store. +type DeleteCommand struct { cmd.Base manifest manifest.Data Input fastly.DeleteObjectStoreInput } -// NewDeleteStoreCommand returns a usable command registered under the parent. -func NewDeleteStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteStoreCommand { - c := DeleteStoreCommand{ +// 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, }, @@ -31,7 +31,7 @@ func NewDeleteStoreCommand(parent cmd.Registerer, globals *config.Data, data man } // Exec invokes the application logic for the command. -func (c *DeleteStoreCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error { err := c.Globals.APIClient.DeleteObjectStore(&c.Input) if err != nil { c.Globals.ErrLog.Add(err) diff --git a/pkg/commands/objectstore/describestore.go b/pkg/commands/objectstore/describe.go similarity index 75% rename from pkg/commands/objectstore/describestore.go rename to pkg/commands/objectstore/describe.go index 5cb4dc97d..d59b6bfb6 100644 --- a/pkg/commands/objectstore/describestore.go +++ b/pkg/commands/objectstore/describe.go @@ -13,17 +13,17 @@ import ( "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 { +// 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 } -// NewDescribeStoreCommand returns a usable command registered under the parent. -func NewDescribeStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DescribeStoreCommand { - c := DescribeStoreCommand{ +// 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, }, @@ -44,7 +44,7 @@ func NewDescribeStoreCommand(parent cmd.Registerer, globals *config.Data, data m } // Exec invokes the application logic for the command. -func (c *DescribeStoreCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error { if c.Globals.Verbose() && c.json { return fsterr.ErrInvalidVerboseJSONCombo } diff --git a/pkg/commands/objectstore/liststore.go b/pkg/commands/objectstore/liststore.go index b65363b84..871deec61 100644 --- a/pkg/commands/objectstore/liststore.go +++ b/pkg/commands/objectstore/liststore.go @@ -13,17 +13,17 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// ListStoreCommand calls the Fastly API to list the available object stores. -type ListStoreCommand struct { +// ListCommand calls the Fastly API to list the available object stores. +type ListCommand struct { cmd.Base json bool manifest manifest.Data Input fastly.ListObjectStoresInput } -// NewListStoreCommand returns a usable command registered under the parent. -func NewListStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListStoreCommand { - c := ListStoreCommand{ +// NewListCommand returns a usable command registered under the parent. +func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListCommand { + c := ListCommand{ Base: cmd.Base{ Globals: globals, }, @@ -43,7 +43,7 @@ func NewListStoreCommand(parent cmd.Registerer, globals *config.Data, data manif } // Exec invokes the application logic for the command. -func (c *ListStoreCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { if c.Globals.Verbose() && c.json { return fsterr.ErrInvalidVerboseJSONCombo } diff --git a/pkg/commands/objectstore/root_store.go b/pkg/commands/objectstore/root.go similarity index 100% rename from pkg/commands/objectstore/root_store.go rename to pkg/commands/objectstore/root.go diff --git a/pkg/commands/objectstore/root_keys.go b/pkg/commands/objectstore/root_keys.go deleted file mode 100644 index 6fd89557b..000000000 --- a/pkg/commands/objectstore/root_keys.go +++ /dev/null @@ -1,28 +0,0 @@ -package objectstore - -import ( - "io" - - "github.com/fastly/cli/pkg/cmd" - "github.com/fastly/cli/pkg/config" -) - -// KeysRootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type KeysRootCommand struct { - cmd.Base - // no flags -} - -// NewKeysRootCommand returns a new command registered in the parent. -func NewKeysRootCommand(parent cmd.Registerer, globals *config.Data) *KeysRootCommand { - var c KeysRootCommand - c.Globals = globals - c.CmdClause = parent.Command("object-store-keys", "Manipulate Fastly Object Store keys") - return &c -} - -// Exec implements the command interface. -func (c *KeysRootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/objectstore/deletekey.go b/pkg/commands/objectstorekeys/delete.go similarity index 68% rename from pkg/commands/objectstore/deletekey.go rename to pkg/commands/objectstorekeys/delete.go index b6ebeb3d8..652c63494 100644 --- a/pkg/commands/objectstore/deletekey.go +++ b/pkg/commands/objectstorekeys/delete.go @@ -1,4 +1,4 @@ -package objectstore +package objectstorekeys import ( "io" @@ -10,16 +10,16 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// DeleteKeyCommand calls the Fastly API to delete an object store. -type DeleteKeyCommand struct { +// DeleteCommand calls the Fastly API to delete an object store. +type DeleteCommand struct { cmd.Base manifest manifest.Data Input fastly.DeleteObjectStoreKeyInput } -// NewDeleteKeyCommand returns a usable command registered under the parent. -func NewDeleteKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteKeyCommand { - c := DeleteKeyCommand{ +// 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, }, @@ -32,7 +32,7 @@ func NewDeleteKeyCommand(parent cmd.Registerer, globals *config.Data, data manif } // Exec invokes the application logic for the command. -func (c *DeleteKeyCommand) Exec(_ io.Reader, out io.Writer) error { +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) diff --git a/pkg/commands/objectstorekeys/doc.go b/pkg/commands/objectstorekeys/doc.go new file mode 100644 index 000000000..05b908bcc --- /dev/null +++ b/pkg/commands/objectstorekeys/doc.go @@ -0,0 +1,3 @@ +// Package objectstorekeys contains commands to inspect and manipulate Fastly edge +// object stores keys. +package objectstorekeys diff --git a/pkg/commands/objectstore/getkey.go b/pkg/commands/objectstorekeys/get.go similarity index 76% rename from pkg/commands/objectstore/getkey.go rename to pkg/commands/objectstorekeys/get.go index aca4c0664..09fd4efbe 100644 --- a/pkg/commands/objectstore/getkey.go +++ b/pkg/commands/objectstorekeys/get.go @@ -1,4 +1,4 @@ -package objectstore +package objectstorekeys import ( "io" @@ -11,17 +11,17 @@ 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 { - c := GetKeyCommand{ +// 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, }, @@ -43,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 } diff --git a/pkg/commands/objectstore/insertkey.go b/pkg/commands/objectstorekeys/insert.go similarity index 69% rename from pkg/commands/objectstore/insertkey.go rename to pkg/commands/objectstorekeys/insert.go index 5e41c32be..c4dbf4892 100644 --- a/pkg/commands/objectstore/insertkey.go +++ b/pkg/commands/objectstorekeys/insert.go @@ -1,4 +1,4 @@ -package objectstore +package objectstorekeys import ( "io" @@ -10,16 +10,16 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// InsertKeyCommand calls the Fastly API to insert a key into an object store. -type InsertKeyCommand struct { +// InsertCommand calls the Fastly API to insert a key into an object store. +type InsertCommand struct { cmd.Base manifest manifest.Data Input fastly.InsertObjectStoreKeyInput } -// NewInsertKeyCommand returns a usable command registered under the parent. -func NewInsertKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *InsertKeyCommand { - c := InsertKeyCommand{ +// 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, }, @@ -33,7 +33,7 @@ func NewInsertKeyCommand(parent cmd.Registerer, globals *config.Data, data manif } // Exec invokes the application logic for the command. -func (c *InsertKeyCommand) Exec(_ io.Reader, out io.Writer) error { +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) diff --git a/pkg/commands/objectstore/listkeys.go b/pkg/commands/objectstorekeys/list.go similarity index 77% rename from pkg/commands/objectstore/listkeys.go rename to pkg/commands/objectstorekeys/list.go index 5586ad246..1b1802742 100644 --- a/pkg/commands/objectstore/listkeys.go +++ b/pkg/commands/objectstorekeys/list.go @@ -1,4 +1,4 @@ -package objectstore +package objectstorekeys import ( "encoding/json" @@ -13,17 +13,17 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// ListKeysCommand calls the Fastly API to list the keys for a given object store. -type ListKeysCommand struct { +// ListCommand calls the Fastly API to list the keys for a given object store. +type ListCommand struct { cmd.Base json bool manifest manifest.Data Input fastly.ListObjectStoreKeysInput } -// NewListKeysCommand returns a usable command registered under the parent. -func NewListKeysCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListKeysCommand { - c := ListKeysCommand{ +// NewListCommand returns a usable command registered under the parent. +func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListCommand { + c := ListCommand{ Base: cmd.Base{ Globals: globals, }, @@ -43,7 +43,7 @@ func NewListKeysCommand(parent cmd.Registerer, globals *config.Data, data manife } // Exec invokes the application logic for the command. -func (c *ListKeysCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { if c.Globals.Verbose() && c.json { return fsterr.ErrInvalidVerboseJSONCombo } diff --git a/pkg/commands/objectstorekeys/root.go b/pkg/commands/objectstorekeys/root.go new file mode 100644 index 000000000..e31962845 --- /dev/null +++ b/pkg/commands/objectstorekeys/root.go @@ -0,0 +1,28 @@ +package objectstorekeys + +import ( + "io" + + "github.com/fastly/cli/pkg/cmd" + "github.com/fastly/cli/pkg/config" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + cmd.Base + // no flags +} + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent cmd.Registerer, globals *config.Data) *RootCommand { + var c RootCommand + c.Globals = globals + c.CmdClause = parent.Command("object-store-keys", "Manipulate Fastly Object Store keys") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} From f1aab49d1f50f9fe8e2f6382d35ddb87b7b37d9a Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 15:25:50 +0000 Subject: [PATCH 12/16] Fix tests Mock recently added methods. --- pkg/mock/api.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/mock/api.go b/pkg/mock/api.go index c0f9c4523..5b11405fa 100644 --- a/pkg/mock/api.go +++ b/pkg/mock/api.go @@ -320,11 +320,13 @@ type API struct { DeleteServiceAuthorizationFn func(i *fastly.DeleteServiceAuthorizationInput) error CreateObjectStoreFn func(i *fastly.CreateObjectStoreInput) (*fastly.ObjectStore, error) + GetObjectStoreFn func(i *fastly.GetObjectStoreInput) (*fastly.ObjectStore, error) ListObjectStoresFn func(i *fastly.ListObjectStoresInput) (*fastly.ListObjectStoresResponse, error) DeleteObjectStoreFn func(i *fastly.DeleteObjectStoreInput) error ListObjectStoreKeysFn func(i *fastly.ListObjectStoreKeysInput) (*fastly.ListObjectStoreKeysResponse, error) GetObjectStoreKeyFn func(i *fastly.GetObjectStoreKeyInput) (string, error) InsertObjectStoreKeyFn func(i *fastly.InsertObjectStoreKeyInput) error + DeleteObjectStoreKeyFn func(i *fastly.DeleteObjectStoreKeyInput) error CreateSecretStoreFn func(i *fastly.CreateSecretStoreInput) (*fastly.SecretStore, error) GetSecretStoreFn func(i *fastly.GetSecretStoreInput) (*fastly.SecretStore, error) @@ -1633,6 +1635,11 @@ func (m API) CreateObjectStore(i *fastly.CreateObjectStoreInput) (*fastly.Object return m.CreateObjectStoreFn(i) } +// GetObjectStore implements Interface. +func (m API) GetObjectStore(i *fastly.GetObjectStoreInput) (*fastly.ObjectStore, error) { + return m.GetObjectStoreFn(i) +} + // ListObjectStores implements Interface. func (m API) ListObjectStores(i *fastly.ListObjectStoresInput) (*fastly.ListObjectStoresResponse, error) { return m.ListObjectStoresFn(i) @@ -1658,6 +1665,11 @@ func (m API) InsertObjectStoreKey(i *fastly.InsertObjectStoreKeyInput) error { return m.InsertObjectStoreKeyFn(i) } +// DeleteObjectStoreKey implements Interface. +func (m API) DeleteObjectStoreKey(i *fastly.DeleteObjectStoreKeyInput) error { + return m.DeleteObjectStoreKeyFn(i) +} + // CreateSecretStore implements Interface. func (m API) CreateSecretStore(i *fastly.CreateSecretStoreInput) (*fastly.SecretStore, error) { return m.CreateSecretStoreFn(i) From b42efce76ef58ac543e6c1804a3636d580cea7ca Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 15:51:57 +0000 Subject: [PATCH 13/16] Show the object store id on creation --- pkg/commands/objectstore/create.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/objectstore/create.go b/pkg/commands/objectstore/create.go index 4b36e1bba..3fb2ed349 100644 --- a/pkg/commands/objectstore/create.go +++ b/pkg/commands/objectstore/create.go @@ -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 } From a522f103b253ebb043c33904fa53acf85ee832a1 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 15:57:22 +0000 Subject: [PATCH 14/16] Add json output support to object-store create --- pkg/commands/objectstore/create.go | 31 ++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/commands/objectstore/create.go b/pkg/commands/objectstore/create.go index 3fb2ed349..6e6e7096f 100644 --- a/pkg/commands/objectstore/create.go +++ b/pkg/commands/objectstore/create.go @@ -1,10 +1,13 @@ 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" @@ -13,6 +16,7 @@ import ( // CreateCommand calls the Fastly API to create an object store. type CreateCommand struct { cmd.Base + json bool manifest manifest.Data Input fastly.CreateObjectStoreInput } @@ -27,17 +31,40 @@ func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest } c.CmdClause = parent.Command("create", "Create an object store") c.CmdClause.Flag("name", "Name of Object Store").Short('n').Required().StringVar(&c.Input.Name) + 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 *CreateCommand) Exec(_ io.Reader, out io.Writer) error { - d, err := c.Globals.APIClient.CreateObjectStore(&c.Input) + if c.Globals.Verbose() && c.json { + return fsterr.ErrInvalidVerboseJSONCombo + } + + o, err := c.Globals.APIClient.CreateObjectStore(&c.Input) if err != nil { c.Globals.ErrLog.Add(err) return err } - text.Success(out, "Created object store %s (name %s)", d.ID, d.Name) + 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.Success(out, "Created object store %s (name %s)", o.ID, o.Name) return nil } From 939a8c510223e439c72da920f38e4cdf2ac1e21a Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 15:59:30 +0000 Subject: [PATCH 15/16] Rename file Missed in previous commit. --- pkg/commands/objectstore/{liststore.go => list.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pkg/commands/objectstore/{liststore.go => list.go} (100%) diff --git a/pkg/commands/objectstore/liststore.go b/pkg/commands/objectstore/list.go similarity index 100% rename from pkg/commands/objectstore/liststore.go rename to pkg/commands/objectstore/list.go From c837c168b8cd24131e53c8b47c3f26c7b6305a78 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Mon, 6 Feb 2023 16:11:06 +0000 Subject: [PATCH 16/16] Rename insert to create --- pkg/app/commands.go | 4 ++-- .../objectstorekeys/{insert.go => create.go} | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) rename pkg/commands/objectstorekeys/{insert.go => create.go} (68%) diff --git a/pkg/app/commands.go b/pkg/app/commands.go index 2a3bad3ad..da0264c7f 100644 --- a/pkg/app/commands.go +++ b/pkg/app/commands.go @@ -304,9 +304,9 @@ func defineCommands( objectstoreDescribe := objectstore.NewDescribeCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data) objectstoreKeysCmdRoot := objectstorekeys.NewRootCommand(app, globals) + objectstoreCreateKey := objectstorekeys.NewCreateCommand(objectstoreKeysCmdRoot.CmdClause, globals, data) 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) @@ -635,7 +635,7 @@ func defineCommands( objectstoreDelete, objectstoreDescribe, objectstoreListKeys, - objectstoreInsertKey, + objectstoreCreateKey, objectstoreGetKey, objectstoreDeleteKey, popCmdRoot, diff --git a/pkg/commands/objectstorekeys/insert.go b/pkg/commands/objectstorekeys/create.go similarity index 68% rename from pkg/commands/objectstorekeys/insert.go rename to pkg/commands/objectstorekeys/create.go index c4dbf4892..cb14eff0a 100644 --- a/pkg/commands/objectstorekeys/insert.go +++ b/pkg/commands/objectstorekeys/create.go @@ -10,22 +10,22 @@ import ( "github.com/fastly/go-fastly/v7/fastly" ) -// InsertCommand calls the Fastly API to insert a key into an object store. -type InsertCommand struct { +// CreateCommand calls the Fastly API to insert a key into an object store. +type CreateCommand 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{ +// NewCreateCommand returns a usable command registered under the parent. +func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateCommand { + c := CreateCommand{ Base: cmd.Base{ Globals: globals, }, manifest: data, } - c.CmdClause = parent.Command("insert", "Insert a key-value pair") + c.CmdClause = parent.Command("create", "Insert a key-value pair").Alias("insert") 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) @@ -33,7 +33,7 @@ func NewInsertCommand(parent cmd.Registerer, globals *config.Data, data manifest } // Exec invokes the application logic for the command. -func (c *InsertCommand) Exec(_ io.Reader, out io.Writer) error { +func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { err := c.Globals.APIClient.InsertObjectStoreKey(&c.Input) if err != nil { c.Globals.ErrLog.Add(err)