From 518c7308ab58fc1b17281ea097c3673b38043afd Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 19 May 2024 11:21:01 +0100 Subject: [PATCH] a bit of refactoring; added new custom errors --- cmd/enbas/add.go | 7 +++---- cmd/enbas/errors.go | 14 ++++++++++++++ cmd/enbas/main.go | 6 +++++- cmd/enbas/remove.go | 7 +++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cmd/enbas/add.go b/cmd/enbas/add.go index c1afa29..a0612b7 100644 --- a/cmd/enbas/add.go +++ b/cmd/enbas/add.go @@ -1,7 +1,6 @@ package main import ( - "errors" "flag" "fmt" @@ -24,9 +23,9 @@ func newAddCommand(name, summary string) *addCommand { accountIDs: accountIDs(emptyArr), } - command.StringVar(&command.toResourceType, "add-to", "", "specify the type of resource to add to") + command.StringVar(&command.toResourceType, addToFlag, "", "specify the type of resource to add to") command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to add to") - command.Var(&command.accountIDs, "account-id", "the ID of the account to add to the list") + command.Var(&command.accountIDs, accountIDFlag, "the ID of the account to add to the list") command.Usage = commandUsageFunc(name, summary, command.FlagSet) @@ -61,7 +60,7 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error { } if len(c.accountIDs) == 0 { - return errors.New("no account IDs has been specified") + return noAccountIDsSpecifiedError{} } if err := gtsClient.AddAccountsToList(c.listID, []string(c.accountIDs)); err != nil { diff --git a/cmd/enbas/errors.go b/cmd/enbas/errors.go index 9a0db60..8aa5dcc 100644 --- a/cmd/enbas/errors.go +++ b/cmd/enbas/errors.go @@ -23,3 +23,17 @@ type invalidTimelineCategoryError struct { func (e invalidTimelineCategoryError) Error() string { return "'" + e.category + "' is not a valid timeline category (please choose home, public, tag or list)" } + +type unknownSubcommandError struct { + subcommand string +} + +func (e unknownSubcommandError) Error() string { + return "unknown subcommand '" + e.subcommand + "'" +} + +type noAccountIDsSpecifiedError struct{} + +func (e noAccountIDsSpecifiedError) Error() string { + return "no account IDs specified" +} diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 3a5e8de..b9fa5f7 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -8,11 +8,14 @@ import ( const ( accountFlag = "account" + accountIDFlag = "account-id" + addToFlag = "add-to" instanceFlag = "instance" listIDFlag = "list-id" listTitleFlag = "list-title" listRepliesPolicyFlag = "list-replies-policy" myAccountFlag = "my-account" + removeFromFlag = "remove-from" resourceTypeFlag = "type" statusIDFlag = "status-id" tagFlag = "tag" @@ -107,7 +110,8 @@ func run() error { executor = newRemoveCommand(remove, summaries[remove]) default: flag.Usage() - return fmt.Errorf("unknown subcommand %q", subcommand) + + return unknownSubcommandError{subcommand} } if err := executor.Parse(args); err != nil { diff --git a/cmd/enbas/remove.go b/cmd/enbas/remove.go index 951a939..0c79676 100644 --- a/cmd/enbas/remove.go +++ b/cmd/enbas/remove.go @@ -1,7 +1,6 @@ package main import ( - "errors" "flag" "fmt" @@ -24,9 +23,9 @@ func newRemoveCommand(name, summary string) *removeCommand { accountIDs: accountIDs(emptyArr), } - command.StringVar(&command.fromResourceType, "remove-from", "", "specify the type of resource to remove from") + command.StringVar(&command.fromResourceType, removeFromFlag, "", "specify the type of resource to remove from") command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to remove from") - command.Var(&command.accountIDs, "account-id", "the ID of the account to remove from the list") + command.Var(&command.accountIDs, accountIDFlag, "the ID of the account to remove from the list") command.Usage = commandUsageFunc(name, summary, command.FlagSet) @@ -61,7 +60,7 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error { } if len(c.accountIDs) == 0 { - return errors.New("no account IDs has been specified") + return noAccountIDsSpecifiedError{} } if err := gtsClient.RemoveAccountsFromList(c.listID, []string(c.accountIDs)); err != nil {