Skip to content

Commit

Permalink
feat: added static flag completion for enums (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell authored Mar 30, 2021
1 parent 3c16c98 commit 586dd61
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down

0 comments on commit 586dd61

Please sign in to comment.