a bit of refactoring; added new custom errors

This commit is contained in:
Dan Anglin 2024-05-19 11:21:01 +01:00
parent 22ed2e095d
commit 518c7308ab
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 25 additions and 9 deletions

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
@ -24,9 +23,9 @@ func newAddCommand(name, summary string) *addCommand {
accountIDs: accountIDs(emptyArr), 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.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) command.Usage = commandUsageFunc(name, summary, command.FlagSet)
@ -61,7 +60,7 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
} }
if len(c.accountIDs) == 0 { 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 { if err := gtsClient.AddAccountsToList(c.listID, []string(c.accountIDs)); err != nil {

View file

@ -23,3 +23,17 @@ type invalidTimelineCategoryError struct {
func (e invalidTimelineCategoryError) Error() string { func (e invalidTimelineCategoryError) Error() string {
return "'" + e.category + "' is not a valid timeline category (please choose home, public, tag or list)" 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"
}

View file

@ -8,11 +8,14 @@ import (
const ( const (
accountFlag = "account" accountFlag = "account"
accountIDFlag = "account-id"
addToFlag = "add-to"
instanceFlag = "instance" instanceFlag = "instance"
listIDFlag = "list-id" listIDFlag = "list-id"
listTitleFlag = "list-title" listTitleFlag = "list-title"
listRepliesPolicyFlag = "list-replies-policy" listRepliesPolicyFlag = "list-replies-policy"
myAccountFlag = "my-account" myAccountFlag = "my-account"
removeFromFlag = "remove-from"
resourceTypeFlag = "type" resourceTypeFlag = "type"
statusIDFlag = "status-id" statusIDFlag = "status-id"
tagFlag = "tag" tagFlag = "tag"
@ -107,7 +110,8 @@ func run() error {
executor = newRemoveCommand(remove, summaries[remove]) executor = newRemoveCommand(remove, summaries[remove])
default: default:
flag.Usage() flag.Usage()
return fmt.Errorf("unknown subcommand %q", subcommand)
return unknownSubcommandError{subcommand}
} }
if err := executor.Parse(args); err != nil { if err := executor.Parse(args); err != nil {

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
@ -24,9 +23,9 @@ func newRemoveCommand(name, summary string) *removeCommand {
accountIDs: accountIDs(emptyArr), 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.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) command.Usage = commandUsageFunc(name, summary, command.FlagSet)
@ -61,7 +60,7 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error {
} }
if len(c.accountIDs) == 0 { 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 { if err := gtsClient.RemoveAccountsFromList(c.listID, []string(c.accountIDs)); err != nil {