Skip to content

Commit 79c2dd6

Browse files
committed
feat: add option --version
1 parent 427d8d2 commit 79c2dd6

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

build/build.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
cd $(dirname "$0")
44

55
TMP='/tmp'
6-
LDFLAGS='-s -w'
76
OUTDIR='../output'
87
MAINNAME='ghfs'
9-
VERSION="$(git describe --abbrev=0 --tags 2> /dev/null || git rev-parse --abbrev-ref HEAD 2> /dev/null)"
8+
MOD=$(go list ../src/)
9+
VERSION=$(git describe --abbrev=0 --tags 2> /dev/null || git rev-parse --abbrev-ref HEAD 2> /dev/null)
10+
LDFLAGS="-s -w -X $MOD/version.appVer=$VERSION"
1011
LICENSE='../LICENSE'
1112

1213
mkdir -p "$OUTDIR"

src/param/cli.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"../goNixArgParser"
55
"../serverErrHandler"
66
"../util"
7+
"../version"
78
"errors"
89
"fmt"
910
"io/ioutil"
@@ -130,6 +131,9 @@ func init() {
130131
err = options.AddFlagValue("config", "--config", "", "", "print this help")
131132
serverErrHandler.CheckFatal(err)
132133

134+
err = options.AddFlag("version", "--version", "", "print version")
135+
serverErrHandler.CheckFatal(err)
136+
133137
err = options.AddFlags("help", []string{"-h", "--help"}, "", "print this help")
134138
serverErrHandler.CheckFatal(err)
135139
}
@@ -147,6 +151,12 @@ func doParseCli() []*Param {
147151
for i, length := 0, len(results); i < length; i++ {
148152
result := results[i]
149153

154+
// version
155+
if result.HasFlagKey("version") {
156+
version.PrintVersion()
157+
os.Exit(0)
158+
}
159+
150160
// help
151161
if result.HasFlagKey("help") {
152162
cliCmd.PrintHelp()

src/version/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
var appVer = "dev"
9+
10+
const entryFormat = "%-8s %s\n"
11+
12+
func PrintVersion() {
13+
fmt.Println("GHFS: Go HTTP File Server")
14+
fmt.Printf(entryFormat, "Version:", appVer)
15+
fmt.Printf(entryFormat, "SDK:", runtime.Version())
16+
fmt.Printf(entryFormat, "OS:", runtime.GOOS)
17+
fmt.Printf(entryFormat, "ARCH:", runtime.GOARCH)
18+
}

0 commit comments

Comments
 (0)