Skip to content

Commit

Permalink
feat: Starting to parse the version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Curtis Jewell committed Dec 29, 2023
1 parent 3704a42 commit 86c2ccc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 13 deletions.
26 changes: 13 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cmd

import (
"errors"
"fmt"
"log/slog"
"os"
"path"
Expand All @@ -41,10 +42,11 @@ const (
versionMajor VersionSegment = iota
versionMinor
versionPatch
versionPre
versionAlpha
versionBeta
versionGamma
versionRC
versionPre
)

var (
Expand All @@ -55,21 +57,18 @@ var (

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "git-next-tag",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Use: "git next-tag",
Version: FullVersion(),
Short: "Commit the next tag.",
Long: `...`,
PreRunE: func(cmd *cobra.Command, args []string) error { return initConfig() },
RunE: func(cmd *cobra.Command, args []string) error { return nextTag(cmd, args) },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
fmt.Println(Version())
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand All @@ -81,9 +80,9 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.Flags().Bool("major", false, "Increment major")
rootCmd.Flags().Bool("minor", false, "Increment minor")
rootCmd.Flags().Bool("patch", false, "Increment patch")
rootCmd.Flags().Bool("major", false, "Increment major version")
rootCmd.Flags().Bool("minor", false, "Increment minor version")
rootCmd.Flags().Bool("patch", false, "Increment patch version")
}

// initConfig reads in config file and ENV variables if set.
Expand Down Expand Up @@ -115,9 +114,10 @@ func initConfig() error {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
slog.Debug("Using config file:", viper.ConfigFileUsed())
slog.Debug("Using config file:" + viper.ConfigFileUsed())
} else {
// Initialize the file.
// TODO: Make this a 'question and answer' bit
viper.Set("initial_v", true)
viper.Set("tag_annotated", true)
viper.Set("version_files", []string{})
Expand Down
85 changes: 85 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright © 2023 Curtis Jewell <golang@curtisjewell.name>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"fmt"
"regexp"
"runtime/debug"
)

// From https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/,
// altered to the use case.

// commit is the commit hash
// uncommitted is whether there were other uncommitted changes when built
// FullCommit is the full commit string.
var commit, uncommitted, FullCommit = func() (string, bool, string) {
commit := ""
modified := false
commitInfo := ""
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
commit = setting.Value
case "vcs.modified":
if setting.Value == "true" {
modified = true
commitInfo = " + uncommitted changes"
}
}
}
}
if commit == "" {
return "unknown", false, " status - not built from repository"
}
return commit, modified, fmt.Sprint(commit, commitInfo)
}()

// version is the
var version = func() string { return "v0.0.0+pre" }()

var rxVersion *regexp.Regexp

func init() {
rxVersion, _ = regexp.Compile(`v?(\d+)[.](\d+)[.](\d+)(?:-(alpha|beta|gamma|rc)[.](\d+))?(?:[+](pre))?`)
}

// FullVersion returns the version, including git status.
func FullVersion() string {
v := version
matches := rxVersion.FindStringSubmatch(v)
if len(matches) > 0 && matches[6] == "pre" {
v += fmt.Sprintf(" (snapshot: %s)", FullCommit)
}
return v
}

// Version returns the base version.
func Version() string {
return version
}

// TODO
// struct ParsedVersion {}
// func GetParsedVersion() *ParsedVersion {}

0 comments on commit 86c2ccc

Please sign in to comment.