From f66421519acda8738acb570c8e6f2c808d104fd9 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 30 Mar 2021 19:07:51 +0200 Subject: [PATCH] feat: added static flag completion for enums --- cmd/close.go | 2 +- cmd/merge.go | 2 +- cmd/print.go | 2 +- cmd/root.go | 17 +++++++++++++---- cmd/run.go | 2 +- cmd/status.go | 2 +- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cmd/close.go b/cmd/close.go index c4107775..ab2b38d4 100644 --- a/cmd/close.go +++ b/cmd/close.go @@ -20,7 +20,7 @@ func CloseCmd() *cobra.Command { cmd.Flags().StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.") configurePlatform(cmd) - cmd.Flags().AddFlagSet(logFlags("-")) + configureLogging(cmd, "-") return cmd } diff --git a/cmd/merge.go b/cmd/merge.go index bae535f5..1fd7edd5 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -21,7 +21,7 @@ func MergeCmd() *cobra.Command { cmd.Flags().StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.") cmd.Flags().StringSliceP("merge-type", "", []string{"merge", "squash", "rebase"}, "The type of merge that should be done (GitHub). Multiple types can be used as backup strategies if the first one is not allowed.") configurePlatform(cmd) - cmd.Flags().AddFlagSet(logFlags("-")) + configureLogging(cmd, "-") return cmd } diff --git a/cmd/print.go b/cmd/print.go index 4d3715cd..63ea266d 100755 --- a/cmd/print.go +++ b/cmd/print.go @@ -36,7 +36,7 @@ func PrintCmd() *cobra.Command { cmd.Flags().IntP("fetch-depth", "f", 1, "Limit fetching to the specified number of commits. Set to 0 for no limit") cmd.Flags().StringP("error-output", "E", "-", `The file that the output of the script should be outputted to. "-" means stderr`) configurePlatform(cmd) - cmd.Flags().AddFlagSet(logFlags("")) + configureLogging(cmd, "") cmd.Flags().AddFlagSet(outputFlag()) return cmd diff --git a/cmd/root.go b/cmd/root.go index ce1de2d1..06e2983e 100755 --- a/cmd/root.go +++ b/cmd/root.go @@ -55,6 +55,9 @@ func configurePlatform(cmd *cobra.Command) { flags.StringSliceP("project", "P", nil, "The name, including owner of a GitLab project in the format \"ownerName/repoName\"") flags.StringP("platform", "p", "github", "The platform that is used. Available values: github, gitlab") + _ = cmd.RegisterFlagCompletionFunc("platform", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"github", "gitlab"}, cobra.ShellCompDirectiveDefault + }) // Autocompletion for organizations _ = cmd.RegisterFlagCompletionFunc("org", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { @@ -129,14 +132,20 @@ func configurePlatform(cmd *cobra.Command) { }) } -func logFlags(logFile string) *flag.FlagSet { - flags := flag.NewFlagSet("log", flag.ExitOnError) +func configureLogging(cmd *cobra.Command, logFile string) { + flags := cmd.Flags() flags.StringP("log-level", "L", "info", "The level of logging that should be made. Available values: trace, debug, info, error") + _ = cmd.RegisterFlagCompletionFunc("log-level", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"trace", "debug", "info", "error"}, cobra.ShellCompDirectiveDefault + }) + flags.StringP("log-format", "", "text", `The formating of the logs. Available values: text, json, json-pretty`) - flags.StringP("log-file", "", logFile, `The file where all logs should be printed to. "-" means stdout`) + _ = cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"text", "json", "json-pretty"}, cobra.ShellCompDirectiveDefault + }) - return flags + flags.StringP("log-file", "", logFile, `The file where all logs should be printed to. "-" means stdout`) } func logFlagInit(cmd *cobra.Command, args []string) error { diff --git a/cmd/run.go b/cmd/run.go index a8afeea4..e2e6eaf9 100755 --- a/cmd/run.go +++ b/cmd/run.go @@ -48,7 +48,7 @@ func RunCmd() *cobra.Command { cmd.Flags().StringP("author-name", "", "", "Name of the committer. If not set, the global git config setting will be used.") cmd.Flags().StringP("author-email", "", "", "Email of the committer. If not set, the global git config setting will be used.") configurePlatform(cmd) - cmd.Flags().AddFlagSet(logFlags("-")) + configureLogging(cmd, "-") cmd.Flags().AddFlagSet(outputFlag()) return cmd diff --git a/cmd/status.go b/cmd/status.go index 281a3f2c..4b8914d6 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -21,7 +21,7 @@ func StatusCmd() *cobra.Command { cmd.Flags().StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.") configurePlatform(cmd) - cmd.Flags().AddFlagSet(logFlags("-")) + configureLogging(cmd, "-") cmd.Flags().AddFlagSet(outputFlag()) return cmd