/
string.go
65 lines (58 loc) · 1.54 KB
/
string.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
//
// (C) Copyright 2023 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package build
import (
"encoding/json"
"fmt"
"strings"
"time"
)
func revString(version string) string {
if ReleaseBuild {
return version
}
revParts := []string{version}
if Revision != "" {
switch VCS {
case "git":
revParts = append(revParts, fmt.Sprintf("g%7s", Revision)[0:7])
default:
revParts = append(revParts, Revision)
}
if DirtyBuild {
revParts = append(revParts, "dirty")
}
}
return strings.Join(revParts, "-")
}
// String returns a string containing the name, version, and for non-release builds,
// the revision of the binary.
func String(name string) string {
return fmt.Sprintf("%s version %s", name, revString(DaosVersion))
}
// MarshalJSON returns a JSON string containing a structured representation of
// the binary build info.
func MarshalJSON(name string) ([]byte, error) {
// Not a fatal error if the build time can't be parsed.
buildTime, _ := time.Parse(time.RFC3339, BuildTime)
return json.Marshal(&struct {
Name string `json:"name"`
Version string `json:"version"`
Revision string `json:"revision,omitempty"`
Dirty bool `json:"dirty,omitempty"`
Release bool `json:"release,omitempty"`
BuildHost string `json:"build_host,omitempty"`
BuildTime time.Time `json:"build_time,omitempty"`
}{
Name: name,
Version: DaosVersion,
Revision: Revision,
Dirty: DirtyBuild,
Release: ReleaseBuild,
BuildHost: BuildHost,
BuildTime: buildTime,
})
}