Skip to content

Commit b2b1bcb

Browse files
authored
Merge pull request #33 from nao1215/fix-version-control
Changed to get version information from ldflags
2 parents f5e2e85 + 25b0ac6 commit b2b1bcb

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

.goreleaser.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ before:
88
builds:
99
- main: .
1010
ldflags:
11-
- -s -w
11+
- -s -w '-X github.com/nao1215/gup/internal/cmdinfo.Version=v{{ .Version }}'
1212
env:
1313
- CGO_ENABLED=0
1414
goos:

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.PHONY: build test clean vet fmt chkfmt
22

33
APP = gup
4+
VERSION = $(shell git describe --tags --abbrev=0)
45
GO = go
56
GO_BUILD = $(GO) build
67
GO_FORMAT = $(GO) fmt
@@ -14,6 +15,7 @@ GOOS = linux
1415
GOARCH = amd64
1516
GO_PKGROOT = ./...
1617
GO_PACKAGES = $(shell $(GO_LIST) $(GO_PKGROOT))
18+
GO_LDFLAGS = -ldflags '-X github.com/nao1215/gup/internal/cmdinfo.Version=${VERSION}'
1719

1820
build: ## Build binary
1921
env GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) $(GO_LDFLAGS) -o $(APP) main.go

cmd/version.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99

1010
var versionCmd = &cobra.Command{
1111
Use: "version",
12-
Short: "Show " + cmdinfo.Name() + " command version information",
12+
Short: "Show " + cmdinfo.Name + " command version information",
1313
Run: func(cmd *cobra.Command, args []string) {
14-
fmt.Println(cmdinfo.Version())
14+
fmt.Println(cmdinfo.GetVersion())
1515
},
1616
}
1717

internal/cmdinfo/cmdinfo.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ package cmdinfo
22

33
import (
44
"fmt"
5+
"runtime/debug"
56
)
67

7-
const (
8-
name = "gup"
9-
version = "0.10.3"
10-
)
8+
// Version value is set by ldflags
9+
var Version string
1110

12-
// Version return gup command version.
13-
func Version() string {
14-
return fmt.Sprintf("%s version %s (under Apache License version 2.0)",
15-
Name(), version)
16-
}
11+
// Name is command name
12+
const Name = "gup"
1713

18-
// Name return command name.
19-
func Name() string {
20-
return name
14+
// GetVersion return gup command version.
15+
// Version global variable is set by ldflags.
16+
func GetVersion() string {
17+
version := "unknown"
18+
if Version != "" {
19+
version = Version
20+
} else if buildInfo, ok := debug.ReadBuildInfo(); ok {
21+
version = buildInfo.Main.Version
22+
}
23+
return fmt.Sprintf("%s version %s (under Apache License version 2.0)", Name, version)
2124
}

internal/completion/completion.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ func bashCompletionFilePath() string {
246246

247247
// fishCompletionFilePath return fish-completion file path.
248248
func fishCompletionFilePath() string {
249-
return filepath.Join(os.Getenv("HOME"), ".config", "fish", "completions", cmdinfo.Name()+".fish")
249+
return filepath.Join(os.Getenv("HOME"), ".config", "fish", "completions", cmdinfo.Name+".fish")
250250
}
251251

252252
// zshCompletionFilePath return zsh-completion file path.
253253
func zshCompletionFilePath() string {
254-
return filepath.Join(os.Getenv("HOME"), ".zsh", "completion", "_"+cmdinfo.Name())
254+
return filepath.Join(os.Getenv("HOME"), ".zsh", "completion", "_"+cmdinfo.Name)
255255
}
256256

257257
// zshrcPath return .zshrc path.

internal/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func DirPath() string {
2626
// current directory. The .config directory path is displayed
2727
// when reporting the completion of the export subcommand.
2828
// So, user notices that the output destination is strange.
29-
return filepath.Join(os.Getenv("HOME"), ".config", cmdinfo.Name())
29+
return filepath.Join(os.Getenv("HOME"), ".config", cmdinfo.Name)
3030
}
31-
return filepath.Join(home, ".config", cmdinfo.Name())
31+
return filepath.Join(home, ".config", cmdinfo.Name)
3232
}
3333

3434
// ReadConfFile return contents of configuration-file (package information)

internal/print/print.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ var (
2020
// Info print information message at STDOUT.
2121
func Info(msg string) {
2222
fmt.Fprintf(Stdout, "%s:%s: %s\n",
23-
cmdinfo.Name(), color.GreenString("INFO "), msg)
23+
cmdinfo.Name, color.GreenString("INFO "), msg)
2424
}
2525

2626
// Warn print warning message at STDERR.
2727
func Warn(err interface{}) {
2828
fmt.Fprintf(Stderr, "%s:%s: %v\n",
29-
cmdinfo.Name(), color.YellowString("WARN "), err)
29+
cmdinfo.Name, color.YellowString("WARN "), err)
3030
}
3131

3232
// Err print error message at STDERR.
3333
func Err(err interface{}) {
3434
fmt.Fprintf(Stderr, "%s:%s: %v\n",
35-
cmdinfo.Name(), color.HiYellowString("ERROR"), err)
35+
cmdinfo.Name, color.HiYellowString("ERROR"), err)
3636
}
3737

3838
// Fatal print dying message at STDERR.
3939
func Fatal(err interface{}) {
4040
fmt.Fprintf(Stderr, "%s:%s: %v\n",
41-
cmdinfo.Name(), color.RedString("FATAL"), err)
41+
cmdinfo.Name, color.RedString("FATAL"), err)
4242
os.Exit(1)
4343
}
4444

@@ -47,7 +47,7 @@ func Question(ask string) bool {
4747
var response string
4848

4949
fmt.Fprintf(Stdout, "%s:%s: %s",
50-
cmdinfo.Name(), color.GreenString("CHECK"), ask+" [Y/n] ")
50+
cmdinfo.Name, color.GreenString("CHECK"), ask+" [Y/n] ")
5151
_, err := fmt.Scanln(&response)
5252
if err != nil {
5353
// If user input only enter.

0 commit comments

Comments
 (0)