checkpoint: updated getAccountID to process accountNames with new StringSliceValue type

This commit is contained in:
Dan Anglin 2024-08-13 11:20:11 +01:00
parent a95847ad60
commit a070488352
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
13 changed files with 35 additions and 117 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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