-
Notifications
You must be signed in to change notification settings - Fork 18
/
version.go
67 lines (57 loc) · 1.59 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package build
import (
_ "embed"
"encoding/json"
"fmt"
"os"
"github.com/filecoin-project/lassie/pkg/internal/revision"
)
var (
// version is the built version.
// Set with ldflags in .goreleaser.yaml via -ldflags="-X github.com/filecoin-project/lassie/pkg/build.version=v{{.Version}}".
version string
// Version returns the current version of the Lassie application
Version string
UserAgent string
)
const (
defaultVersion string = "v0.0.0" // Default version if not set by ldflags
versionFile string = "version.json" // Version file path
)
func init() {
if version == "" {
// This is being ran in development, try to grab the latest known version from the version.json file
var err error
version, err = readVersionFromFile()
if err != nil {
// Use the default version
version = defaultVersion
}
}
Version = fmt.Sprintf("%s-%s", version, revision.Revision)
UserAgent = fmt.Sprintf("lassie/%s", Version)
}
// versionJson is used to read the local version.json file
type versionJson struct {
Version string `json:"version"`
}
// readVersionFromFile reads the version from the version.json file.
// Reading this should be fine in development since the version.json file
// should be present in the project, I hope :)
func readVersionFromFile() (string, error) {
// Open file
file, err := os.Open(versionFile)
if err != nil {
return "", err
}
defer file.Close()
// Decode json into struct
decoder := json.NewDecoder(file)
var vJson versionJson
err = decoder.Decode(&vJson)
if err != nil {
return "", err
}
// Read version from json
return vJson.Version, nil
}