2024-06-02 07:53:13 +01:00
|
|
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SwitchExecutor struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
|
|
|
|
topLevelFlags TopLevelFlags
|
|
|
|
toResourceType string
|
|
|
|
accountName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSwitchExecutor(tlf TopLevelFlags, name, summary string) *SwitchExecutor {
|
|
|
|
switchExe := SwitchExecutor{
|
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
topLevelFlags: tlf,
|
|
|
|
}
|
|
|
|
|
2024-06-09 15:58:10 +01:00
|
|
|
switchExe.StringVar(&switchExe.toResourceType, flagTo, "", "The account to switch to")
|
|
|
|
switchExe.StringVar(&switchExe.accountName, flagAccountName, "", "The name of the account to switch to")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
switchExe.Usage = commandUsageFunc(name, summary, switchExe.FlagSet)
|
|
|
|
|
|
|
|
return &switchExe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SwitchExecutor) Execute() error {
|
|
|
|
funcMap := map[string]func() error{
|
|
|
|
resourceAccount: s.switchToAccount,
|
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[s.toResourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: s.toResourceType}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SwitchExecutor) switchToAccount() error {
|
|
|
|
if s.accountName == "" {
|
|
|
|
return NoAccountSpecifiedError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.UpdateCurrentAccount(s.accountName, s.topLevelFlags.ConfigDir); 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
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("The current account is now set to %q.\n", s.accountName)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|