From dee32cc27cd1c8c14b464312c66f83476abd1012 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 9 Aug 2024 17:35:44 +0100 Subject: [PATCH] checkpoint: optionally set printer and config --- cmd/enbas-cli-generators/main.go | 19 +------------------ cmd/enbas-cli-generators/schema.go | 4 ++-- cmd/enbas-cli-generators/templates.go | 21 ++++++++++++++++++++- schema/enbas_cli_schema.json | 8 ++++---- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/cmd/enbas-cli-generators/main.go b/cmd/enbas-cli-generators/main.go index a766e93..e68322b 100644 --- a/cmd/enbas-cli-generators/main.go +++ b/cmd/enbas-cli-generators/main.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "flag" "fmt" "os" @@ -20,7 +19,7 @@ func main() { flag.StringVar(&executorsFilePath, "path-to-enbas-executors", "", "The path to the executors Go file") flag.Parse() - schema, err := readSchemaFile(enbasCLISchemaFilepath) + schema, err := newEnbasCLISchemaFromFile(enbasCLISchemaFilepath) if err != nil { fmt.Printf("ERROR: Unable to read the schema file: %v.\n", err) } @@ -34,22 +33,6 @@ func main() { } } -func readSchemaFile(path string) (enbasCLISchema, error) { - file, err := os.Open(path) - if err != nil { - return enbasCLISchema{}, fmt.Errorf("unable to open the schema file: %w", err) - } - defer file.Close() - - var schema enbasCLISchema - - if err := json.NewDecoder(file).Decode(&schema); err != nil { - return enbasCLISchema{}, fmt.Errorf("unable to decode the JSON data: %w", err) - } - - return schema, nil -} - func generateExecutors(schema enbasCLISchema, output string) error { funcMap := template.FuncMap{ "capitalise": capitalise, diff --git a/cmd/enbas-cli-generators/schema.go b/cmd/enbas-cli-generators/schema.go index 6d18c5b..a444590 100644 --- a/cmd/enbas-cli-generators/schema.go +++ b/cmd/enbas-cli-generators/schema.go @@ -53,8 +53,8 @@ func (e enbasCLISchemaFlagMap) getDescription(name string) string { } type enbasCLISchemaCommand struct { - AddPrinter bool `json:"addPrinter"` - AddConfig bool `json:"addConfig"` + UsePrinter bool `json:"usePrinter"` + UseConfig bool `json:"useConfig"` Flags []enbasCLISchemaFlagReference `json:"flags"` Summary string `json:"summary"` } diff --git a/cmd/enbas-cli-generators/templates.go b/cmd/enbas-cli-generators/templates.go index b8596e5..d0e62b1 100644 --- a/cmd/enbas-cli-generators/templates.go +++ b/cmd/enbas-cli-generators/templates.go @@ -8,6 +8,12 @@ var executorsFileTemplate = `package executor // {{ $struct_name }} is the executor for the {{ $name }} command. type {{ $struct_name }} struct { *flag.FlagSet + {{- if $command.UsePrinter }} + printer *printer.Printer + {{- end }} + {{- if $command.UseConfig }} + config *config.Config + {{- end }} {{- range $flag := $command.Flags -}} {{ print "" }} {{ $flag.Flag }} {{ getFlagType $flag.Flag }} @@ -15,9 +21,22 @@ type {{ $struct_name }} struct { {{ print "" }} } -func {{ $new_executor_function_name }}() *{{ $struct_name }} { +func {{ $new_executor_function_name }}( + {{- if $command.UsePrinter }} + printer *printer.Printer, + {{- end }} + {{- if $command.UseConfig }} + config *config.Config, + {{- end }} +) *{{ $struct_name }} { exe := {{ $struct_name }}{ FlagSet: flag.NewFlagSet({{ printf "%q" $name }}, flag.ExitOnError), + {{- if $command.UsePrinter }} + printer: printer, + {{- end }} + {{- if $command.UseConfig }} + config: config, + {{- end }} } {{ print "" }} exe.Usage = commandUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, showExe.FlagSet) diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index 30c1d90..fb50881 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -12,16 +12,16 @@ "commands": { "login": { - "addPrinter": true, - "addConfig": true, + "usePrinter": true, + "useConfig": true, "flags": [ { "flag": "instance", "default": "" } ], "summary": "Login to an account on GoToSocial" }, "version": { - "addPrinter": true, - "addConfig": false, + "usePrinter": true, + "useConfig": false, "summary": "Prints the application's version and build information", "flags": [ { "flag": "full", "default": "false" }