Skip to content

Commit f84723e

Browse files
committed
Add version information
1 parent 2045751 commit f84723e

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

.goreleaser.yml

+2
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ release:
2929
github:
3030
owner: breml
3131
name: bidichk
32+
gomod:
33+
proxy: true

cmd/bidichk/main.go

+31
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
11
package main
22

33
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"runtime"
8+
"runtime/debug"
9+
410
"golang.org/x/tools/go/analysis/singlechecker"
511

612
"github.com/breml/bidichk/pkg/bidichk"
713
)
814

15+
var (
16+
version = "development"
17+
commit = ""
18+
date = ""
19+
)
20+
921
func main() {
22+
bidichk.Version = buildVersion()
23+
1024
singlechecker.Main(bidichk.NewAnalyzer())
1125
}
26+
27+
func buildVersion() string {
28+
result := fmt.Sprintf("%s version %s", filepath.Base(os.Args[0]), version)
29+
30+
if commit != "" {
31+
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
32+
}
33+
if date != "" {
34+
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
35+
}
36+
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
37+
result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
38+
}
39+
result = fmt.Sprintf("%s\ngoos: %s\ngoarch: %s", result, runtime.GOOS, runtime.GOARCH)
40+
41+
return result
42+
}

pkg/bidichk/bidichk.go

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func NewAnalyzer() *analysis.Analyzer {
136136

137137
a.Flags.Init("bidichk", flag.ExitOnError)
138138
a.Flags.Var(&bidichk.disallowedRunes, "disallowed-runes", disallowedDoc)
139+
a.Flags.Var(versionFlag{}, "V", "print version and exit")
139140

140141
return a
141142
}

pkg/bidichk/version.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package bidichk
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
var Version = "bidichk version dev"
9+
10+
type versionFlag struct{}
11+
12+
func (versionFlag) IsBoolFlag() bool { return true }
13+
func (versionFlag) Get() interface{} { return nil }
14+
func (versionFlag) String() string { return "" }
15+
func (versionFlag) Set(s string) error {
16+
fmt.Println(Version)
17+
os.Exit(0)
18+
return nil
19+
}

0 commit comments

Comments
 (0)