Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prompt feature: select the patch/minor/major from prompt #9

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

== USAGE

Usage: gobump (major|minor|patch|set <version>) [-w] [-v] [<path>]
Usage: gobump (major|minor|patch|up|set <version>|show) [-w] [-v] [<path>]

Commands:
major bump major version up
minor bump minor version up
patch bump patch version up
up bump up with prompt
set <version> set exact version (no increments)
show only show the versions (implies -v)

Expand Down
5 changes: 4 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ func Run(argv []string, outStream, errStream io.Writer) error {
fs.Usage = func() {
out := errStream
fs.SetOutput(out)
fmt.Fprintln(out, `Usage: gobump (major|minor|patch|set <version>) [-w] [-v] [<path>]
fmt.Fprintln(out, `Usage: gobump (major|minor|patch|up|set <version>|show) [-w] [-v] [<path>]

Commands:
major bump major version up
minor bump minor version up
patch bump patch version up
up bump up with prompt
set <version> set exact version (no increments)
show only show the versions (implies -v)
Flags:`)
Expand All @@ -42,6 +43,8 @@ Flags:`)
gb.Config.MinorDelta = 1
case "patch":
gb.Config.PatchDelta = 1
case "up":
gb.Config.Prompt = true
case "set":
if len(argv) < 2 {
return fmt.Errorf("please specify a version to set")
Expand Down
25 changes: 25 additions & 0 deletions gobump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gobump
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -113,6 +114,8 @@ type Config struct {
Exact string
// The pattern of "version" variable/constants. Defaults to /^(?i)version$/.
NamePattern *regexp.Regexp
// Prompt the bump up target (major, minor or patch)
Prompt bool
// Default version in the case none was set. Defaults to "0.0.0".
Default string
}
Expand Down Expand Up @@ -216,6 +219,28 @@ func (conf Config) ProcessNode(fset *token.FileSet, node ast.Node) (versions map
currentVersion = v
}

if conf.Prompt {
result, err := promptTarget(currentVersion, fset.File(n.Pos()).Name())
if err != nil {
nodeErr = err
return false
}
conf.PatchDelta = 0
conf.MinorDelta = 0
conf.MajorDelta = 0
switch result {
case promptResultPatch:
conf.PatchDelta = 1
case promptResultMinor:
conf.MinorDelta = 1
case promptResultMajor:
conf.MajorDelta = 1
default:
nodeErr = errors.New("unexpected target")
return false
}
}

ver, err := conf.bumpedVersion(currentVersion)
if err != nil {
nodeErr = NodeErr{
Expand Down
63 changes: 63 additions & 0 deletions prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package gobump

import (
"fmt"

"github.com/manifoldco/promptui"
"github.com/mattn/go-tty"
)

type promptResult int

const (
promptResultNone promptResult = iota
promptResultPatch
promptResultMinor
promptResultMajor
)

func promptTarget(currentVersion, target string) (promptResult, error) {
tty, err := tty.Open()
if err != nil {
return promptResultNone, err
}
defer tty.Close()

candidates := []struct {
name string
config Config
result promptResult
}{
{"patch", Config{PatchDelta: 1}, promptResultPatch},
{"minor", Config{MinorDelta: 1}, promptResultMinor},
{"major", Config{MajorDelta: 1}, promptResultMajor},
}

items := make([]string, len(candidates))
promptResults := make(map[int]promptResult)

for i, c := range candidates {
newVersion, err := c.config.bumpedVersion(currentVersion)
if err != nil {
return promptResultNone, err
}
items[i] = fmt.Sprintf("%s (%s -> %s)", c.name, currentVersion, newVersion)
promptResults[i] = c.result
}

prompt := promptui.Select{
Label: "Bump up " + target,
HideHelp: true,
Items: items,
Stdin: tty.Input(),
Stdout: tty.Output(),
}

index, _, err := prompt.Run()

if err != nil {
return promptResultNone, err
}

return promptResults[index], nil
}