Skip to content

Commit

Permalink
feat(cli): add --init flag to initialize a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
SKalt committed Nov 4, 2023
1 parent 7db2bd0 commit bf594e0
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 84 deletions.
128 changes: 97 additions & 31 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,24 @@ func redoMessage(cmd *cobra.Command) {
var Cmd = &cobra.Command{
Use: "git-cc",
Short: "write conventional commits",
// not using cobra subcommands since they prevent passing arbitrary arguments
// not using cobra subcommands since they prevent passing arbitrary arguments,
// and I'd like to be able to start an invocation like `git-cc this is the commit message`
// without having to think about whether `this` is a subcommand.
PersistentPreRun: func(cmd *cobra.Command, args []string) {
dryRun, _ := cmd.Flags().GetBool("dry-run")
_, err := config.Init(dryRun)
if err != nil {
log.Fatalf("unable to initialize config: %s", err)
}
},
Run: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags()
// FIXME: move version, etc into subcommands
if version, _ := flags.GetBool("version"); version {
versionMode()
os.Exit(0)
} else if genCompletion, _ := flags.GetBool("generate-shell-completion"); genCompletion {
generateShellCompletion(cmd, args)
generateShellCompletion(cmd, args) // FIXME: use subcommand instead
os.Exit(0)
} else if genManPage, _ := flags.GetBool("generate-man-page"); genManPage {
generateManPage(cmd, args)
Expand All @@ -226,6 +236,22 @@ var Cmd = &cobra.Command{
fmt.Printf("config file path: %s\n", file)
os.Exit(0)
}
if init, _ := flags.GetBool("init"); init {
format, _ := cmd.Flags().GetString("config-format")
switch format {
case "yaml", "yml", "toml":
break
default:
log.Fatalf("unsupported default config-file format: %s", format)
}
if err != nil {
log.Fatalf("%s", err)
}
if err := config.InitDefaultCfgFile(cfg, format); err != nil {
log.Fatalf("%s", err)
}
os.Exit(0)
}
if redo, _ := flags.GetBool("redo"); redo {
redoMessage(cmd)
}
Expand All @@ -234,34 +260,74 @@ var Cmd = &cobra.Command{
},
}

var initCmd = &cobra.Command{
Use: "init",
Short: "initialize a config file if none is present",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("init", args)
dryRun, _ := cmd.Flags().GetBool("dry-run")
format, _ := cmd.Flags().GetString("config-format")
cfg, err := config.Init(dryRun)
switch format {
case "yaml", "yml", "toml":
break
default:
log.Fatalf("unsupported default config-file format: %s", format)
}
if err != nil {
log.Fatalf("%s", err)
}
if err := config.InitDefaultCfgFile(cfg, format); err != nil {
log.Fatalf("%s", err)
}
},
}

func init() {
flags := Cmd.Flags()
flags.BoolP("help", "h", false, "print the usage of git-cc")
flags.Bool("dry-run", false, "Only print the resulting conventional commit message; don't commit.")
flags.Bool("redo", false, "Reuse your last commit message")
flags.StringArrayP("message", "m", []string{}, "pass a complete conventional commit. If valid, it'll be committed without editing.")
flags.Bool("version", false, "print the version")
flags.Bool("show-config", false, "print the path to the config file and the relevant config ")
// TODO: accept more of git commit's flags; see https://git-scm.com/docs/git-commit
// likely: --cleanup=<mode>
// more difficult, and possibly better done manually: --amend, -C <commit>
// --reuse-message=<commit>, -c <commit>, --reedit-message=<commit>,
// --fixup=<commit>, --squash=<commit>
flags.String("author", "", "delegated to git-commit")
flags.String("date", "", "delegated to git-commit")
flags.BoolP("all", "a", false, "see the git-commit docs for --all|-a")
flags.BoolP("signoff", "s", false, "see the git-commit docs for --signoff|-s")
flags.Bool("no-gpg-sign", false, "see the git-commit docs for --no-gpg-sign")
flags.Bool("no-post-rewrite", false, "Bypass the post-rewrite hook")
flags.Bool("no-edit", false, "Use the selected commit message without launching an editor.")
flags.BoolP("no-verify", "n", false, "Bypass git hooks")
flags.Bool("verify", true, "Ensure git hooks run")
// https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---no-verify
flags.Bool("no-signoff", true, "Don't add a a `Signed-off-by` trailer to the commit message")
flags.Bool("generate-man-page", false, "Generate a man page in your manpath")
flags.Bool(
"generate-shell-completion",
false,
"print a bash/zsh/fish/powershell completion script to stdout",
)
{ // flags for git-cc
flags := Cmd.Flags()
flags.BoolP("help", "h", false, "print the usage of git-cc")
flags.Bool("dry-run", false, "Only print the resulting conventional commit message; don't commit.")
flags.Bool("redo", false, "Reuse your last commit message")
flags.StringArrayP("message", "m", []string{}, "pass a complete conventional commit. If valid, it'll be committed without editing.")
flags.Bool("version", false, "print the version")
flags.Bool("show-config", false, "print the path to the config file and the relevant config ")
// TODO: accept more of git commit's flags; see https://git-scm.com/docs/git-commit
// likely: --cleanup=<mode>
// more difficult, and possibly better done manually: --amend, -C <commit>
// --reuse-message=<commit>, -c <commit>, --reedit-message=<commit>,
// --fixup=<commit>, --squash=<commit>
flags.String("author", "", "delegated to git-commit")
flags.String("date", "", "delegated to git-commit")
flags.BoolP("all", "a", false, "see the git-commit docs for --all|-a")
flags.BoolP("signoff", "s", false, "see the git-commit docs for --signoff|-s")
flags.Bool("no-gpg-sign", false, "see the git-commit docs for --no-gpg-sign")
flags.Bool("no-post-rewrite", false, "Bypass the post-rewrite hook")
flags.Bool("no-edit", false, "Use the selected commit message without launching an editor.")
flags.BoolP("no-verify", "n", false, "Bypass git hooks")
flags.Bool("verify", true, "Ensure git hooks run")
// https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---no-verify
flags.Bool("no-signoff", true, "Don't add a a `Signed-off-by` trailer to the commit message")
flags.Bool("generate-man-page", false, "Generate a man page in your manpath")
flags.Bool(
"generate-shell-completion",
false,
"print a bash/zsh/fish/powershell completion script to stdout",
)
flags.Bool("init", false, "initialize a config file if none is present")
flags.String("config-format", "yaml", "The format of the config file to generate. One of: toml, yml, yaml")
}
// { // flags for git-cc init
// flags := initCmd.Flags()
// flags.Bool("dry-run", false, "Only print the resulting configuration; don't commit.")
// flags.StringP(
// "format",
// "f",
// "yaml",
// "The format of the config file to generate. One of: toml, yml, yaml",
// )
// }
// { // assemble sub-commands
// Cmd.AddCommand(initCmd)
// }
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/charmbracelet/bubbletea v0.22.1
github.com/muesli/reflow v0.3.0
github.com/muesli/termenv v0.13.0
github.com/spf13/cobra v1.5.0
github.com/spf13/cobra v1.7.0
github.com/wk8/go-ordered-map/v2 v2.1.5
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -22,7 +22,7 @@ require (
github.com/containerd/console v1.0.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -41,5 +41,4 @@ require (
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.8 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down Expand Up @@ -68,8 +68,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -97,8 +97,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit bf594e0

Please # to comment.