178 lines
5.2 KiB
Go Template
178 lines
5.2 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
|
|
)
|
|
|
|
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:]
|
|
|
|
executorMap := map[string]func(string, bool, []string) error {
|
|
{{- range $name, $command := . -}}
|
|
{{- $execute_command_function_name := capitalise $name | printf "Execute%sCommand" -}}
|
|
{{ print "" }}
|
|
{{ printf "%q" $name }}: {{ $execute_command_function_name }},
|
|
{{- end -}}
|
|
{{ print "" }}
|
|
}
|
|
|
|
executorFunc, ok := executorMap[command]
|
|
if !ok {
|
|
err := UnknownCommandError{command: command}
|
|
printer.NewPrinter(noColor, "", 0).PrintFailure("Error: "+err.Error())
|
|
|
|
return err
|
|
}
|
|
|
|
return executorFunc(configDir, noColor, args)
|
|
}
|
|
{{ print "" }}
|
|
{{ range $name, $command := . }}
|
|
{{- $struct_name := capitalise $name | printf "%sExecutor" -}}
|
|
{{- $execute_command_function_name := capitalise $name | printf "Execute%sCommand" -}}
|
|
{{ print "" }}
|
|
// {{ $struct_name }} is the executor for the {{ $name }} command.
|
|
type {{ $struct_name }} struct {
|
|
*flag.FlagSet
|
|
printer *printer.Printer
|
|
config *config.Config
|
|
{{- range $flag := $command.Flags -}}
|
|
{{- $flag_type := getFlagType $flag.Flag -}}
|
|
{{- if internalFlagValue $flag_type -}}
|
|
{{ print "" }}
|
|
{{ flagFieldName $flag }} internalFlag.{{ $flag_type }}
|
|
{{- else -}}
|
|
{{ print "" }}
|
|
{{ flagFieldName $flag }} {{ $flag_type }}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- range $field := $command.AdditionalFields -}}
|
|
{{ print "" }}
|
|
{{ $field.Name }} {{ $field.Type }}
|
|
{{- end -}}
|
|
{{ print "" }}
|
|
}
|
|
|
|
// {{ $execute_command_function_name }} initialises and runs the executor for the {{ $name }} command.
|
|
func {{ $execute_command_function_name }}(
|
|
configDir string,
|
|
noColor bool,
|
|
args []string,
|
|
) error {
|
|
exe := {{ $struct_name }}{
|
|
FlagSet: flag.NewFlagSet({{ printf "%q" $name }}, flag.ExitOnError),
|
|
printer: nil,
|
|
config: nil,
|
|
configDir: configDir,
|
|
{{- range $flag := $command.Flags -}}
|
|
{{- $flag_type := getFlagType $flag.Flag -}}
|
|
{{- if internalFlagValue $flag_type -}}
|
|
{{ print "" }}
|
|
{{ flagFieldName $flag }}: internalFlag.New{{ $flag_type }}(),
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{ print "" }}
|
|
}
|
|
{{ print "" }}
|
|
exe.Usage = usage.ExecutorUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, exe.FlagSet)
|
|
{{ print "" }}
|
|
{{- range $flag := $command.Flags -}}
|
|
{{- $flag_type := getFlagType $flag.Flag -}}
|
|
{{- if eq $flag_type "string" -}}
|
|
{{ print "" }}
|
|
exe.StringVar(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ printf "%q" $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
|
|
{{- else if eq $flag_type "bool" -}}
|
|
{{ print "" }}
|
|
exe.BoolVar(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
|
|
{{- else if eq $flag_type "int" -}}
|
|
{{ print "" }}
|
|
exe.IntVar(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
|
|
{{- else if internalFlagValue $flag_type -}}
|
|
{{ print "" }}
|
|
exe.Var(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- if $command.UseConfig -}}
|
|
{{ print "" }}
|
|
{{ print "" }}
|
|
// Load the configuration from file.
|
|
exeConfig, err := config.NewConfigFromFile(exe.configDir)
|
|
if err != nil {
|
|
printer.NewPrinter(noColor, "", 0).PrintFailure("({{ $name }}) unable to load configuration: " + err.Error() + ".")
|
|
|
|
return err
|
|
}
|
|
exe.config = exeConfig
|
|
|
|
// Create the printer for the executor.
|
|
exe.printer = printer.NewPrinter(
|
|
noColor,
|
|
exe.config.Integrations.Pager,
|
|
exe.config.LineWrapMaxWidth,
|
|
)
|
|
{{- else -}}
|
|
{{ print "" }}
|
|
{{ print "" }}
|
|
// Create the printer for the executor.
|
|
exe.printer = printer.NewPrinter(noColor, "", 0)
|
|
{{- end -}}
|
|
{{ print "" }}
|
|
{{ print "" }}
|
|
// Parse the remaining arguments.
|
|
if err := exe.Parse(args); err != nil {
|
|
exe.printer.PrintFailure("({{ $name }}) flag parsing error: " + err.Error() + ".")
|
|
|
|
return err
|
|
}
|
|
|
|
// Run the executor.
|
|
if err := exe.Execute(); err != nil {
|
|
exe.printer.PrintFailure("({{ $name }}) execution error: " + err.Error() + ".")
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
{{ end }}
|