-
Notifications
You must be signed in to change notification settings - Fork 1
/
next_version.go
70 lines (58 loc) · 1.58 KB
/
next_version.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
64
65
66
67
68
69
70
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/nextlinux/chronicle/internal/git"
"github.com/nextlinux/chronicle/internal/log"
)
var nextVersionCmd = &cobra.Command{
Use: "next-version [PATH]",
Short: "Guess the next version based on the changelog diff from the last release",
Args: cobra.MaximumNArgs(1),
RunE: runNextVersion,
PreRunE: func(cmd *cobra.Command, args []string) error {
var repo = "./"
if len(args) == 1 {
if !git.IsRepository(args[0]) {
return fmt.Errorf("given path is not a git repository: %s", args[0])
}
repo = args[0]
} else {
log.Infof("no repository path given, assuming %q", repo)
}
appConfig.CliOptions.RepoPath = repo
return nil
},
}
func init() {
setNextVersionFlags(nextVersionCmd.Flags())
if err := bindNextVersionConfigOptions(nextVersionCmd.Flags()); err != nil {
panic(err)
}
rootCmd.AddCommand(nextVersionCmd)
}
func setNextVersionFlags(flags *pflag.FlagSet) {
flags.BoolP(
"enforce-v0", "e", false,
"major changes bump the minor version field for versions < 1.0",
)
}
func bindNextVersionConfigOptions(flags *pflag.FlagSet) error {
if err := viper.BindPFlag("enforce-v0", flags.Lookup("enforce-v0")); err != nil {
return err
}
return nil
}
func runNextVersion(cmd *cobra.Command, args []string) error {
appConfig.SpeculateNextVersion = true
worker := selectWorker(appConfig.CliOptions.RepoPath)
_, description, err := worker()
if err != nil {
return err
}
_, err = os.Stdout.Write([]byte(description.Release.Version))
return err
}