Compare commits

..

2 commits

Author SHA1 Message Date
b12f2328b3
add client method to add a private note 2024-05-21 23:08:11 +01:00
6e40792fec
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.
2024-05-21 23:05:23 +01:00
3 changed files with 38 additions and 6 deletions

View file

@ -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
}

View file

@ -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"

View file

@ -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
}