diff --git a/cmd/gogitver/cmd/root.go b/cmd/gogitver/cmd/root.go index ee9158b..522a31b 100644 --- a/cmd/gogitver/cmd/root.go +++ b/cmd/gogitver/cmd/root.go @@ -19,10 +19,23 @@ var rootCmd = &cobra.Command{ Run: runRoot, } +var prereleaseCmd = &cobra.Command{ + Use: "label", + Short: "Gets the prerelease label, if any", + Long: ``, + Run: runPrerelease, +} + func init() { - rootCmd.Flags().String("path", ".", "the path to the git repository") - rootCmd.Flags().String("settings", "./.gogitver.yaml", "the file that contains the settings") + var cmds = [2]*cobra.Command{rootCmd, prereleaseCmd} + for _, cmd := range cmds { + cmd.Flags().String("path", ".", "the path to the git repository") + cmd.Flags().String("settings", "./.gogitver.yaml", "the file that contains the settings") + } + rootCmd.Flags().Bool("forbid-behind-master", false, "error if the current branch's calculated version is behind the calculated version of refs/heads/master") + + rootCmd.AddCommand(prereleaseCmd) } // Execute gogitver @@ -33,16 +46,12 @@ func Execute() { } } -func runRoot(cmd *cobra.Command, args []string) { +func getRepoAndSettings(cmd *cobra.Command) (*gogit.Repository, *git.Settings) { f := cmd.Flag("path") sf := cmd.Flag("settings") - fbm, err := strconv.ParseBool(cmd.Flag("forbid-behind-master").Value.String()) - if err != nil { - fbm = false - } var s *git.Settings - _, err = os.Stat(sf.Value.String()) + _, err := os.Stat(sf.Value.String()) if sf.Changed || err == nil { r, err := os.Open(sf.Value.String()) if err != nil { @@ -62,6 +71,17 @@ func runRoot(cmd *cobra.Command, args []string) { panic(err) } + return r, s +} + +func runRoot(cmd *cobra.Command, args []string) { + r, s := getRepoAndSettings(cmd) + + fbm, err := strconv.ParseBool(cmd.Flag("forbid-behind-master").Value.String()) + if err != nil { + fbm = false + } + version, err := git.GetCurrentVersion(r, s, false, fbm) if err != nil { panic(err) @@ -69,3 +89,18 @@ func runRoot(cmd *cobra.Command, args []string) { fmt.Println(version) } + +func runPrerelease(cmd *cobra.Command, args[]string) { + r, s := getRepoAndSettings(cmd) + + label, err := git.GetPrereleaseLabel(r, s) + if err != nil { + panic(err) + } + + if label == "master" { + label = "" + } + + fmt.Println(label) +} diff --git a/pkg/git/git.go b/pkg/git/git.go index 74737ce..198eb97 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -78,6 +78,15 @@ func GetCurrentVersion(r *git.Repository, settings *Settings, ignoreTravisTag bo return v.String(), nil } +// GetPrereleaseLabel returns the prerelease label for the current branch +func GetPrereleaseLabel(r *git.Repository, settings *Settings) (result string, err error) { + h, err := r.Head() + if err != nil { + return "", errors.Wrap(err, "GetCurrentVersion failed") + } + return getCurrentBranch(r, h) +} + func getVersion(r *git.Repository, h *plumbing.Reference, tagMap map[string]string, forbidBehindMaster bool, settings *Settings) (version *semver.Version, err error) { currentBranch, err := getCurrentBranch(r, h) if err != nil {