Compare commits
No commits in common. "d1f5e577037f8aa29c9e9bcf5ae048c9dc30503d" and "5b8a407a3f168b3db4e2f3db702084f4ffe4a273" have entirely different histories.
d1f5e57703
...
5b8a407a3f
4 changed files with 31 additions and 65 deletions
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
@ -19,7 +20,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 := newEnbasCLISchemaFromFile(enbasCLISchemaFilepath)
|
schema, err := readSchemaFile(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)
|
||||||
}
|
}
|
||||||
|
@ -33,6 +34,22 @@ 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,
|
||||||
|
|
|
@ -53,19 +53,13 @@ func (e enbasCLISchemaFlagMap) getDescription(name string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type enbasCLISchemaCommand struct {
|
type enbasCLISchemaCommand struct {
|
||||||
AdditionalFields []enbasCLISchemaAdditionalFields `json:"additionalFields"`
|
AddPrinter bool `json:"addPrinter"`
|
||||||
|
AddConfig bool `json:"addConfig"`
|
||||||
Flags []enbasCLISchemaFlagReference `json:"flags"`
|
Flags []enbasCLISchemaFlagReference `json:"flags"`
|
||||||
Summary string `json:"summary"`
|
Summary string `json:"summary"`
|
||||||
UseConfig bool `json:"useConfig"`
|
|
||||||
UsePrinter bool `json:"usePrinter"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type enbasCLISchemaFlagReference struct {
|
type enbasCLISchemaFlagReference struct {
|
||||||
Flag string `json:"flag"`
|
Flag string `json:"flag"`
|
||||||
Default string `json:"default"`
|
Default string `json:"default"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type enbasCLISchemaAdditionalFields struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
}
|
|
||||||
|
|
|
@ -8,52 +8,19 @@ 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 }}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- range $field := $command.AdditionalFields -}}
|
|
||||||
{{ print "" }}
|
|
||||||
{{ $field.Name }} {{ $field.Type }}
|
|
||||||
{{- end -}}
|
|
||||||
{{ print "" }}
|
{{ print "" }}
|
||||||
}
|
}
|
||||||
|
|
||||||
func {{ $new_executor_function_name }}(
|
func {{ $new_executor_function_name }}() *{{ $struct_name }} {
|
||||||
{{- if $command.UsePrinter }}
|
|
||||||
printer *printer.Printer,
|
|
||||||
{{- end }}
|
|
||||||
{{- if $command.UseConfig }}
|
|
||||||
config *config.Config,
|
|
||||||
{{- end }}
|
|
||||||
{{- range $field := $command.AdditionalFields -}}
|
|
||||||
{{ 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,
|
|
||||||
{{- end }}
|
|
||||||
{{- if $command.UseConfig }}
|
|
||||||
config: config,
|
|
||||||
{{- end }}
|
|
||||||
{{- range $field := $command.AdditionalFields -}}
|
|
||||||
{{ print "" }}
|
|
||||||
{{ $field.Name }}: {{ $field.Name }},
|
|
||||||
{{- end -}}
|
|
||||||
{{ print "" }}
|
|
||||||
}
|
}
|
||||||
{{ print "" }}
|
{{ print "" }}
|
||||||
exe.Usage = commandUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, exe.FlagSet)
|
exe.Usage = commandUsageFunc({{ printf "%q" $name }}, {{ printf "%q" $command.Summary }}, showExe.FlagSet)
|
||||||
{{ print "" }}
|
{{ print "" }}
|
||||||
{{- range $flag := $command.Flags -}}
|
{{- range $flag := $command.Flags -}}
|
||||||
{{- if eq (getFlagType $flag.Flag) "string" -}}
|
{{- if eq (getFlagType $flag.Flag) "string" -}}
|
||||||
|
|
|
@ -12,32 +12,20 @@
|
||||||
|
|
||||||
"commands": {
|
"commands": {
|
||||||
"login": {
|
"login": {
|
||||||
|
"addPrinter": true,
|
||||||
|
"addConfig": true,
|
||||||
"flags": [
|
"flags": [
|
||||||
{ "flag": "instance", "default": "" }
|
{ "flag": "instance", "default": "" }
|
||||||
],
|
],
|
||||||
"summary": "Log into an account on GoToSocial",
|
"summary": "Login to an account on GoToSocial"
|
||||||
"useConfig": true,
|
|
||||||
"usePrinter": true
|
|
||||||
},
|
},
|
||||||
"version": {
|
"version": {
|
||||||
"additionalFields": [
|
"addPrinter": true,
|
||||||
{ "name": "binaryVersion", "type": "string"},
|
"addConfig": false,
|
||||||
{ "name": "buildTime", "type": "string"},
|
"summary": "Prints the application's version and build information",
|
||||||
{ "name": "goVersion", "type": "string"},
|
|
||||||
{ "name": "gitCommit", "type": "string"}
|
|
||||||
],
|
|
||||||
"flags": [
|
"flags": [
|
||||||
{ "flag": "full", "default": "false" }
|
{ "flag": "full", "default": "false" }
|
||||||
],
|
]
|
||||||
"summary": "Prints the application's version and build information",
|
|
||||||
"useConfig": false,
|
|
||||||
"usePrinter": true
|
|
||||||
},
|
|
||||||
"whoami": {
|
|
||||||
"flags": [],
|
|
||||||
"summary": "Prints the account that you are currently logged into",
|
|
||||||
"useConfig": true,
|
|
||||||
"usePrinter": true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue