-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
prompt.go
63 lines (51 loc) · 1.24 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
}