-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
98 lines (83 loc) · 2.8 KB
/
cmd.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"os"
"github.com/mbamber/ccp/git"
"github.com/spf13/cobra"
"github.com/xfxdev/xlog"
)
// flags
var (
current string
directory string
since string
verbose bool
)
var rootCmd = &cobra.Command{
Use: "ccp",
Short: "Cov Commit Parser is a simple tool for parsing conventional commits",
Long: "A simple tool for parsing conventional commits. The full specification for conventional commits is available at https://conventionalcommits.org/en/v1.0.0",
Version: func() string { return applicationVersion }(), // Use a func() here so we can override the variable using linker
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
xlog.SetLevel(xlog.InfoLevel)
if verbose {
xlog.SetLevel(xlog.DebugLevel)
}
return nil
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Determine the next build number to use",
Long: "Determine the next build number to use based on git tags or specified verisons",
RunE: runVersionCmd,
}
func runVersionCmd(cmd *cobra.Command, args []string) (err error) {
// Get the directory of the git repo. Default to the current working directory
if directory == "" {
directory, err = os.Getwd()
if err != nil {
return err
}
}
xlog.Debugf("Using directory %s", directory)
// Get the latest version tag in the git repo
latestVersion, err := git.GetLatestVersionInDirectory(directory)
if err != nil {
if current == "" || since == "" { // We only need this if either `current` or `since` were not provided
xlog.Warn("Unable to determine current version so suggesting initial version of 0.1.0")
fmt.Println("0.1.0")
return nil
}
}
// Get the current version number. Default to the latest tag on the branch
if current == "" {
current = latestVersion
}
xlog.Debugf("Using current version %s", current)
// Get the start point
if since == "" {
since = latestVersion
}
xlog.Debugf("Discovering commits since %s", since)
// Discover the commits
commits, err := git.GetCommitsInDirectory(since, "HEAD", directory)
if err != nil {
return err
}
// Compute the next version
v, err := GetNextVersion(current, commits, DefaultPatchTypes)
if err != nil {
return err
}
// Print the version
fmt.Println(v)
return nil
}
func setup() {
rootCmd.PersistentFlags().StringVarP(¤t, "current", "c", "", "Current version number from which to base the version change. Defaults to the latest version tag in the repository")
rootCmd.PersistentFlags().StringVarP(&directory, "directory", "d", "", "Directory of the git repository")
rootCmd.PersistentFlags().StringVarP(&since, "since", "s", "", "Revision to track commits from. Defaults to the latest version tag in the repository")
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Enable verbose logging")
rootCmd.AddCommand(versionCmd)
}