checkpoint: refactored getAccount and getAccountID

This commit is contained in:
Dan Anglin 2024-08-13 11:54:54 +01:00
parent a070488352
commit 8cffac2074
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 36 additions and 60 deletions

View file

@ -10,53 +10,41 @@ import (
)
func getAccountID(gtsClient *client.Client, myAccount bool, accountNames internalFlag.StringSliceValue, credentialsFile string) (string, error) {
account, err := getAccount(gtsClient, myAccount, accountNames, credentialsFile)
if err != nil {
return "", fmt.Errorf("unable to get the account information: %w", err)
}
return account.ID, nil
}
func getAccount(
gtsClient *client.Client,
myAccount bool,
accountNames internalFlag.StringSliceValue,
credentialsFile string,
) (model.Account, error) {
var (
accountID string
account model.Account
err error
)
switch {
case myAccount:
accountID, err = getMyAccountID(gtsClient, credentialsFile)
account, err = getMyAccount(gtsClient, credentialsFile)
if err != nil {
return "", fmt.Errorf("unable to get your account ID: %w", err)
return account, fmt.Errorf("unable to get your account ID: %w", err)
}
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])
account, err = getOtherAccount(gtsClient, accountNames)
if err != nil {
return "", fmt.Errorf("unable to get the account ID: %w", err)
return account, fmt.Errorf("unable to get the account ID: %w", err)
}
default:
return "", NoAccountSpecifiedError{}
return account, NoAccountSpecifiedError{}
}
return accountID, nil
}
func getMyAccountID(gtsClient *client.Client, path string) (string, error) {
account, err := getMyAccount(gtsClient, path)
if err != nil {
return "", fmt.Errorf("received an error while getting your account details: %w", err)
}
return account.ID, nil
}
func getTheirAccountID(gtsClient *client.Client, accountURI string) (string, error) {
account, err := getAccount(gtsClient, accountURI)
if err != nil {
return "", fmt.Errorf("unable to retrieve your account: %w", err)
}
return account.ID, nil
return account, nil
}
func getMyAccount(gtsClient *client.Client, path string) (model.Account, error) {
@ -67,7 +55,7 @@ func getMyAccount(gtsClient *client.Client, path string) (model.Account, error)
accountURI := authConfig.CurrentAccount
account, err := getAccount(gtsClient, accountURI)
account, err := gtsClient.GetAccount(accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve your account: %w", err)
}
@ -75,8 +63,16 @@ func getMyAccount(gtsClient *client.Client, path string) (model.Account, error)
return account, nil
}
func getAccount(gtsClient *client.Client, accountURI string) (model.Account, error) {
account, err := gtsClient.GetAccount(accountURI)
func getOtherAccount(gtsClient *client.Client, accountNames internalFlag.StringSliceValue) (model.Account, error) {
expectedNumAccountNames := 1
if !accountNames.ExpectedLength(expectedNumAccountNames) {
return model.Account{}, fmt.Errorf(
"received an unexpected number of account names: want %d",
expectedNumAccountNames,
)
}
account, err := gtsClient.GetAccount(accountNames[0])
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the account details: %w", err)
}

View file

@ -59,29 +59,9 @@ func (s *ShowExecutor) showInstance(gtsClient *client.Client) error {
}
func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
var (
account model.Account
err error
)
if s.myAccount {
account, err = getMyAccount(gtsClient, s.config.CredentialsFile)
account, err := getAccount(gtsClient, s.myAccount, s.accountName, s.config.CredentialsFile)
if err != nil {
return fmt.Errorf("received an error while getting the account details: %w", err)
}
} else {
expectedNumAccountNames := 1
if !s.accountName.ExpectedLength(expectedNumAccountNames) {
return fmt.Errorf(
"found an unexpected number of --account-name flags: expected %d",
expectedNumAccountNames,
)
}
account, err = getAccount(gtsClient, s.accountName[0])
if err != nil {
return fmt.Errorf("received an error while getting the account details: %w", err)
}
return fmt.Errorf("unable to get the account information: %w", err)
}
if s.showInBrowser {