forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
45 lines (38 loc) · 1.13 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package webrpc
import (
_ "embed"
"os/exec"
"strings"
)
// Version of webrpc-gen tooling & Template Functions API.
// Available as {{.WebrpcGenVersion}} variable in Go templates.
//
// The value is injected during `go build' in the release CI step.
var VERSION = ""
//go:embed go.mod
var GoModFile string
func init() {
if VERSION == "" {
VERSION = getRuntimeVersion()
}
}
// getRuntimeVersion tries to infer webrpc version
// 1. from the current go.mod file, which is useful when running webrpc-gen from
// another Go module using `go run github.com/webrpc/webrpc/cmd/webrpc-gen'.
// 2. from the current git history.
func getRuntimeVersion() string {
// $ go list -m github.com/webrpc/webrpc
// github.com/webrpc/webrpc v0.15.1\n
if out, _ := exec.Command("go", "list", "-m", "github.com/webrpc/webrpc").Output(); len(out) > 0 {
parts := strings.Split(strings.TrimSpace(string(out)), " ")
if len(parts) >= 2 {
return parts[1]
}
}
// $ git describe --tags
// v0.15.1-6-g550333d\n
if out, _ := exec.Command("git", "describe", "--tags").Output(); len(out) > 0 {
return strings.TrimSpace(string(out))
}
return "unknown"
}