diff --git a/cmd/enbas/account.go b/cmd/enbas/account.go new file mode 100644 index 0000000..4134b45 --- /dev/null +++ b/cmd/enbas/account.go @@ -0,0 +1,76 @@ +package main + +import ( + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/client" + "codeflow.dananglin.me.uk/apollo/enbas/internal/config" + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +func getAccountID(gts *client.Client, myAccount bool, accountName string) (string, error) { + var ( + accountID string + err error + ) + + switch { + case myAccount: + accountID, err = getMyAccountID(gts) + if err != nil { + return "", fmt.Errorf("unable to get your account ID; %w", err) + } + case accountName != "": + accountID, err = getTheirAccountID(gts, accountName) + if err != nil { + return "", fmt.Errorf("unable to get their account ID; %w", err) + } + default: + return "", noAccountSpecifiedError{} + } + + return accountID, nil +} + +func getTheirAccountID(gts *client.Client, accountURI string) (string, error) { + account, err := getAccount(gts, accountURI) + if err != nil { + return "", fmt.Errorf("unable to retrieve your account; %w", err) + } + + return account.ID, nil +} + +func getMyAccountID(gts *client.Client) (string, error) { + account, err := getMyAccount(gts) + if err != nil { + return "", fmt.Errorf("received an error while getting your account details; %w", err) + } + + return account.ID, nil +} + +func getMyAccount(gts *client.Client) (model.Account, error) { + authConfig, err := config.NewAuthenticationConfigFromFile() + if err != nil { + return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration; %w", err) + } + + accountURI := authConfig.CurrentAccount + + account, err := getAccount(gts, accountURI) + if err != nil { + return model.Account{}, fmt.Errorf("unable to retrieve your account; %w", err) + } + + return account, nil +} + +func getAccount(gts *client.Client, accountURI string) (model.Account, error) { + account, err := gts.GetAccount(accountURI) + if err != nil { + return model.Account{}, fmt.Errorf("unable to retrieve the account details; %w", err) + } + + return account, nil +} diff --git a/cmd/enbas/add.go b/cmd/enbas/add.go index a0612b7..b47c178 100644 --- a/cmd/enbas/add.go +++ b/cmd/enbas/add.go @@ -12,20 +12,20 @@ type addCommand struct { toResourceType string listID string - accountIDs accountIDs + accountNames accountNames } func newAddCommand(name, summary string) *addCommand { emptyArr := make([]string, 0, 3) command := addCommand{ - FlagSet: flag.NewFlagSet(name, flag.ExitOnError), - accountIDs: accountIDs(emptyArr), + FlagSet: flag.NewFlagSet(name, flag.ExitOnError), + accountNames: accountNames(emptyArr), } 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, accountIDFlag, "the ID of the account to add to the list") + command.Var(&command.accountNames, accountNameFlag, "the name of the account to add to the resource") command.Usage = commandUsageFunc(name, summary, command.FlagSet) @@ -34,7 +34,7 @@ func newAddCommand(name, summary string) *addCommand { func (c *addCommand) Execute() error { if c.toResourceType == "" { - return flagNotSetError{flagText: "add-to"} + return flagNotSetError{flagText: addToFlag} } funcMap := map[string]func(*client.Client) error{ @@ -59,11 +59,21 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error { return flagNotSetError{flagText: listIDFlag} } - if len(c.accountIDs) == 0 { - return noAccountIDsSpecifiedError{} + if len(c.accountNames) == 0 { + return noAccountSpecifiedError{} } - if err := gtsClient.AddAccountsToList(c.listID, []string(c.accountIDs)); err != nil { + accountIDs := make([]string, len(c.accountNames)) + + for i := range c.accountNames { + accountID, err := getTheirAccountID(gtsClient, c.accountNames[i]) + if err != nil { + return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err) + } + accountIDs[i] = accountID + } + + if err := gtsClient.AddAccountsToList(c.listID, accountIDs); err != nil { return fmt.Errorf("unable to add the accounts to the list; %w", err) } diff --git a/cmd/enbas/errors.go b/cmd/enbas/errors.go index 8aa5dcc..e2be4f9 100644 --- a/cmd/enbas/errors.go +++ b/cmd/enbas/errors.go @@ -32,8 +32,8 @@ func (e unknownSubcommandError) Error() string { return "unknown subcommand '" + e.subcommand + "'" } -type noAccountIDsSpecifiedError struct{} +type noAccountSpecifiedError struct{} -func (e noAccountIDsSpecifiedError) Error() string { - return "no account IDs specified" +func (e noAccountSpecifiedError) Error() string { + return "no account specified in this request" } diff --git a/cmd/enbas/flags.go b/cmd/enbas/flags.go index 4856a45..df46ae5 100644 --- a/cmd/enbas/flags.go +++ b/cmd/enbas/flags.go @@ -2,13 +2,13 @@ package main import "strings" -type accountIDs []string +type accountNames []string -func (a *accountIDs) String() string { +func (a *accountNames) String() string { return strings.Join(*a, ", ") } -func (a *accountIDs) Set(value string) error { +func (a *accountNames) Set(value string) error { if len(value) > 0 { *a = append(*a, value) } diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 792e024..485f290 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -4,15 +4,10 @@ import ( "flag" "fmt" "os" - - "codeflow.dananglin.me.uk/apollo/enbas/internal/client" - "codeflow.dananglin.me.uk/apollo/enbas/internal/config" - "codeflow.dananglin.me.uk/apollo/enbas/internal/model" ) const ( - accountFlag = "account" - accountIDFlag = "account-id" + accountNameFlag = "account-name" addToFlag = "add-to" instanceFlag = "instance" listIDFlag = "list-id" @@ -151,19 +146,3 @@ func run() error { return nil } - -func getMyAccount(gts *client.Client) (model.Account, error) { - authConfig, err := config.NewAuthenticationConfigFromFile() - if err != nil { - return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration; %w", err) - } - - accountURI := authConfig.CurrentAccount - - account, err := gts.GetAccount(accountURI) - if err != nil { - return model.Account{}, fmt.Errorf("unable to retrieve the account details; %w", err) - } - - return account, nil -} diff --git a/cmd/enbas/remove.go b/cmd/enbas/remove.go index 0c79676..9d388b1 100644 --- a/cmd/enbas/remove.go +++ b/cmd/enbas/remove.go @@ -12,20 +12,20 @@ type removeCommand struct { fromResourceType string listID string - accountIDs accountIDs + accountNames accountNames } func newRemoveCommand(name, summary string) *removeCommand { emptyArr := make([]string, 0, 3) command := removeCommand{ - FlagSet: flag.NewFlagSet(name, flag.ExitOnError), - accountIDs: accountIDs(emptyArr), + FlagSet: flag.NewFlagSet(name, flag.ExitOnError), + accountNames: accountNames(emptyArr), } 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, accountIDFlag, "the ID of the account to remove from the list") + command.Var(&command.accountNames, accountNameFlag, "the name of the account to remove from the resource") command.Usage = commandUsageFunc(name, summary, command.FlagSet) @@ -34,7 +34,7 @@ func newRemoveCommand(name, summary string) *removeCommand { func (c *removeCommand) Execute() error { if c.fromResourceType == "" { - return flagNotSetError{flagText: "remove-from"} + return flagNotSetError{flagText: removeFromFlag} } funcMap := map[string]func(*client.Client) error{ @@ -59,11 +59,21 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error { return flagNotSetError{flagText: listIDFlag} } - if len(c.accountIDs) == 0 { - return noAccountIDsSpecifiedError{} + if len(c.accountNames) == 0 { + return noAccountSpecifiedError{} } - if err := gtsClient.RemoveAccountsFromList(c.listID, []string(c.accountIDs)); err != nil { + accountIDs := make([]string, len(c.accountNames)) + + for i := range c.accountNames { + accountID, err := getTheirAccountID(gtsClient, c.accountNames[i]) + if err != nil { + return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err) + } + accountIDs[i] = accountID + } + + if err := gtsClient.RemoveAccountsFromList(c.listID, accountIDs); err != nil { return fmt.Errorf("unable to remove the accounts from the list; %w", err) } diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index ad0939d..a52c035 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -15,8 +15,7 @@ type showCommand struct { showAccountRelationship bool showUserPreferences bool resourceType string - account string - accountID string + accountName string statusID string timelineCategory string listID string @@ -33,8 +32,7 @@ func newShowCommand(name, summary string) *showCommand { command.BoolVar(&command.showAccountRelationship, showAccountRelationshipFlag, false, "show your relationship to the specified account") command.BoolVar(&command.showUserPreferences, showUserPreferencesFlag, false, "show your preferences") command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to display") - command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup") - command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID") + command.StringVar(&command.accountName, accountNameFlag, "", "specify the account name in full (username@domain)") command.StringVar(&command.statusID, statusIDFlag, "", "specify the ID of the status to display") command.StringVar(&command.timelineCategory, timelineCategoryFlag, "home", "specify the type of timeline to display (valid values are home, public, list and tag)") command.StringVar(&command.listID, listIDFlag, "", "specify the ID of the list to display") @@ -95,18 +93,16 @@ func (c *showCommand) showAccount(gts *client.Client) error { if c.myAccount { account, err = getMyAccount(gts) if err != nil { - return fmt.Errorf("received an error while getting account details; %w", err) + return fmt.Errorf("received an error while getting the account details; %w", err) } } else { - if c.account == "" { - return flagNotSetError{flagText: accountFlag} + if c.accountName == "" { + return flagNotSetError{flagText: accountNameFlag} } - accountURI := c.account - - account, err = gts.GetAccount(accountURI) + account, err = getAccount(gts, c.accountName) if err != nil { - return fmt.Errorf("unable to retrieve the account details; %w", err) + return fmt.Errorf("received an error while getting the account details; %w", err) } } @@ -238,21 +234,9 @@ func (c *showCommand) showLists(gts *client.Client) error { } func (c *showCommand) showFollowers(gts *client.Client) error { - var accountID string - - if c.myAccount { - account, err := getMyAccount(gts) - if err != nil { - return fmt.Errorf("received an error while getting account details; %w", err) - } - - accountID = account.ID - } else { - if c.accountID == "" { - return flagNotSetError{flagText: accountIDFlag} - } - - accountID = c.accountID + accountID, err := getAccountID(gts, c.myAccount, c.accountName) + if err != nil { + return fmt.Errorf("received an error while getting the account ID; %w", err) } followers, err := gts.GetFollowers(accountID, c.limit) @@ -270,21 +254,9 @@ func (c *showCommand) showFollowers(gts *client.Client) error { } func (c *showCommand) showFollowing(gts *client.Client) error { - var accountID string - - if c.myAccount { - account, err := getMyAccount(gts) - if err != nil { - return fmt.Errorf("received an error while getting account details; %w", err) - } - - accountID = account.ID - } else { - if c.accountID == "" { - return flagNotSetError{flagText: accountIDFlag} - } - - accountID = c.accountID + accountID, err := getAccountID(gts, c.myAccount, c.accountName) + if err != nil { + return fmt.Errorf("received an error while getting the account ID; %w", err) } following, err := gts.GetFollowing(accountID, c.limit)