Skip to content

Commit

Permalink
feat: added named flags
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Oct 2, 2023
1 parent e410ded commit 6b4a4ef
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 83 deletions.
16 changes: 11 additions & 5 deletions cmd/cmd-close.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"

"github.com/lindell/multi-gitter/cmd/namedflag"
"github.com/lindell/multi-gitter/internal/multigitter"
"github.com/spf13/cobra"
)
Expand All @@ -18,11 +19,16 @@ func CloseCmd() *cobra.Command {
RunE: closeCMD,
}

cmd.Flags().StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.")
configurePlatform(cmd)
configureRunPlatform(cmd, false)
configureLogging(cmd, "-")
configureConfig(cmd)
fss := namedflag.New(cmd)
flags := fss.FlagSet("Close")

flags.StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.")
configurePlatform(cmd, fss.FlagSet("Platform"))
configureRunPlatform(fss.FlagSet("Platform"), false)
configureLogging(fss.FlagSet("Logging"), "-")
configureConfig(fss.FlagSet("Config"))

namedflag.SetUsageAndHelpFunc(cmd, fss)

return cmd
}
Expand Down
18 changes: 12 additions & 6 deletions cmd/cmd-merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"

"github.com/lindell/multi-gitter/cmd/namedflag"
"github.com/lindell/multi-gitter/internal/multigitter"
"github.com/spf13/cobra"
)
Expand All @@ -18,13 +19,18 @@ func MergeCmd() *cobra.Command {
RunE: merge,
}

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"},
fss := namedflag.New(cmd)
flags := fss.FlagSet("Merge")

flags.StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.")
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)
configureRunPlatform(cmd, false)
configureLogging(cmd, "-")
configureConfig(cmd)
configurePlatform(cmd, fss.FlagSet("Platform"))
configureRunPlatform(fss.FlagSet("Platform"), false)
configureLogging(fss.FlagSet("Logging"), "-")
configureConfig(fss.FlagSet("Config"))

namedflag.SetUsageAndHelpFunc(cmd, fss)

return cmd
}
Expand Down
20 changes: 13 additions & 7 deletions cmd/cmd-print.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/pkg/errors"

"github.com/lindell/multi-gitter/cmd/namedflag"
"github.com/lindell/multi-gitter/internal/multigitter"
"github.com/spf13/cobra"
)
Expand All @@ -32,13 +33,18 @@ func PrintCmd() *cobra.Command {
RunE: printCMD,
}

cmd.Flags().IntP("concurrent", "C", 1, "The maximum number of concurrent runs.")
cmd.Flags().StringP("error-output", "E", "-", `The file that the output of the script should be outputted to. "-" means stderr.`)
configureGit(cmd)
configurePlatform(cmd)
configureLogging(cmd, "")
configureConfig(cmd)
cmd.Flags().AddFlagSet(outputFlag())
fss := namedflag.New(cmd)
flags := fss.FlagSet("Print")

flags.IntP("concurrent", "C", 1, "The maximum number of concurrent runs.")
flags.StringP("error-output", "E", "-", `The file that the output of the script should be outputted to. "-" means stderr.`)
configureGit(fss.FlagSet("Git"))
configurePlatform(cmd, fss.FlagSet("Platform"))
configureLogging(fss.FlagSet("Logging"), "")
configureConfig(fss.FlagSet("Config"))
outputFlag(fss.FlagSet("Output"))

namedflag.SetUsageAndHelpFunc(cmd, fss)

return cmd
}
Expand Down
59 changes: 33 additions & 26 deletions cmd/cmd-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"syscall"

"github.com/lindell/multi-gitter/cmd/namedflag"
"github.com/lindell/multi-gitter/internal/git"

"github.com/lindell/multi-gitter/internal/multigitter"
Expand All @@ -35,39 +36,45 @@ func RunCmd() *cobra.Command {
RunE: run,
}

cmd.Flags().StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.")
cmd.Flags().StringP("base-branch", "", "", "The branch which the changes will be based on.")
cmd.Flags().StringP("pr-title", "t", "", "The title of the PR. Will default to the first line of the commit message if none is set.")
cmd.Flags().StringP("pr-body", "b", "", "The body of the commit message. Will default to everything but the first line of the commit message if none is set.")
cmd.Flags().StringP("commit-message", "m", "", "The commit message. Will default to title + body if none is set.")
cmd.Flags().StringSliceP("reviewers", "r", nil, "The username of the reviewers to be added on the pull request.")
cmd.Flags().StringSliceP("team-reviewers", "", nil, "Github team names of the reviewers, in format: 'org/team'")
cmd.Flags().StringSliceP("assignees", "a", nil, "The username of the assignees to be added on the pull request.")
cmd.Flags().IntP("max-reviewers", "M", 0, "If this value is set, reviewers will be randomized.")
cmd.Flags().IntP("max-team-reviewers", "", 0, "If this value is set, team reviewers will be randomized")
cmd.Flags().IntP("concurrent", "C", 1, "The maximum number of concurrent runs.")
cmd.Flags().BoolP("skip-pr", "", false, "Skip pull request and directly push to the branch.")
cmd.Flags().StringSliceP("skip-repo", "s", nil, "Skip changes on specified repositories, the name is including the owner of repository in the format \"ownerName/repoName\".")
cmd.Flags().BoolP("interactive", "i", false, "Take manual decision before committing any change. Requires git to be installed.")
fss := namedflag.New(cmd)
flags := fss.FlagSet("Run")
gitFlags := fss.FlagSet("Git")

gitFlags.StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.")
gitFlags.StringP("base-branch", "", "", "The branch which the changes will be based on.")
flags.StringP("pr-title", "t", "", "The title of the PR. Will default to the first line of the commit message if none is set.")
flags.StringP("pr-body", "b", "", "The body of the commit message. Will default to everything but the first line of the commit message if none is set.")
gitFlags.StringP("commit-message", "m", "", "The commit message. Will default to title + body if none is set.")
flags.StringSliceP("reviewers", "r", nil, "The username of the reviewers to be added on the pull request.")
flags.StringSliceP("team-reviewers", "", nil, "Github team names of the reviewers, in format: 'org/team'")
flags.StringSliceP("assignees", "a", nil, "The username of the assignees to be added on the pull request.")
flags.IntP("max-reviewers", "M", 0, "If this value is set, reviewers will be randomized.")
flags.IntP("max-team-reviewers", "", 0, "If this value is set, team reviewers will be randomized")
flags.IntP("concurrent", "C", 1, "The maximum number of concurrent runs.")
flags.BoolP("skip-pr", "", false, "Skip pull request and directly push to the branch.")
flags.StringSliceP("skip-repo", "s", nil, "Skip changes on specified repositories, the name is including the owner of repository in the format \"ownerName/repoName\".")
flags.BoolP("interactive", "i", false, "Take manual decision before committing any change. Requires git to be installed.")
cmd.Flags().BoolP("dry-run", "d", false, "Run without pushing changes or creating pull requests.")
cmd.Flags().StringP("conflict-strategy", "", "skip", `What should happen if the branch already exist.
flags.StringP("conflict-strategy", "", "skip", `What should happen if the branch already exist.
Available values:
skip: Skip making any changes to the existing branch and do not create a new pull request.
replace: Replace the existing content of the branch by force pushing any new changes, then reuse any existing pull request, or create a new one if none exist.
`)
cmd.Flags().BoolP("draft", "", false, "Create pull request(s) as draft.")
_ = cmd.RegisterFlagCompletionFunc("conflict-strategy", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
_ = flags.RegisterFlagCompletionFunc("conflict-strategy", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"skip", "replace"}, cobra.ShellCompDirectiveNoFileComp
})
cmd.Flags().StringSliceP("labels", "", nil, "Labels to be added to any created pull request.")
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.")
configureGit(cmd)
configurePlatform(cmd)
configureRunPlatform(cmd, true)
configureLogging(cmd, "-")
configureConfig(cmd)
cmd.Flags().AddFlagSet(outputFlag())
flags.BoolP("draft", "", false, "Create pull request(s) as draft.")
flags.StringSliceP("labels", "", nil, "Labels to be added to any created pull request.")
gitFlags.StringP("author-name", "", "", "Name of the committer. If not set, the global git config setting will be used.")
gitFlags.StringP("author-email", "", "", "Email of the committer. If not set, the global git config setting will be used.")
configureGit(gitFlags)
configurePlatform(cmd, fss.FlagSet("Platform"))
configureRunPlatform(fss.FlagSet("Platform"), true)
configureLogging(fss.FlagSet("Logging"), "-")
configureConfig(fss.FlagSet("Config"))
outputFlag(fss.FlagSet("Output"))

namedflag.SetUsageAndHelpFunc(cmd, fss)

return cmd
}
Expand Down
18 changes: 12 additions & 6 deletions cmd/cmd-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"

"github.com/lindell/multi-gitter/cmd/namedflag"
"github.com/lindell/multi-gitter/internal/multigitter"
"github.com/spf13/cobra"
)
Expand All @@ -19,12 +20,17 @@ func StatusCmd() *cobra.Command {
RunE: status,
}

cmd.Flags().StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.")
configurePlatform(cmd)
configureRunPlatform(cmd, false)
configureLogging(cmd, "-")
configureConfig(cmd)
cmd.Flags().AddFlagSet(outputFlag())
fss := namedflag.New(cmd)
flags := fss.FlagSet("Status")

flags.StringP("branch", "B", "multi-gitter-branch", "The name of the branch where changes are committed.")
configurePlatform(cmd, fss.FlagSet("Platform"))
configureRunPlatform(fss.FlagSet("Platform"), false)
configureLogging(fss.FlagSet("Logging"), "-")
configureConfig(fss.FlagSet("Config"))
outputFlag(fss.FlagSet("Output"))

namedflag.SetUsageAndHelpFunc(cmd, fss)

return cmd
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cmd
import (
"fmt"

"github.com/lindell/multi-gitter/cmd/namedflag"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

func configureConfig(cmd *cobra.Command) {
cmd.Flags().StringP("config", "", "", "Path of the config file.")
func configureConfig(flags namedflag.Set) {
flags.StringP("config", "", "", "Path of the config file.")
}

func initializeConfig(cmd *cobra.Command) error {
Expand Down
9 changes: 5 additions & 4 deletions cmd/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/lindell/multi-gitter/cmd/namedflag"
"github.com/lindell/multi-gitter/internal/git/cmdgit"
"github.com/lindell/multi-gitter/internal/git/gogit"
"github.com/lindell/multi-gitter/internal/multigitter"
Expand All @@ -9,14 +10,14 @@ import (
flag "github.com/spf13/pflag"
)

func configureGit(cmd *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("git-type", "", "go", `The type of git implementation to use.
func configureGit(flags namedflag.Set) {
flags.IntP("fetch-depth", "f", 1, "Limit fetching to the specified number of commits. Set to 0 for no limit.")
flags.StringP("git-type", "", "go", `The type of git implementation to use.
Available values:
go: Uses go-git, a Go native implementation of git. This is compiled with the multi-gitter binary, and no extra dependencies are needed.
cmd: Calls out to the git command. This requires git to be installed and available with by calling "git".
`)
_ = cmd.RegisterFlagCompletionFunc("git-type", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
_ = flags.RegisterFlagCompletionFunc("git-type", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"go", "cmd"}, cobra.ShellCompDirectiveDefault
})
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/lindell/multi-gitter/cmd/namedflag"
internallog "github.com/lindell/multi-gitter/internal/log"
)

func configureLogging(cmd *cobra.Command, logFile string) {
flags := cmd.Flags()

func configureLogging(flags namedflag.Set, logFile string) {
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) {
_ = flags.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 formatting of the logs. Available values: text, json, json-pretty.`)
_ = cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
_ = flags.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"text", "json", "json-pretty"}, cobra.ShellCompDirectiveDefault
})

Expand Down
Loading

0 comments on commit 6b4a4ef

Please sign in to comment.