Dan Anglin
89e53bcc9f
Updated the executor lookup table by changing the map type from map[string]Executor to map[string]func(string, bool, []string) error in order to reduce the cost of initialising the map. Previously the map initialised all the executors despite only needing to run the single executor called by the command. The map now instead maps the command to the function of type 'func(string, bool, []string) error' that will initialise and run the executor called by the command. As a result of the change, the Executor interface is no longer needed and has been removed. PR: #57
174 lines
5 KiB
Go Template
174 lines
5 KiB
Go Template
/*
|
|
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 }}
|