Skip to content

Commit

Permalink
chore: rework version library
Browse files Browse the repository at this point in the history
Use caarlos0/go-version for version info, which ultimately
relies on go's `runtime/debug`.

This "should" allow go install to populate gitVersion when,
however, it may not either - yolo.
  • Loading branch information
retr0h committed May 6, 2024
1 parent 8a48e16 commit c1a9e9f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 195 deletions.
4 changes: 4 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
before:
hooks:
- go mod tidy

builds:
- env:
- CGO_ENABLED=0
Expand All @@ -12,6 +13,9 @@ builds:
- -s -w -X {{ .ModulePath }}/cmd.date={{ .CommitDate }}
- -X {{ .ModulePath }}/cmd.commit={{ .Commit }}
- -X {{ .ModulePath }}/cmd.version={{ .Version }}
- -X {{ .ModulePath }}/cmd.builtBy=goreleaser
- -X {{ .ModulePath }}/cmd.treestate={{ .IsGitDirty }}

archives:
- format: binary
checksum:
Expand Down
9 changes: 9 additions & 0 deletions cmd/resources/art.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

o o
o | |
o--o | -o-
| | | | |
o--O | o o
|
o--o

23 changes: 10 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package cmd

import (
_ "embed"
"fmt"
"log/slog"
"os"
"strings"
Expand All @@ -37,25 +39,20 @@ import (
var (
logger *slog.Logger
appConfig config.Repositories
//go:embed resources/art.txt
asciiArt string
)

const (
desc = "A GIT layering command line tool.\n"
website = "https://github.com/retr0h/gilt"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gilt",
Short: "A GIT layering command line tool",
Long: `
o o
o | |
o--o | -o-
| | | | |
o--O | o o
|
o--o
A GIT layering command line tool.
https://github.com/retr0h/gilt
`,
Long: fmt.Sprintf("%sgilt: %s\n%s", asciiArt, desc, website),
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
81 changes: 31 additions & 50 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,74 +21,55 @@
package cmd

import (
"encoding/json"
"fmt"

goversion "github.com/caarlos0/go-version"
"github.com/spf13/cobra"
goVersion "go.hein.dev/go-version"
)

// Info creates a formattable struct for output.
type Info struct {
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
Date string `json:"date,omitempty"`
}

// getVersion return the sensor's VersionInfo details.
func getVersion() *Info {
versionOutput := goVersion.New(version, commit, date)

output := &Info{
Version: versionOutput.Version,
Commit: versionOutput.Commit,
Date: versionOutput.Date,
}

return output
}

// toJSON converts the Info into a JSON String.
func (v *Info) toJSON() string {
bytes, _ := json.Marshal(v)

return string(bytes)
}

// toShortened converts the Version into a String.
func (v *Info) toShortened() string {
return fmt.Sprintf("Version: %s\n", v.Version)
func buildVersion(version, commit, date, builtBy, treeState string) goversion.Info {
return goversion.GetVersionInfo(
goversion.WithAppDetails("gilt", desc, website),
goversion.WithASCIIName(asciiArt),
func(i *goversion.Info) {
if commit != "" {
i.GitCommit = commit
}
if treeState != "" {
i.GitTreeState = treeState
}
if date != "" {
i.BuildDate = date
}
if version != "" {
i.GitVersion = version
}
if builtBy != "" {
i.BuiltBy = builtBy
}
},
)
}

// versionCmd represents the version command.
var (
shortened = false
version = "dev"
commit = "none"
date = "unknown"
output = "json"
version = ""
commit = ""
treeState = ""
date = ""
builtBy = ""
versionCmd = &cobra.Command{
Use: "version",
Short: "Display the version of tool",
Run: func(cmd *cobra.Command, args []string) {
var response string

versionInfo := getVersion()

if shortened {
response = versionInfo.toShortened()
} else {
response = versionInfo.toJSON()
}
version := buildVersion(version, commit, date, builtBy, treeState)

fmt.Printf("%s\n", response)
jsonOut, _ := version.JSONString()
fmt.Println(jsonOut)
},
}
)

func init() {
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.")
versionCmd.Flags().
StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.")
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.22

require (
github.com/avfs/avfs v0.33.0
github.com/caarlos0/go-version v0.1.1
github.com/danjacques/gofslock v0.0.0-20230728142113-ae8f59f9e88b
github.com/go-playground/validator/v10 v10.20.0
github.com/golang/mock v1.6.0
github.com/lmittmann/tint v1.0.4
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
go.hein.dev/go-version v0.1.0
golang.org/x/term v0.19.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -45,5 +45,4 @@ require (
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit c1a9e9f

Please sign in to comment.