2024-08-13 14:53:26 +01:00
|
|
|
{{- /* vim: set noexpandtab : */ -}}
|
|
|
|
{{- /* vim: set tabstop=8 : */ -}}
|
|
|
|
{{- /* vim: set shiftwidth=8 : */ -}}
|
|
|
|
{{- /* vim: set softtabstop=8 : */ -}}
|
|
|
|
/*
|
|
|
|
This file is generated by the enbas-codegen
|
|
|
|
DO NOT EDIT.
|
|
|
|
*/
|
|
|
|
{{ print "" }}
|
|
|
|
package executor
|
|
|
|
{{ print "" }}
|
|
|
|
{{ print "" }}
|
|
|
|
import "fmt"
|
2024-08-18 07:18:57 +01:00
|
|
|
import "flag"
|
|
|
|
import "os"
|
2024-08-13 14:53:26 +01:00
|
|
|
import "codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
2024-08-18 07:18:57 +01:00
|
|
|
import internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
|
2024-08-13 14:53:26 +01:00
|
|
|
import "codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
|
2024-08-18 07:18:57 +01:00
|
|
|
import "codeflow.dananglin.me.uk/apollo/enbas/internal/usage"
|
2024-08-13 14:53:26 +01:00
|
|
|
{{ print "" }}
|
|
|
|
{{ print "" }}
|
2024-08-18 07:18:57 +01:00
|
|
|
func Execute() error {
|
2024-08-13 14:53:26 +01:00
|
|
|
var (
|
2024-08-18 07:18:57 +01:00
|
|
|
configDir string
|
|
|
|
noColorFlag internalFlag.BoolPtrValue
|
|
|
|
noColor bool
|
2024-08-13 14:53:26 +01:00
|
|
|
enbasConfig *config.Config
|
|
|
|
enbasPrinter *printer.Printer
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2024-08-18 07:18:57 +01:00
|
|
|
flag.StringVar(&configDir, "config-dir", "", "The path to your configuration directory")
|
|
|
|
flag.Var(&noColorFlag, "no-color", "Set to true to disable ANSI colour output when displaying text on screen")
|
|
|
|
|
|
|
|
flag.Usage = usage.AppUsageFunc()
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if flag.NArg() < 1 {
|
|
|
|
flag.Usage()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if noColorFlag.Value != nil {
|
|
|
|
noColor = *noColorFlag.Value
|
|
|
|
} else if os.Getenv("NO_COLOR") != "" {
|
|
|
|
noColor = true
|
|
|
|
}
|
|
|
|
|
|
|
|
command := flag.Arg(0)
|
|
|
|
args := flag.Args()[1:]
|
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
{{ print "" }}
|
|
|
|
{{ print "" }}
|
|
|
|
func execute(
|
|
|
|
command string,
|
|
|
|
args []string,
|
|
|
|
enbasPrinter *printer.Printer,
|
|
|
|
enbasConfig *config.Config,
|
|
|
|
configDir string,
|
|
|
|
) error {
|
|
|
|
executorMap := map[string]Executor{
|
|
|
|
{{- range $name, $command := . -}}
|
|
|
|
{{- $new_executor_function_name := capitalise $name | printf "New%sExecutor" -}}
|
|
|
|
{{ print "" }}
|
|
|
|
{{ printf "%q" $name }}: {{ $new_executor_function_name }}(
|
|
|
|
{{- if $command.UsePrinter -}}
|
|
|
|
{{ print "" }}
|
|
|
|
enbasPrinter,
|
|
|
|
{{- end -}}
|
|
|
|
{{- if $command.UseConfig -}}
|
|
|
|
{{ print "" }}
|
|
|
|
enbasConfig,
|
|
|
|
{{- end -}}
|
|
|
|
{{- range $field := $command.AdditionalFields -}}
|
|
|
|
{{ print "" }}
|
|
|
|
{{ $field.Name }},
|
|
|
|
{{- end -}}
|
|
|
|
{{ print "" }}
|
|
|
|
),
|
|
|
|
{{- end -}}
|
|
|
|
{{ print "" }}
|
|
|
|
}
|
|
|
|
|
|
|
|
exe, ok := executorMap[command]
|
|
|
|
if !ok {
|
2024-08-18 07:18:57 +01:00
|
|
|
return UnknownCommandError{command: command}
|
2024-08-13 14:53:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|