Skip to content

Commit

Permalink
Refactor version number embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 29, 2020
1 parent f92a292 commit 1257ea8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ jobs:
sudo snap install goreleaser --classic
sudo snap install snapcraft --classic
goreleaser release --skip-publish --snapshot
# verify that version information is embedded correctly
./dist/chezmoi-cgo_linux_amd64/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v"
./dist/chezmoi-cgo_linux_amd64/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v"
./dist/chezmoi-nocgo_linux_386/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v"
./dist/chezmoi-nocgo-snap_linux_386/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v"
release:
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
needs:
Expand Down
4 changes: 0 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ before:
builds:
- id: chezmoi-cgo
binary: chezmoi
ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}"
env:
- CGO_ENABLED=1
goos:
Expand All @@ -18,7 +17,6 @@ builds:
binary: chezmoi
env:
- CGO_ENABLED=0
ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}"
goos:
- linux
- darwin
Expand All @@ -43,7 +41,6 @@ builds:
binary: chezmoi
flags:
- -tags=snap
ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}"
env:
- CGO_ENABLED=1
goos:
Expand All @@ -54,7 +51,6 @@ builds:
binary: chezmoi
flags:
- -tags=snap
ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}"
env:
- CGO_ENABLED=0
goos:
Expand Down
2 changes: 1 addition & 1 deletion cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func (c *doctorSuspiciousFilesCheck) Skip() bool {
}

func (doctorVersionCheck) Check() (bool, error) {
if VersionStr == devVersionStr || Commit == unknownStr || Date == unknownStr {
if VersionStr == "" || Commit == "" || Date == "" {
return false, nil
}
return true, nil
Expand Down
50 changes: 27 additions & 23 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ var config = newConfig()

// Version information.
var (
devVersionStr = "dev"
unknownStr = "unknown"
VersionStr = devVersionStr
Commit = unknownStr
Date = unknownStr
Version *semver.Version
VersionStr string
Commit string
Date string
BuiltBy string
Version *semver.Version
)

var rootCmd = &cobra.Command{
Expand All @@ -35,23 +34,6 @@ var rootCmd = &cobra.Command{
}

func init() {
if VersionStr != devVersionStr {
var err error
Version, err = semver.NewVersion(strings.TrimPrefix(VersionStr, "v"))
if err != nil {
printErrorAndExit(err)
}
}

versionComponents := []string{VersionStr}
if Commit != unknownStr {
versionComponents = append(versionComponents, "commit "+Commit)
}
if Date != unknownStr {
versionComponents = append(versionComponents, "built at "+Date)
}
rootCmd.Version = strings.Join(versionComponents, ", ")

homeDir, err := os.UserHomeDir()
if err != nil {
printErrorAndExit(err)
Expand Down Expand Up @@ -114,6 +96,28 @@ func init() {

// Execute executes the root command.
func Execute() {
var versionComponents []string
if VersionStr != "" {
var err error
Version, err = semver.NewVersion(strings.TrimPrefix(VersionStr, "v"))
if err != nil {
printErrorAndExit(err)
}
versionComponents = append(versionComponents, VersionStr)
} else {
versionComponents = append(versionComponents, "dev")
}
if Commit != "" {
versionComponents = append(versionComponents, "commit "+Commit)
}
if Date != "" {
versionComponents = append(versionComponents, "built at "+Date)
}
if BuiltBy != "" {
versionComponents = append(versionComponents, "built by "+BuiltBy)
}
rootCmd.Version = strings.Join(versionComponents, ", ")

if err := rootCmd.Execute(); err != nil {
printErrorAndExit(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *Config) runUpgradeCmd(cmd *cobra.Command, args []string) error {

// If the upgrade is not forced and we're not a dev version, stop if we're
// already the latest version.
if !c.upgrade.force && VersionStr != devVersionStr {
if !c.upgrade.force && VersionStr != "" {
if !Version.LessThan(*releaseVersion) {
fmt.Fprintf(c.Stdout, "chezmoi: already at the latest version (%s)\n", Version)
return nil
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ package main

import "github.com/twpayne/chezmoi/cmd"

var (
version = ""
commit = ""
date = ""
builtBy = ""
)

func main() {
cmd.VersionStr = version
cmd.Commit = commit
cmd.Date = date
cmd.BuiltBy = builtBy
cmd.Execute()
}

0 comments on commit 1257ea8

Please sign in to comment.