From 6e40792fecfdde67ef5fe8411e07231c969f5865 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 21 May 2024 23:05:23 +0100 Subject: [PATCH] fix: redesign the add and remove commands Redesigned the add and remove subcommands so that it is easier to add logic when adding and removing additional resources. --- cmd/enbas/add.go | 20 ++++++++++++++++++-- cmd/enbas/main.go | 4 ++-- cmd/enbas/remove.go | 20 ++++++++++++++++++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/cmd/enbas/add.go b/cmd/enbas/add.go index b47c178..23c155c 100644 --- a/cmd/enbas/add.go +++ b/cmd/enbas/add.go @@ -10,6 +10,7 @@ import ( type addCommand struct { *flag.FlagSet + resourceType string toResourceType string listID string accountNames accountNames @@ -23,7 +24,8 @@ func newAddCommand(name, summary string) *addCommand { accountNames: accountNames(emptyArr), } - command.StringVar(&command.toResourceType, addToFlag, "", "specify the type of resource to add to") + command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the resource type to add (e.g. account, note)") + command.StringVar(&command.toResourceType, addToFlag, "", "specify the target resource type to add to (e.g. list, account, etc)") command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to add to") command.Var(&command.accountNames, accountNameFlag, "the name of the account to add to the resource") @@ -38,7 +40,7 @@ func (c *addCommand) Execute() error { } funcMap := map[string]func(*client.Client) error{ - listResource: c.addAccountsToList, + listResource: c.addToList, } doFunc, ok := funcMap[c.toResourceType] @@ -54,6 +56,19 @@ func (c *addCommand) Execute() error { return doFunc(gtsClient) } +func (c *addCommand) addToList(gtsClient *client.Client) error { + funcMap := map[string]func(*client.Client) error{ + accountResource: c.addAccountsToList, + } + + doFunc, ok := funcMap[c.resourceType] + if !ok { + return unsupportedResourceTypeError{resourceType: c.resourceType} + } + + return doFunc(gtsClient) +} + func (c *addCommand) addAccountsToList(gtsClient *client.Client) error { if c.listID == "" { return flagNotSetError{flagText: listIDFlag} @@ -70,6 +85,7 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error { if err != nil { return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err) } + accountIDs[i] = accountID } diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 485f290..b036204 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -8,13 +8,13 @@ import ( const ( accountNameFlag = "account-name" - addToFlag = "add-to" + addToFlag = "to" instanceFlag = "instance" listIDFlag = "list-id" listTitleFlag = "list-title" listRepliesPolicyFlag = "list-replies-policy" myAccountFlag = "my-account" - removeFromFlag = "remove-from" + removeFromFlag = "from" resourceTypeFlag = "type" statusIDFlag = "status-id" tagFlag = "tag" diff --git a/cmd/enbas/remove.go b/cmd/enbas/remove.go index 9d388b1..e5ebbde 100644 --- a/cmd/enbas/remove.go +++ b/cmd/enbas/remove.go @@ -10,6 +10,7 @@ import ( type removeCommand struct { *flag.FlagSet + resourceType string fromResourceType string listID string accountNames accountNames @@ -23,7 +24,8 @@ func newRemoveCommand(name, summary string) *removeCommand { accountNames: accountNames(emptyArr), } - command.StringVar(&command.fromResourceType, removeFromFlag, "", "specify the type of resource to remove from") + command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the resource type to remove (e.g. account, note)") + command.StringVar(&command.fromResourceType, removeFromFlag, "", "specify the resource type to remove from (e.g. list, account, etc)") command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to remove from") command.Var(&command.accountNames, accountNameFlag, "the name of the account to remove from the resource") @@ -38,7 +40,7 @@ func (c *removeCommand) Execute() error { } funcMap := map[string]func(*client.Client) error{ - listResource: c.removeAccountsFromList, + listResource: c.removeFromList, } doFunc, ok := funcMap[c.fromResourceType] @@ -54,6 +56,19 @@ func (c *removeCommand) Execute() error { return doFunc(gtsClient) } +func (c *removeCommand) removeFromList(gtsClient *client.Client) error { + funcMap := map[string]func(*client.Client) error{ + accountResource: c.removeAccountsFromList, + } + + doFunc, ok := funcMap[c.resourceType] + if !ok { + return unsupportedResourceTypeError{resourceType: c.resourceType} + } + + return doFunc(gtsClient) +} + func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error { if c.listID == "" { return flagNotSetError{flagText: listIDFlag} @@ -70,6 +85,7 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error { if err != nil { return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err) } + accountIDs[i] = accountID }