2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *SwitchExecutor) Execute() error {
|
|
|
|
funcMap := map[string]func() error{
|
|
|
|
resourceAccount: s.switchToAccount,
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
doFunc, ok := funcMap[s.to]
|
2024-05-23 18:06:49 +01:00
|
|
|
if !ok {
|
2024-08-13 14:53:26 +01:00
|
|
|
return UnsupportedTypeError{resourceType: s.to}
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SwitchExecutor) switchToAccount() error {
|
2024-08-13 14:53:26 +01:00
|
|
|
expectedNumAccountNames := 1
|
|
|
|
if !s.accountName.ExpectedLength(expectedNumAccountNames) {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"found an unexpected number of --account-name flags: expected %d",
|
|
|
|
expectedNumAccountNames,
|
|
|
|
)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
if err := config.UpdateCurrentAccount(s.accountName[0], s.config.CredentialsFile); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to switch account to the account: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
s.printer.PrintSuccess("The current account is now set to '" + s.accountName[0] + "'.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|