enbas/internal/executor/account.go
Dan Anglin 55b93c6586
fix(breaking): update project structure
Move all executors to the internal folder package. This PR also comes
with additional breaking changes.

Changes:

- refactor: move all executors to the internal/executor package.
- refactor: update naming patterns for constants, variables, custom
  types, etc.
- fix(breaking): renamed the update command to edit.
- fix(breaking): update the flags for the switch command to make it
  more generic.
- fix(breaking): renamed the show-account-relationship flag to
  show-relationship.
- fix: update the print message from the whoami command.
2024-05-23 18:06:49 +01:00

76 lines
2 KiB
Go

package executor
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(gtsClient *client.Client, myAccount bool, accountName, configDir string) (string, error) {
var (
accountID string
err error
)
switch {
case myAccount:
accountID, err = getMyAccountID(gtsClient, configDir)
if err != nil {
return "", fmt.Errorf("unable to get your account ID; %w", err)
}
case accountName != "":
accountID, err = getTheirAccountID(gtsClient, accountName)
if err != nil {
return "", fmt.Errorf("unable to get their account ID; %w", err)
}
default:
return "", NoAccountSpecifiedError{}
}
return accountID, 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 getMyAccountID(gtsClient *client.Client, configDir string) (string, error) {
account, err := getMyAccount(gtsClient, configDir)
if err != nil {
return "", fmt.Errorf("received an error while getting your account details; %w", err)
}
return account.ID, nil
}
func getMyAccount(gtsClient *client.Client, configDir string) (model.Account, error) {
authConfig, err := config.NewCredentialsConfigFromFile(configDir)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration; %w", err)
}
accountURI := authConfig.CurrentAccount
account, err := getAccount(gtsClient, accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve your account; %w", err)
}
return account, nil
}
func getAccount(gtsClient *client.Client, accountURI string) (model.Account, error) {
account, err := gtsClient.GetAccount(accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the account details; %w", err)
}
return account, nil
}