diff --git a/internal/executor/accept.go b/internal/executor/accept.go index 9cbd8c2..158927d 100644 --- a/internal/executor/accept.go +++ b/internal/executor/accept.go @@ -25,15 +25,7 @@ func (a *AcceptExecutor) Execute() error { } func (a *AcceptExecutor) acceptFollowRequest(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !a.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, a.accountName[0], a.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, a.accountName, a.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/account.go b/internal/executor/account.go index 4185d3e..5e88e5b 100644 --- a/internal/executor/account.go +++ b/internal/executor/account.go @@ -5,10 +5,11 @@ import ( "codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/config" + internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag" "codeflow.dananglin.me.uk/apollo/enbas/internal/model" ) -func getAccountID(gtsClient *client.Client, myAccount bool, accountName, path string) (string, error) { +func getAccountID(gtsClient *client.Client, myAccount bool, accountNames internalFlag.StringSliceValue, credentialsFile string) (string, error) { var ( accountID string err error @@ -16,14 +17,22 @@ func getAccountID(gtsClient *client.Client, myAccount bool, accountName, path st switch { case myAccount: - accountID, err = getMyAccountID(gtsClient, path) + accountID, err = getMyAccountID(gtsClient, credentialsFile) if err != nil { return "", fmt.Errorf("unable to get your account ID: %w", err) } - case accountName != "": - accountID, err = getTheirAccountID(gtsClient, accountName) + case !accountNames.Empty(): + expectedNumAccountNames := 1 + if !accountNames.ExpectedLength(expectedNumAccountNames) { + return "", fmt.Errorf( + "received an unexpected number of account names: want %d", + expectedNumAccountNames, + ) + } + + accountID, err = getTheirAccountID(gtsClient, accountNames[0]) if err != nil { - return "", fmt.Errorf("unable to get their account ID: %w", err) + return "", fmt.Errorf("unable to get the account ID: %w", err) } default: return "", NoAccountSpecifiedError{} @@ -32,19 +41,19 @@ func getAccountID(gtsClient *client.Client, myAccount bool, accountName, path st return accountID, nil } -func getTheirAccountID(gtsClient *client.Client, accountURI string) (string, error) { - account, err := getAccount(gtsClient, accountURI) +func getMyAccountID(gtsClient *client.Client, path string) (string, error) { + account, err := getMyAccount(gtsClient, path) if err != nil { - return "", fmt.Errorf("unable to retrieve your account: %w", err) + return "", fmt.Errorf("received an error while getting your account details: %w", err) } return account.ID, nil } -func getMyAccountID(gtsClient *client.Client, path string) (string, error) { - account, err := getMyAccount(gtsClient, path) +func getTheirAccountID(gtsClient *client.Client, accountURI string) (string, error) { + account, err := getAccount(gtsClient, accountURI) if err != nil { - return "", fmt.Errorf("received an error while getting your account details: %w", err) + return "", fmt.Errorf("unable to retrieve your account: %w", err) } return account.ID, nil diff --git a/internal/executor/add.go b/internal/executor/add.go index fd5e5e7..b72b3a4 100644 --- a/internal/executor/add.go +++ b/internal/executor/add.go @@ -104,15 +104,7 @@ func (a *AddExecutor) addToAccount(gtsClient *client.Client) error { } func (a *AddExecutor) addNoteToAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !a.accountNames.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, a.accountNames[0], a.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, a.accountNames, a.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/block.go b/internal/executor/block.go index ea7729e..1b0eeae 100644 --- a/internal/executor/block.go +++ b/internal/executor/block.go @@ -25,15 +25,7 @@ func (b *BlockExecutor) Execute() error { } func (b *BlockExecutor) blockAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !b.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, b.accountName[0], b.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/follow.go b/internal/executor/follow.go index 9834d40..b76ca09 100644 --- a/internal/executor/follow.go +++ b/internal/executor/follow.go @@ -25,11 +25,7 @@ func (f *FollowExecutor) Execute() error { } func (f *FollowExecutor) followAccount(gtsClient *client.Client) error { - if !f.accountName.ExpectedLength(1) { - return fmt.Errorf("found an unexpected number of %s flags: expected %d", "--account-name", 1) - } - - accountID, err := getAccountID(gtsClient, false, f.accountName[0], f.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, f.accountName, f.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/mute.go b/internal/executor/mute.go index fec24e3..73d8f50 100644 --- a/internal/executor/mute.go +++ b/internal/executor/mute.go @@ -25,15 +25,7 @@ func (m *MuteExecutor) Execute() error { } func (m *MuteExecutor) muteAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !m.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, m.accountName[0], m.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, m.accountName, m.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/reject.go b/internal/executor/reject.go index e4114cf..adaf804 100644 --- a/internal/executor/reject.go +++ b/internal/executor/reject.go @@ -25,15 +25,7 @@ func (r *RejectExecutor) Execute() error { } func (r *RejectExecutor) rejectFollowRequest(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !r.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, r.accountName[0], r.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, r.accountName, r.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/remove.go b/internal/executor/remove.go index 616105d..f03060b 100644 --- a/internal/executor/remove.go +++ b/internal/executor/remove.go @@ -93,15 +93,7 @@ func (r *RemoveExecutor) removeFromAccount(gtsClient *client.Client) error { } func (r *RemoveExecutor) removeNoteFromAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !r.accountNames.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, r.accountNames[0], r.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, r.accountNames, r.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/show.go b/internal/executor/show.go index 2780bbc..ef99f3a 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -77,6 +77,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error { expectedNumAccountNames, ) } + account, err = getAccount(gtsClient, s.accountName[0]) if err != nil { return fmt.Errorf("received an error while getting the account details: %w", err) @@ -272,15 +273,7 @@ func (s *ShowExecutor) showFollowers(gtsClient *client.Client) error { } func (s *ShowExecutor) showFollowersFromAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !s.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName[0], s.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName, s.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } @@ -320,15 +313,7 @@ func (s *ShowExecutor) showFollowing(gtsClient *client.Client) error { } func (s *ShowExecutor) showFollowingFromAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !s.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName[0], s.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName, s.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/unblock.go b/internal/executor/unblock.go index 4559e7c..831102f 100644 --- a/internal/executor/unblock.go +++ b/internal/executor/unblock.go @@ -25,15 +25,7 @@ func (b *UnblockExecutor) Execute() error { } func (b *UnblockExecutor) unblockAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !b.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, b.accountName[0], b.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/unfollow.go b/internal/executor/unfollow.go index 308dcd6..102ce3d 100644 --- a/internal/executor/unfollow.go +++ b/internal/executor/unfollow.go @@ -25,15 +25,7 @@ func (f *UnfollowExecutor) Execute() error { } func (f *UnfollowExecutor) unfollowAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !f.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, f.accountName[0], f.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, f.accountName, f.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/unmute.go b/internal/executor/unmute.go index 59f3765..c4f4e83 100644 --- a/internal/executor/unmute.go +++ b/internal/executor/unmute.go @@ -25,15 +25,7 @@ func (m *UnmuteExecutor) Execute() error { } func (m *UnmuteExecutor) unmuteAccount(gtsClient *client.Client) error { - expectedNumAccountNames := 1 - if !m.accountName.ExpectedLength(expectedNumAccountNames) { - return fmt.Errorf( - "found an unexpected number of --account-name flags: expected %d", - expectedNumAccountNames, - ) - } - - accountID, err := getAccountID(gtsClient, false, m.accountName[0], m.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, m.accountName, m.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index 68cd55f..b441830 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -58,7 +58,7 @@ }, "from": { "type": "string", - "description": "Specify the resource type to action the target resource from" + "description": "The resource type to action the target resource from (e.g. status)" }, "from-file": { "type": "string", @@ -182,7 +182,7 @@ }, "to": { "type": "string", - "description": "TBC" + "description": "The resource type to action the target resource to (e.g. status)" }, "type": { "type": "string",