checkpoint: removed interface, update template, hit a blocker

This commit is contained in:
Dan Anglin 2024-08-21 08:28:55 +01:00
parent 74f32aab53
commit 60079f9bf1
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 17 additions and 28 deletions

View file

@ -13,15 +13,9 @@ package executor
import internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag" import internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
import "codeflow.dananglin.me.uk/apollo/enbas/internal/usage" import "codeflow.dananglin.me.uk/apollo/enbas/internal/usage"
{{ print "" }} {{ print "" }}
{{ print "" }}
type Executor interface {
Name() string
Parse(args []string) error
Execute() error
}
{{ range $name, $command := . }} {{ range $name, $command := . }}
{{- $struct_name := capitalise $name | printf "%sExecutor" -}} {{- $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 "" }} {{ print "" }}
// {{ $struct_name }} is the executor for the {{ $name }} command. // {{ $struct_name }} is the executor for the {{ $name }} command.
type {{ $struct_name }} struct { type {{ $struct_name }} struct {
@ -49,27 +43,16 @@ type {{ $struct_name }} struct {
{{ print "" }} {{ print "" }}
} }
func {{ $new_executor_function_name }}( // {{ $execute_command_function_name }} initialises and runs the executor for the {{ $name }} command.
{{- if $command.UsePrinter }} func {{ $execute_command_function_name }}(
printer *printer.Printer, printer *printer.Printer,
{{- end }}
{{- if $command.UseConfig }}
config *config.Config, config *config.Config,
{{- end }} args []string,
{{- range $field := $command.AdditionalFields -}} ) error {
{{ print "" }}
{{ $field.Name }} {{ $field.Type }},
{{- end -}}
{{ print "" }}
) *{{ $struct_name }} {
exe := {{ $struct_name }}{ exe := {{ $struct_name }}{
FlagSet: flag.NewFlagSet({{ printf "%q" $name }}, flag.ExitOnError), FlagSet: flag.NewFlagSet({{ printf "%q" $name }}, flag.ExitOnError),
{{- if $command.UsePrinter }}
printer: printer, printer: printer,
{{- end }}
{{- if $command.UseConfig }}
config: config, config: config,
{{- end }}
{{- range $flag := $command.Flags -}} {{- range $flag := $command.Flags -}}
{{- $flag_type := getFlagType $flag.Flag -}} {{- $flag_type := getFlagType $flag.Flag -}}
{{- if internalFlagValue $flag_type -}} {{- if internalFlagValue $flag_type -}}
@ -77,10 +60,6 @@ func {{ $new_executor_function_name }}(
{{ flagFieldName $flag }}: internalFlag.New{{ $flag_type }}(), {{ flagFieldName $flag }}: internalFlag.New{{ $flag_type }}(),
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- range $field := $command.AdditionalFields -}}
{{ print "" }}
{{ $field.Name }}: {{ $field.Name }},
{{- end -}}
{{ print "" }} {{ print "" }}
} }
{{ print "" }} {{ print "" }}
@ -104,6 +83,16 @@ func {{ $new_executor_function_name }}(
{{- end -}} {{- end -}}
{{ print "" }} {{ print "" }}
{{ 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 }} {{ end }}

View file

@ -318,7 +318,7 @@
], ],
"flags": [], "flags": [],
"summary": "Creates a new configuration file in the specified configuration directory", "summary": "Creates a new configuration file in the specified configuration directory",
"useConfig": false, "useConfig": true,
"usePrinter": true "usePrinter": true
}, },
"login": { "login": {