From e898a1ded53df56f513ea8a0f5887f4992a98eda Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 23 Aug 2024 02:47:36 +0100 Subject: [PATCH] fix: fixed error when using --help flag Parse the flags before loading the configuration to fix the error where using the --help flag caused the app to return an error if the configuration file does not exist. --- .../templates/executor/executor.go.gotmpl | 15 +- internal/executor/executor.go | 254 +++++++++--------- 2 files changed, 135 insertions(+), 134 deletions(-) diff --git a/cmd/enbas-codegen/templates/executor/executor.go.gotmpl b/cmd/enbas-codegen/templates/executor/executor.go.gotmpl index 54ba0b8..be8fb2e 100644 --- a/cmd/enbas-codegen/templates/executor/executor.go.gotmpl +++ b/cmd/enbas-codegen/templates/executor/executor.go.gotmpl @@ -129,6 +129,14 @@ func {{ $execute_command_function_name }}( exe.Var(&exe.{{ flagFieldName $flag }}, {{ printf "%q" $flag.Flag }}, {{ getFlagDescription $flag.Flag | printf "%q" }}) {{- end -}} {{- end -}} + {{ print "" }} + {{ print "" }} + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("({{ $name }}) flag parsing error: " + err.Error() + ".") + + return err + } {{- if $command.UseConfig -}} {{ print "" }} {{ print "" }} @@ -155,13 +163,6 @@ func {{ $execute_command_function_name }}( {{- end -}} {{ print "" }} {{ print "" }} - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("({{ $name }}) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("({{ $name }}) execution error: " + err.Error() + ".") diff --git a/internal/executor/executor.go b/internal/executor/executor.go index bab0bdc..45c9314 100644 --- a/internal/executor/executor.go +++ b/internal/executor/executor.go @@ -106,6 +106,13 @@ func ExecuteAcceptCommand( exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(accept) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -122,13 +129,6 @@ func ExecuteAcceptCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(accept) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(accept) execution error: " + err.Error() + ".") @@ -179,6 +179,13 @@ func ExecuteAddCommand( exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") exe.Var(&exe.votes, "vote", "Add a vote to an option in a poll") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(add) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -195,13 +202,6 @@ func ExecuteAddCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(add) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(add) execution error: " + err.Error() + ".") @@ -241,6 +241,13 @@ func ExecuteBlockCommand( exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(block) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -257,13 +264,6 @@ func ExecuteBlockCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(block) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(block) execution error: " + err.Error() + ".") @@ -351,6 +351,13 @@ func ExecuteCreateCommand( exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") exe.StringVar(&exe.visibility, "visibility", "", "The visibility of the posted status") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(create) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -367,13 +374,6 @@ func ExecuteCreateCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(create) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(create) execution error: " + err.Error() + ".") @@ -416,6 +416,13 @@ func ExecuteDeleteCommand( exe.StringVar(&exe.statusID, "status-id", "", "The ID of the status") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(delete) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -432,13 +439,6 @@ func ExecuteDeleteCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(delete) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(delete) execution error: " + err.Error() + ".") @@ -490,6 +490,13 @@ func ExecuteEditCommand( exe.Var(&exe.mediaFocusValues, "media-focus", "The focus of the media file") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(edit) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -506,13 +513,6 @@ func ExecuteEditCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(edit) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(edit) execution error: " + err.Error() + ".") @@ -556,6 +556,13 @@ func ExecuteFollowCommand( exe.BoolVar(&exe.showReposts, "show-reposts", true, "Show reposts from the account you want to follow") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(follow) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -572,13 +579,6 @@ func ExecuteFollowCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(follow) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(follow) execution error: " + err.Error() + ".") @@ -612,16 +612,16 @@ func ExecuteInitCommand( exe.Usage = usage.ExecutorUsageFunc("init", "Creates a new configuration file in the specified configuration directory", exe.FlagSet) - // Create the printer for the executor. - exe.printer = printer.NewPrinter(noColor, "", 0) - // Parse the remaining arguments. if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(init) flag parsing error: " + err.Error() + ".") + printer.NewPrinter(noColor, "", 0).PrintFailure("(init) flag parsing error: " + err.Error() + ".") return err } + // Create the printer for the executor. + exe.printer = printer.NewPrinter(noColor, "", 0) + // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(init) execution error: " + err.Error() + ".") @@ -658,6 +658,13 @@ func ExecuteLoginCommand( exe.StringVar(&exe.instance, "instance", "", "The instance that you want to log into") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(login) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -674,13 +681,6 @@ func ExecuteLoginCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(login) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(login) execution error: " + err.Error() + ".") @@ -727,6 +727,13 @@ func ExecuteMuteCommand( exe.StringVar(&exe.statusID, "status-id", "", "The ID of the status") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(mute) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -743,13 +750,6 @@ func ExecuteMuteCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(mute) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(mute) execution error: " + err.Error() + ".") @@ -789,6 +789,13 @@ func ExecuteRejectCommand( exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(reject) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -805,13 +812,6 @@ func ExecuteRejectCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(reject) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(reject) execution error: " + err.Error() + ".") @@ -857,6 +857,13 @@ func ExecuteRemoveCommand( exe.StringVar(&exe.statusID, "status-id", "", "The ID of the status") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(remove) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -873,13 +880,6 @@ func ExecuteRemoveCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(remove) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(remove) execution error: " + err.Error() + ".") @@ -958,6 +958,13 @@ func ExecuteShowCommand( exe.StringVar(&exe.tag, "tag", "", "The name of the tag") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(show) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -974,13 +981,6 @@ func ExecuteShowCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(show) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(show) execution error: " + err.Error() + ".") @@ -1020,6 +1020,13 @@ func ExecuteSwitchCommand( exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.to, "to", "", "The resource type to action the target resource to (e.g. status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(switch) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -1036,13 +1043,6 @@ func ExecuteSwitchCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(switch) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(switch) execution error: " + err.Error() + ".") @@ -1082,6 +1082,13 @@ func ExecuteUnblockCommand( exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(unblock) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -1098,13 +1105,6 @@ func ExecuteUnblockCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(unblock) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(unblock) execution error: " + err.Error() + ".") @@ -1144,6 +1144,13 @@ func ExecuteUnfollowCommand( exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(unfollow) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -1160,13 +1167,6 @@ func ExecuteUnfollowCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(unfollow) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(unfollow) execution error: " + err.Error() + ".") @@ -1208,6 +1208,13 @@ func ExecuteUnmuteCommand( exe.StringVar(&exe.statusID, "status-id", "", "The ID of the status") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(unmute) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -1224,13 +1231,6 @@ func ExecuteUnmuteCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(unmute) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(unmute) execution error: " + err.Error() + ".") @@ -1267,16 +1267,16 @@ func ExecuteVersionCommand( exe.BoolVar(&exe.full, "full", false, "Set to true to print the build information in full") - // Create the printer for the executor. - exe.printer = printer.NewPrinter(noColor, "", 0) - // Parse the remaining arguments. if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(version) flag parsing error: " + err.Error() + ".") + printer.NewPrinter(noColor, "", 0).PrintFailure("(version) flag parsing error: " + err.Error() + ".") return err } + // Create the printer for the executor. + exe.printer = printer.NewPrinter(noColor, "", 0) + // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(version) execution error: " + err.Error() + ".") @@ -1310,6 +1310,13 @@ func ExecuteWhoamiCommand( exe.Usage = usage.ExecutorUsageFunc("whoami", "Prints the account that you are currently logged into", exe.FlagSet) + // Parse the remaining arguments. + if err := exe.Parse(args); err != nil { + printer.NewPrinter(noColor, "", 0).PrintFailure("(whoami) flag parsing error: " + err.Error() + ".") + + return err + } + // Load the configuration from file. exeConfig, err := config.NewConfigFromFile(exe.configDir) if err != nil { @@ -1326,13 +1333,6 @@ func ExecuteWhoamiCommand( exe.config.LineWrapMaxWidth, ) - // Parse the remaining arguments. - if err := exe.Parse(args); err != nil { - exe.printer.PrintFailure("(whoami) flag parsing error: " + err.Error() + ".") - - return err - } - // Run the executor. if err := exe.Execute(); err != nil { exe.printer.PrintFailure("(whoami) execution error: " + err.Error() + ".")