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"
"os"
"os/exec"
"strings"
"text/template"
"unicode"
)
@ -36,6 +37,7 @@ func main() {
func generateExecutors(schema enbasCLISchema, output string) error {
funcMap := template.FuncMap{
"capitalise": capitalise,
"convertFlagToMixedCaps": convertFlagToMixedCaps,
"getFlagType": schema.Flags.getType,
"getFlagDescription": schema.Flags.getDescription,
}
@ -72,3 +74,28 @@ func capitalise(str string) string {
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 }}
{{- range $flag := $command.Flags -}}
{{ print "" }}
{{ $flag.Flag }} {{ getFlagType $flag.Flag }}
{{ convertFlagToMixedCaps $flag.Flag }} {{ getFlagType $flag.Flag }}
{{- end -}}
{{- range $field := $command.AdditionalFields -}}
{{ print "" }}
@ -57,9 +57,11 @@ func {{ $new_executor_function_name }}(
{{ print "" }}
{{- range $flag := $command.Flags -}}
{{- 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" -}}
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 -}}
{{ print "" }}

View file

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

View file

@ -26,10 +26,37 @@ func NewLoginExecutor(
}
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")
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.
type VersionExecutor struct {
*flag.FlagSet
@ -58,6 +85,7 @@ func NewVersionExecutor(
}
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")
return &exe
}

View file

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

View file

@ -1,46 +1,19 @@
package executor
import (
"flag"
"fmt"
"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 {
funcMap := map[string]func() error{
resourceAccount: s.switchToAccount,
}
doFunc, ok := funcMap[s.toResourceType]
doFunc, ok := funcMap[s.to]
if !ok {
return UnsupportedTypeError{resourceType: s.toResourceType}
return UnsupportedTypeError{resourceType: s.to}
}
return doFunc()

View file

@ -1,5 +1,9 @@
{
"flags": {
"account-name": {
"type": "string",
"description": "The name of the account"
},
"full": {
"type": "bool",
"description": "Set to true to print the build information in full"
@ -7,6 +11,10 @@
"instance": {
"type": "string",
"description": "The instance that you want to log into"
},
"to": {
"type": "string",
"description": "TBC"
}
},
@ -19,6 +27,15 @@
"useConfig": 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": {
"additionalFields": [
{ "name": "binaryVersion", "type": "string"},