Skip to content

Commit

Permalink
Merge pull request #644 from twpayne/fix-version
Browse files Browse the repository at this point in the history
Fix version
  • Loading branch information
twpayne committed Mar 29, 2020
2 parents 2db0aad + 1257ea8 commit 6ebf442
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Install tools
if: matrix.os == 'ubuntu-latest'
run: |
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.8
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0
cd $(mktemp -d)
go mod init tmp
go get mvdan.cc/gofumpt/gofumports
Expand Down 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ generate:

.PHONY: install-tools
install-tools:
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- v1.23.8
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- v1.24.0
( cd $$(mktemp -d) && go mod init tmp && go get mvdan.cc/gofumpt/gofumports )

.PHONY: lint
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 6ebf442

Please sign in to comment.