-
Notifications
You must be signed in to change notification settings - Fork 9
/
version.go
77 lines (61 loc) · 1.76 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
68
69
70
71
72
73
74
75
76
77
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package version
import (
"os"
"runtime/debug"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema/storage"
)
const DevelopmentBuildVersion = "dev"
// Set by goreleaser to the tag being released.
var Tag = DevelopmentBuildVersion
func Current() (*storage.NamespaceBinaryVersion, error) {
info, ok := debug.ReadBuildInfo()
if !ok {
return nil, fnerrors.InternalError("buildinfo is missing")
}
return VersionFrom(info)
}
func VersionFrom(info *debug.BuildInfo) (*storage.NamespaceBinaryVersion, error) {
var modified bool
var revision, buildtime string
for _, n := range info.Settings {
switch n.Key {
case "vcs.revision":
revision = n.Value
case "vcs.time":
buildtime = n.Value
case "vcs.modified":
modified = n.Value == "true"
}
}
if revision == "" {
return nil, fnerrors.InternalError("binary does not include version information")
}
v := &storage.NamespaceBinaryVersion{
Version: Tag,
GitCommit: revision,
BuildTimeStr: buildtime,
Modified: modified,
}
if v.BuildTimeStr != "" {
if parsed, err := time.Parse(time.RFC3339, v.BuildTimeStr); err == nil {
v.BuildTime = timestamppb.New(parsed)
}
}
return v, nil
}
func isDevelopmentBuild(ver *storage.NamespaceBinaryVersion) bool {
return ver.BuildTime == nil || ver.Version == DevelopmentBuildVersion
}
func ShouldCheckUpdate(ver *storage.NamespaceBinaryVersion) bool {
if isDevelopmentBuild(ver) {
return false
}
_, have := os.LookupEnv("NS_DO_NOT_UPDATE")
return !have
}