enbas/internal/executor/execute.go
Dan Anglin 84091f398d
feat: add Enbas CLI schema and code generator
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.
2024-08-13 14:53:26 +01:00

160 lines
2.8 KiB
Go

/*
This file is generated by the enbas-codegen
DO NOT EDIT.
*/
package executor
import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
)
func Execute(
command string,
args []string,
noColor bool,
configDir string,
) error {
var (
enbasConfig *config.Config
enbasPrinter *printer.Printer
err error
)
switch command {
case "init", "version":
enbasPrinter = printer.NewPrinter(noColor, "", 0)
default:
enbasConfig, err = config.NewConfigFromFile(configDir)
if err != nil {
enbasPrinter = printer.NewPrinter(noColor, "", 0)
enbasPrinter.PrintFailure("unable to load the configuration: " + err.Error() + ".")
return err
}
enbasPrinter = printer.NewPrinter(
noColor,
enbasConfig.Integrations.Pager,
enbasConfig.LineWrapMaxWidth,
)
}
if err = execute(
command,
args,
enbasPrinter,
enbasConfig,
configDir,
); err != nil {
enbasPrinter.PrintFailure("(" + command + ") " + err.Error() + ".")
return err
}
return nil
}
func execute(
command string,
args []string,
enbasPrinter *printer.Printer,
enbasConfig *config.Config,
configDir string,
) error {
executorMap := map[string]Executor{
"accept": NewAcceptExecutor(
enbasPrinter,
enbasConfig,
),
"add": NewAddExecutor(
enbasPrinter,
enbasConfig,
),
"block": NewBlockExecutor(
enbasPrinter,
enbasConfig,
),
"create": NewCreateExecutor(
enbasPrinter,
enbasConfig,
),
"delete": NewDeleteExecutor(
enbasPrinter,
enbasConfig,
),
"edit": NewEditExecutor(
enbasPrinter,
enbasConfig,
),
"follow": NewFollowExecutor(
enbasPrinter,
enbasConfig,
),
"init": NewInitExecutor(
enbasPrinter,
configDir,
),
"login": NewLoginExecutor(
enbasPrinter,
enbasConfig,
),
"mute": NewMuteExecutor(
enbasPrinter,
enbasConfig,
),
"reject": NewRejectExecutor(
enbasPrinter,
enbasConfig,
),
"remove": NewRemoveExecutor(
enbasPrinter,
enbasConfig,
),
"show": NewShowExecutor(
enbasPrinter,
enbasConfig,
),
"switch": NewSwitchExecutor(
enbasPrinter,
enbasConfig,
),
"unblock": NewUnblockExecutor(
enbasPrinter,
enbasConfig,
),
"unfollow": NewUnfollowExecutor(
enbasPrinter,
enbasConfig,
),
"unmute": NewUnmuteExecutor(
enbasPrinter,
enbasConfig,
),
"version": NewVersionExecutor(
enbasPrinter,
),
"whoami": NewWhoamiExecutor(
enbasPrinter,
enbasConfig,
),
}
exe, ok := executorMap[command]
if !ok {
return UnknownCommandError{Command: command}
}
if err := exe.Parse(args); err != nil {
return fmt.Errorf("flag parsing error: %w", err)
}
if err := exe.Execute(); err != nil {
return fmt.Errorf("execution error: %w", err)
}
return nil
}