Skip to content

Commit

Permalink
Initial migration to Cobra.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgoetz committed Sep 10, 2024
1 parent c5bea99 commit 7e40442
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
71 changes: 39 additions & 32 deletions cmd/versionbump/main.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
package main

import (
"flag"
"fmt"
"github.com/ptgoetz/go-versionbump/internal"
vbc "github.com/ptgoetz/go-versionbump/internal/config"
vbv "github.com/ptgoetz/go-versionbump/internal/version"
"github.com/spf13/cobra"
"log"
"os"
)

var opts vbc.Options

func main() {
// Define command-line flags
var opts vbc.Options
// TODO Migrate to Cobra
flag.BoolVar(&opts.ShowVersion, "V", false, "Show the version of Config and exit.")
flag.StringVar(&opts.ConfigPath, "config", "versionbump.yaml", "The path to the configuration file")
flag.BoolVar(&opts.DryRun, "dry-run", false, "Dry run. Don't change anything, just report what would "+
"be changed")
flag.BoolVar(&opts.NoPrompt, "no-prompt", false, "Don't prompt the user for confirmation before making changes.")
flag.BoolVar(&opts.Quiet, "quiet", false, "Don't print verbose output.")
flag.StringVar(&opts.ResetVersion, "reset", "", "Reset the version to the specified value.")
flag.BoolVar(&opts.NoGit, "no-git", false, "Don't perform any git operations.")
flag.BoolVar(&opts.NoColor, "no-color", false, "Disable color output.")
flag.Parse()
args := flag.Args()
var rootCmd = &cobra.Command{
Use: "versionbump [bump-part]",
Short: "VersionBump is a tool for managing version bumps",
Long: `VersionBump is a tool for managing version bumps with optional git integration.`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if opts.ShowVersion {
fmt.Println(vbv.VersionBumpVersion)
os.Exit(0)
}

// print the version and exit
if opts.ShowVersion {
fmt.Println(vbv.VersionBumpVersion)
os.Exit(0)
}
if len(args) > 0 {
opts.BumpPart = args[0]
}
if len(args) == 0 && opts.ResetVersion == "" {
fmt.Println("ERROR: no version part specified.")
cmd.Usage()

Check failure on line 32 in cmd/versionbump/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `cmd.Usage` is not checked (errcheck)
os.Exit(1)
}

if len(args) > 0 {
opts.BumpPart = args[0]
}
if len(args) == 0 && opts.ResetVersion == "" {
fmt.Println("ERROR: no version part specified.")
flag.Usage()
os.Exit(1)
vb, err := internal.NewVersionBump(opts)
if err != nil {
log.Fatal(err)
}
vb.Run()
},
}

// Create a new VersionBump instance
vb, err := internal.NewVersionBump(opts)
if err != nil {
log.Fatal(err)
rootCmd.Flags().BoolVarP(&opts.ShowVersion, "version", "V", false, "Show the version of Config and exit.")
rootCmd.Flags().StringVarP(&opts.ConfigPath, "config", "c", "versionbump.yaml", "The path to the configuration file")
rootCmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "Dry run. Don't change anything, just report what would be changed")
rootCmd.Flags().BoolVar(&opts.NoPrompt, "no-prompt", false, "Don't prompt the user for confirmation before making changes.")
rootCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Don't print verbose output.")
rootCmd.Flags().StringVar(&opts.ResetVersion, "reset", "", "Reset the version to the specified value.")
rootCmd.Flags().BoolVar(&opts.NoGit, "no-git", false, "Don't perform any git operations.")
rootCmd.Flags().BoolVar(&opts.NoColor, "no-color", false, "Disable color output.")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
vb.Run()
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ module github.com/ptgoetz/go-versionbump
go 1.22.2

require (
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
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.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down

0 comments on commit 7e40442

Please sign in to comment.