checkpoint: added the switch command to schema

This commit is contained in:
Dan Anglin 2024-08-09 23:37:50 +01:00
parent a3a1adbf14
commit 622ee40306
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
7 changed files with 82 additions and 38 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"strings"
"text/template" "text/template"
"unicode" "unicode"
) )
@ -35,9 +36,10 @@ func main() {
func generateExecutors(schema enbasCLISchema, output string) error { func generateExecutors(schema enbasCLISchema, output string) error {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"capitalise": capitalise, "capitalise": capitalise,
"getFlagType": schema.Flags.getType, "convertFlagToMixedCaps": convertFlagToMixedCaps,
"getFlagDescription": schema.Flags.getDescription, "getFlagType": schema.Flags.getType,
"getFlagDescription": schema.Flags.getDescription,
} }
tmpl := template.Must(template.New("executor-template").Funcs(funcMap).Parse(executorsFileTemplate)) tmpl := template.Must(template.New("executor-template").Funcs(funcMap).Parse(executorsFileTemplate))
@ -72,3 +74,28 @@ func capitalise(str string) string {
return string(runes) return string(runes)
} }
func convertFlagToMixedCaps(value string) string {
var builder strings.Builder
runes := []rune(value)
numRunes := len(runes)
cursor := 0
for cursor < numRunes {
if runes[cursor] != '-' {
builder.WriteRune(runes[cursor])
cursor++
} else {
if cursor != numRunes-1 && unicode.IsLower(runes[cursor+1]) {
builder.WriteRune(unicode.ToUpper(runes[cursor+1]))
cursor += 2
} else {
cursor++
}
}
}
return builder.String()
}

View file

@ -16,7 +16,7 @@ type {{ $struct_name }} struct {
{{- end }} {{- end }}
{{- range $flag := $command.Flags -}} {{- range $flag := $command.Flags -}}
{{ print "" }} {{ print "" }}
{{ $flag.Flag }} {{ getFlagType $flag.Flag }} {{ convertFlagToMixedCaps $flag.Flag }} {{ getFlagType $flag.Flag }}
{{- end -}} {{- end -}}
{{- range $field := $command.AdditionalFields -}} {{- range $field := $command.AdditionalFields -}}
{{ print "" }} {{ print "" }}
@ -57,9 +57,11 @@ func {{ $new_executor_function_name }}(
{{ print "" }} {{ print "" }}
{{- range $flag := $command.Flags -}} {{- range $flag := $command.Flags -}}
{{- if eq (getFlagType $flag.Flag) "string" -}} {{- if eq (getFlagType $flag.Flag) "string" -}}
exe.StringVar(&exe.{{ $flag.Flag }}, {{ printf "%q" $flag.Flag }}, {{ printf "%q" $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }}) {{ print "" }}
exe.StringVar(&exe.{{ convertFlagToMixedCaps $flag.Flag }}, {{ printf "%q" $flag.Flag }}, {{ printf "%q" $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
{{- else if eq (getFlagType $flag.Flag) "bool" -}} {{- else if eq (getFlagType $flag.Flag) "bool" -}}
exe.BoolVar(&exe.{{ $flag.Flag }}, {{ printf "%q" $flag.Flag }}, {{ $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }}) {{ print "" }}
exe.BoolVar(&exe.{{ convertFlagToMixedCaps $flag.Flag }}, {{ printf "%q" $flag.Flag }}, {{ $flag.Default }}, {{ getFlagDescription $flag.Flag | printf "%q" }})
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{ print "" }} {{ print "" }}

View file

@ -162,8 +162,6 @@ func run() error {
executor.CommandSwitch: executor.NewSwitchExecutor( executor.CommandSwitch: executor.NewSwitchExecutor(
enbasPrinter, enbasPrinter,
enbasConfig, enbasConfig,
executor.CommandSwitch,
executor.CommandSummaryLookup(executor.CommandSwitch),
), ),
executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor( executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor(
enbasPrinter, enbasPrinter,

View file

@ -26,10 +26,37 @@ func NewLoginExecutor(
} }
exe.Usage = commandUsageFunc("login", "Log into an account on GoToSocial", exe.FlagSet) exe.Usage = commandUsageFunc("login", "Log into an account on GoToSocial", exe.FlagSet)
exe.StringVar(&exe.instance, "instance", "", "The instance that you want to log into") exe.StringVar(&exe.instance, "instance", "", "The instance that you want to log into")
return &exe return &exe
} }
// SwitchExecutor is the executor for the switch command.
type SwitchExecutor struct {
*flag.FlagSet
printer *printer.Printer
config *config.Config
accountName string
to string
}
func NewSwitchExecutor(
printer *printer.Printer,
config *config.Config,
) *SwitchExecutor {
exe := SwitchExecutor{
FlagSet: flag.NewFlagSet("switch", flag.ExitOnError),
printer: printer,
config: config,
}
exe.Usage = commandUsageFunc("switch", "Perform a switch operation (e.g. switching between logged in accounts)", exe.FlagSet)
exe.StringVar(&exe.accountName, "account-name", "", "The name of the account")
exe.StringVar(&exe.to, "to", "", "TBC")
return &exe
}
// VersionExecutor is the executor for the version command. // VersionExecutor is the executor for the version command.
type VersionExecutor struct { type VersionExecutor struct {
*flag.FlagSet *flag.FlagSet
@ -58,6 +85,7 @@ func NewVersionExecutor(
} }
exe.Usage = commandUsageFunc("version", "Prints the application's version and build information", exe.FlagSet) exe.Usage = commandUsageFunc("version", "Prints the application's version and build information", exe.FlagSet)
exe.BoolVar(&exe.full, "full", false, "Set to true to print the build information in full") exe.BoolVar(&exe.full, "full", false, "Set to true to print the build information in full")
return &exe return &exe
} }

View file

@ -24,7 +24,6 @@ const (
flagExcludeReplies = "exclude-replies" flagExcludeReplies = "exclude-replies"
flagFrom = "from" flagFrom = "from"
flagFromFile = "from-file" flagFromFile = "from-file"
flagFull = "full"
flagInReplyTo = "in-reply-to" flagInReplyTo = "in-reply-to"
flagInstance = "instance" flagInstance = "instance"
flagLanguage = "language" flagLanguage = "language"

View file

@ -1,46 +1,19 @@
package executor package executor
import ( import (
"flag"
"fmt" "fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config" "codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
) )
type SwitchExecutor struct {
*flag.FlagSet
config *config.Config
printer *printer.Printer
toResourceType string
accountName string
}
func NewSwitchExecutor(printer *printer.Printer, config *config.Config, name, summary string) *SwitchExecutor {
switchExe := SwitchExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
config: config,
printer: printer,
}
switchExe.StringVar(&switchExe.toResourceType, flagTo, "", "The account to switch to")
switchExe.StringVar(&switchExe.accountName, flagAccountName, "", "The name of the account to switch to")
switchExe.Usage = commandUsageFunc(name, summary, switchExe.FlagSet)
return &switchExe
}
func (s *SwitchExecutor) Execute() error { func (s *SwitchExecutor) Execute() error {
funcMap := map[string]func() error{ funcMap := map[string]func() error{
resourceAccount: s.switchToAccount, resourceAccount: s.switchToAccount,
} }
doFunc, ok := funcMap[s.toResourceType] doFunc, ok := funcMap[s.to]
if !ok { if !ok {
return UnsupportedTypeError{resourceType: s.toResourceType} return UnsupportedTypeError{resourceType: s.to}
} }
return doFunc() return doFunc()

View file

@ -1,5 +1,9 @@
{ {
"flags": { "flags": {
"account-name": {
"type": "string",
"description": "The name of the account"
},
"full": { "full": {
"type": "bool", "type": "bool",
"description": "Set to true to print the build information in full" "description": "Set to true to print the build information in full"
@ -7,6 +11,10 @@
"instance": { "instance": {
"type": "string", "type": "string",
"description": "The instance that you want to log into" "description": "The instance that you want to log into"
},
"to": {
"type": "string",
"description": "TBC"
} }
}, },
@ -19,6 +27,15 @@
"useConfig": true, "useConfig": true,
"usePrinter": true "usePrinter": true
}, },
"switch": {
"flags": [
{ "flag": "account-name", "default": "" },
{ "flag": "to", "default": "" }
],
"summary": "Perform a switch operation (e.g. switching between logged in accounts)",
"useConfig": true,
"usePrinter": true
},
"version": { "version": {
"additionalFields": [ "additionalFields": [
{ "name": "binaryVersion", "type": "string"}, { "name": "binaryVersion", "type": "string"},