Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2709738

Browse files
committed
add version
1 parent bc75141 commit 2709738

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

websites/code/studygolang/install

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ if [ ! -d log ]; then
1515
mkdir log
1616
fi
1717

18+
VERSIONCMD = "`git symbolic-ref HEAD | cut -b 12-`-`git rev-parse HEAD`"
19+
VERSION = $(shell echo $(VERSIONCMD))
20+
DATE = $(shell echo `date +%F\ %T%z`)
21+
1822
gofmt -w src
1923

20-
go install server/studygolang
21-
go install server/crawlarticle
22-
go install server/indexer
24+
go install -ldflags "-X util.version 1.0.0 -X util.date %date%" ./...
2325

2426
export GOPATH="$OLDGOPATH"
2527
export PATH="$OLDPATH"

websites/code/studygolang/install.bat

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ set GOPATH=%~dp0;%~dp0..\thirdparty
1313

1414
if not exist log mkdir log
1515

16+
set VERSION=git symbolic-ref HEAD | cut -b 12-
17+
set VERSION=%VERSION%-git rev-parse HEAD
18+
1619
gofmt -w src
1720

1821
:: -tags "debug" 表示测试
19-
go install -tags "debug" server/studygolang
20-
go install -tags "debug" server/crawlarticle
21-
go install -tags "debug" server/indexer
22+
go install -tags "debug" -ldflags "-X util.version 1.0.0 -X util.date %date%" ./...
2223

2324
set GOPATH=%OLDGOPATH%
2425

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2014 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author:polaris [email protected]
6+
7+
/*
8+
Package version sets version information for the binary where it is imported.
9+
The version can be retrieved either from the -version command line argument,
10+
or from the /debug/version/ http endpoint.
11+
12+
To include in a project simply import the package and call version.Init().
13+
14+
The version and compile date is stored in version and date variables and
15+
are supposed to be set during compile time. Typically this is done by the
16+
install(bash/bat).
17+
18+
To set these manually use -ldflags together with -X, like in this example:
19+
20+
go install -ldflags "-X util.version v1.0"
21+
22+
*/
23+
24+
package util
25+
26+
import (
27+
"flag"
28+
"fmt"
29+
"html"
30+
"io"
31+
"net/http"
32+
"os"
33+
)
34+
35+
var showVersion = flag.Bool("version", false, "Print version of this binary (only valid if compiled with install(bash/bat))")
36+
37+
var (
38+
version string
39+
date string
40+
)
41+
42+
func printVersion(w io.Writer, version string, date string) {
43+
fmt.Fprintf(w, "Version: %s\n", version)
44+
fmt.Fprintf(w, "Binary: %s\n", os.Args[0])
45+
fmt.Fprintf(w, "Compile date: %s\n", date)
46+
fmt.Fprintf(w, "(version and date only valid if compiled with install(bash/bat))\n")
47+
}
48+
49+
// initializes the version flag and /debug/version/ http endpoint.
50+
// Note that this method will call flag.Parse if the flags are not already parsed.
51+
func init() {
52+
if !flag.Parsed() {
53+
flag.Parse()
54+
}
55+
if showVersion != nil && *showVersion {
56+
printVersion(os.Stdout, version, date)
57+
os.Exit(0)
58+
}
59+
60+
http.Handle("/version", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61+
printVersion(w, html.EscapeString(version), html.EscapeString(date))
62+
}))
63+
}

0 commit comments

Comments
 (0)