enbas/cmd/enbas-codegen/templates/executor/execute.go.gotmpl
Dan Anglin 3c8633ff04
All checks were successful
REUSE Compliance Check / check (push) Successful in 5s
refactor: minimise the main package
Minimise the main package by moving all flag parsing to the Execute
function.
2024-08-18 07:18:57 +01:00

133 lines
2.9 KiB
Go Template

{{- /* 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"
import "flag"
import "os"
import "codeflow.dananglin.me.uk/apollo/enbas/internal/config"
import internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
import "codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
import "codeflow.dananglin.me.uk/apollo/enbas/internal/usage"
{{ print "" }}
{{ print "" }}
func Execute() error {
var (
configDir string
noColorFlag internalFlag.BoolPtrValue
noColor bool
enbasConfig *config.Config
enbasPrinter *printer.Printer
err error
)
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:]
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 {
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
}