diff --git a/cmd/enbas-codegen/templates/executor/executors.go.gotmpl b/cmd/enbas-codegen/templates/executor/executors.go.gotmpl index f95b146..e4beb74 100644 --- a/cmd/enbas-codegen/templates/executor/executors.go.gotmpl +++ b/cmd/enbas-codegen/templates/executor/executors.go.gotmpl @@ -13,15 +13,9 @@ package executor import internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag" import "codeflow.dananglin.me.uk/apollo/enbas/internal/usage" {{ print "" }} -{{ print "" }} -type Executor interface { - Name() string - Parse(args []string) error - Execute() error -} {{ range $name, $command := . }} {{- $struct_name := capitalise $name | printf "%sExecutor" -}} -{{- $new_executor_function_name := capitalise $name | printf "New%sExecutor" -}} +{{- $execute_command_function_name := capitalise $name | printf "Execute%sCommand" -}} {{ print "" }} // {{ $struct_name }} is the executor for the {{ $name }} command. type {{ $struct_name }} struct { @@ -49,27 +43,16 @@ type {{ $struct_name }} struct { {{ print "" }} } -func {{ $new_executor_function_name }}( - {{- if $command.UsePrinter }} +// {{ $execute_command_function_name }} initialises and runs the executor for the {{ $name }} command. +func {{ $execute_command_function_name }}( printer *printer.Printer, - {{- end }} - {{- if $command.UseConfig }} config *config.Config, - {{- end }} - {{- range $field := $command.AdditionalFields -}} - {{ print "" }} - {{ $field.Name }} {{ $field.Type }}, - {{- end -}} - {{ print "" }} -) *{{ $struct_name }} { + args []string, +) error { exe := {{ $struct_name }}{ FlagSet: flag.NewFlagSet({{ printf "%q" $name }}, flag.ExitOnError), - {{- if $command.UsePrinter }} printer: printer, - {{- end }} - {{- if $command.UseConfig }} config: config, - {{- end }} {{- range $flag := $command.Flags -}} {{- $flag_type := getFlagType $flag.Flag -}} {{- if internalFlagValue $flag_type -}} @@ -77,10 +60,6 @@ func {{ $new_executor_function_name }}( {{ flagFieldName $flag }}: internalFlag.New{{ $flag_type }}(), {{- end -}} {{- end -}} - {{- range $field := $command.AdditionalFields -}} - {{ print "" }} - {{ $field.Name }}: {{ $field.Name }}, - {{- end -}} {{ print "" }} } {{ print "" }} @@ -104,6 +83,16 @@ func {{ $new_executor_function_name }}( {{- end -}} {{ print "" }} {{ print "" }} - return &exe + if err := exe.Parse(args); err != nil { + return fmt.Errorf("flag parsing error: %w", err) + } + {{ print "" }} + {{ print "" }} + if err := exe.Execute(); err != nil { + return fmt.Errorf("execution error: %w", err) + } + {{ print "" }} + {{ print "" }} + return nil } {{ end }} diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index caec28f..54a798b 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -318,7 +318,7 @@ ], "flags": [], "summary": "Creates a new configuration file in the specified configuration directory", - "useConfig": false, + "useConfig": true, "usePrinter": true }, "login": {