diff --git a/cmd/enbas-cli-generators/main.go b/cmd/enbas-cli-generators/main.go index e68322b..0eef6ff 100644 --- a/cmd/enbas-cli-generators/main.go +++ b/cmd/enbas-cli-generators/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/exec" + "strings" "text/template" "unicode" ) @@ -35,9 +36,10 @@ func main() { func generateExecutors(schema enbasCLISchema, output string) error { funcMap := template.FuncMap{ - "capitalise": capitalise, - "getFlagType": schema.Flags.getType, - "getFlagDescription": schema.Flags.getDescription, + "capitalise": capitalise, + "convertFlagToMixedCaps": convertFlagToMixedCaps, + "getFlagType": schema.Flags.getType, + "getFlagDescription": schema.Flags.getDescription, } tmpl := template.Must(template.New("executor-template").Funcs(funcMap).Parse(executorsFileTemplate)) @@ -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() +} diff --git a/cmd/enbas-cli-generators/templates.go b/cmd/enbas-cli-generators/templates.go index 9b65f20..162efc0 100644 --- a/cmd/enbas-cli-generators/templates.go +++ b/cmd/enbas-cli-generators/templates.go @@ -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 "" }} diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 49e7734..8d1822e 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -162,8 +162,6 @@ func run() error { executor.CommandSwitch: executor.NewSwitchExecutor( enbasPrinter, enbasConfig, - executor.CommandSwitch, - executor.CommandSummaryLookup(executor.CommandSwitch), ), executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor( enbasPrinter, diff --git a/internal/executor/executors.go b/internal/executor/executors.go index 15864f4..24469a4 100644 --- a/internal/executor/executors.go +++ b/internal/executor/executors.go @@ -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 } diff --git a/internal/executor/flags.go b/internal/executor/flags.go index ceebaa4..3c53da3 100644 --- a/internal/executor/flags.go +++ b/internal/executor/flags.go @@ -24,7 +24,6 @@ const ( flagExcludeReplies = "exclude-replies" flagFrom = "from" flagFromFile = "from-file" - flagFull = "full" flagInReplyTo = "in-reply-to" flagInstance = "instance" flagLanguage = "language" diff --git a/internal/executor/switch.go b/internal/executor/switch.go index bb974d5..032aea0 100644 --- a/internal/executor/switch.go +++ b/internal/executor/switch.go @@ -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() diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index e51779f..8125631 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -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"},