Dan Anglin
84091f398d
Summary: - Created a custom CLI schema for Enbas which will act as the Source of Truth for code and document generation. - Created a code generator which uses the schema to generate the executor definitions and code in the internal usage package. Changes: - Created the Enbas CLI schema as the Source of Truth for Enbas. - Created the code generator that generates the executor definitions and code in the usage package. - Regenerated the executor definitions using the code generator. - Moved the custom flag value types to the new internal flag package. - Created a new flag value type for the bool pointer to replace the flag.BoolFunc() used for the sensitive and no-color flags. - Moved the version and build variables to the new internal version package to simplify the version executor. - Created a new usage package and moved the usage functions there. - Changed the type of the account-name flag from string to the internal StringSliceValue type.
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package executor
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"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,
|
|
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 (
|
|
account model.Account
|
|
err error
|
|
)
|
|
|
|
switch {
|
|
case myAccount:
|
|
account, err = getMyAccount(gtsClient, credentialsFile)
|
|
if err != nil {
|
|
return account, fmt.Errorf("unable to get your account ID: %w", err)
|
|
}
|
|
case !accountNames.Empty():
|
|
account, err = getOtherAccount(gtsClient, accountNames)
|
|
if err != nil {
|
|
return account, fmt.Errorf("unable to get the account ID: %w", err)
|
|
}
|
|
default:
|
|
return account, NoAccountSpecifiedError{}
|
|
}
|
|
|
|
return account, nil
|
|
}
|
|
|
|
func getMyAccount(gtsClient *client.Client, path string) (model.Account, error) {
|
|
authConfig, err := config.NewCredentialsConfigFromFile(path)
|
|
if err != nil {
|
|
return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration: %w", err)
|
|
}
|
|
|
|
accountURI := authConfig.CurrentAccount
|
|
|
|
account, err := gtsClient.GetAccount(accountURI)
|
|
if err != nil {
|
|
return model.Account{}, fmt.Errorf("unable to retrieve your account: %w", err)
|
|
}
|
|
|
|
return account, nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
return account, nil
|
|
}
|
|
|
|
func getOtherAccounts(gtsClient *client.Client, accountNames internalFlag.StringSliceValue) ([]model.Account, error) {
|
|
numAccountNames := len(accountNames)
|
|
accounts := make([]model.Account, numAccountNames)
|
|
|
|
for ind := 0; ind < numAccountNames; ind++ {
|
|
var err error
|
|
|
|
accounts[ind], err = gtsClient.GetAccount(accountNames[ind])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to retrieve the account information for %s: %w", accountNames[ind], err)
|
|
}
|
|
}
|
|
|
|
return accounts, nil
|
|
}
|