checkpoint: optionally set printer and config

This commit is contained in:
Dan Anglin 2024-08-09 17:35:44 +01:00
parent 5b8a407a3f
commit dee32cc27c
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 27 additions and 25 deletions

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -20,7 +19,7 @@ func main() {
flag.StringVar(&executorsFilePath, "path-to-enbas-executors", "", "The path to the executors Go file") flag.StringVar(&executorsFilePath, "path-to-enbas-executors", "", "The path to the executors Go file")
flag.Parse() flag.Parse()
schema, err := readSchemaFile(enbasCLISchemaFilepath) schema, err := newEnbasCLISchemaFromFile(enbasCLISchemaFilepath)
if err != nil { if err != nil {
fmt.Printf("ERROR: Unable to read the schema file: %v.\n", err) 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 { func generateExecutors(schema enbasCLISchema, output string) error {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"capitalise": capitalise, "capitalise": capitalise,

View file

@ -53,8 +53,8 @@ func (e enbasCLISchemaFlagMap) getDescription(name string) string {
} }
type enbasCLISchemaCommand struct { type enbasCLISchemaCommand struct {
AddPrinter bool `json:"addPrinter"` UsePrinter bool `json:"usePrinter"`
AddConfig bool `json:"addConfig"` UseConfig bool `json:"useConfig"`
Flags []enbasCLISchemaFlagReference `json:"flags"` Flags []enbasCLISchemaFlagReference `json:"flags"`
Summary string `json:"summary"` Summary string `json:"summary"`
} }

View file

@ -8,6 +8,12 @@ var executorsFileTemplate = `package executor
// {{ $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 {
*flag.FlagSet *flag.FlagSet
{{- if $command.UsePrinter }}
printer *printer.Printer
{{- end }}
{{- if $command.UseConfig }}
config *config.Config
{{- end }}
{{- range $flag := $command.Flags -}} {{- range $flag := $command.Flags -}}
{{ print "" }} {{ print "" }}
{{ $flag.Flag }} {{ getFlagType $flag.Flag }} {{ $flag.Flag }} {{ getFlagType $flag.Flag }}
@ -15,9 +21,22 @@ type {{ $struct_name }} struct {
{{ print "" }} {{ 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 }}{ exe := {{ $struct_name }}{
FlagSet: flag.NewFlagSet({{ printf "%q" $name }}, flag.ExitOnError), FlagSet: flag.NewFlagSet({{ printf "%q" $name }}, flag.ExitOnError),
{{- if $command.UsePrinter }}
printer: printer,
{{- end }}
{{- if $command.UseConfig }}
config: config,
{{- end }}
} }
{{ print "" }} {{ print "" }}
exe.Usage = commandUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, showExe.FlagSet) exe.Usage = commandUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, showExe.FlagSet)

View file

@ -12,16 +12,16 @@
"commands": { "commands": {
"login": { "login": {
"addPrinter": true, "usePrinter": true,
"addConfig": true, "useConfig": true,
"flags": [ "flags": [
{ "flag": "instance", "default": "" } { "flag": "instance", "default": "" }
], ],
"summary": "Login to an account on GoToSocial" "summary": "Login to an account on GoToSocial"
}, },
"version": { "version": {
"addPrinter": true, "usePrinter": true,
"addConfig": false, "useConfig": false,
"summary": "Prints the application's version and build information", "summary": "Prints the application's version and build information",
"flags": [ "flags": [
{ "flag": "full", "default": "false" } { "flag": "full", "default": "false" }