checkpoint: refactored getAccount and getAccountID
This commit is contained in:
parent
a070488352
commit
8cffac2074
2 changed files with 36 additions and 60 deletions
|
@ -10,53 +10,41 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func getAccountID(gtsClient *client.Client, myAccount bool, accountNames internalFlag.StringSliceValue, credentialsFile string) (string, error) {
|
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 (
|
var (
|
||||||
accountID string
|
account model.Account
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case myAccount:
|
case myAccount:
|
||||||
accountID, err = getMyAccountID(gtsClient, credentialsFile)
|
account, err = getMyAccount(gtsClient, credentialsFile)
|
||||||
if err != nil {
|
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():
|
case !accountNames.Empty():
|
||||||
expectedNumAccountNames := 1
|
account, err = getOtherAccount(gtsClient, accountNames)
|
||||||
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 {
|
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:
|
default:
|
||||||
return "", NoAccountSpecifiedError{}
|
return account, NoAccountSpecifiedError{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return accountID, nil
|
return account, 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMyAccount(gtsClient *client.Client, path string) (model.Account, error) {
|
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
|
accountURI := authConfig.CurrentAccount
|
||||||
|
|
||||||
account, err := getAccount(gtsClient, accountURI)
|
account, err := gtsClient.GetAccount(accountURI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Account{}, fmt.Errorf("unable to retrieve your account: %w", err)
|
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
|
return account, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAccount(gtsClient *client.Client, accountURI string) (model.Account, error) {
|
func getOtherAccount(gtsClient *client.Client, accountNames internalFlag.StringSliceValue) (model.Account, error) {
|
||||||
account, err := gtsClient.GetAccount(accountURI)
|
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 {
|
if err != nil {
|
||||||
return model.Account{}, fmt.Errorf("unable to retrieve the account details: %w", err)
|
return model.Account{}, fmt.Errorf("unable to retrieve the account details: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,29 +59,9 @@ func (s *ShowExecutor) showInstance(gtsClient *client.Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
|
func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
|
||||||
var (
|
account, err := getAccount(gtsClient, s.myAccount, s.accountName, s.config.CredentialsFile)
|
||||||
account model.Account
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if s.myAccount {
|
|
||||||
account, err = getMyAccount(gtsClient, s.config.CredentialsFile)
|
|
||||||
if err != nil {
|
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)
|
||||||
}
|
|
||||||
} 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.showInBrowser {
|
if s.showInBrowser {
|
||||||
|
|
Loading…
Reference in a new issue