-
Notifications
You must be signed in to change notification settings - Fork 12
/
common.go
64 lines (49 loc) · 1.24 KB
/
common.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package proto
import (
"fmt"
"strings"
"time"
)
func TimeNow() *Timestamp {
return Time(time.Now())
}
func Time(s time.Time) *Timestamp {
return &Timestamp{
Seconds: int32(s.Unix()),
Nanos: int32(s.Nanosecond()),
}
}
func (t *Timestamp) Time() time.Time {
return time.Unix(int64(t.Seconds), int64(t.Nanos))
}
func (bi *BuildInfo) ToString() string {
attrs := []string{
fmt.Sprintf("os=%s", bi.Os),
fmt.Sprintf("arch=%s", bi.Arch),
}
if bi.Commit != "" {
attrs = append(attrs, fmt.Sprintf("commit=%s", bi.Commit[:8]))
}
if bi.Branch != "" {
attrs = append(attrs, fmt.Sprintf("branch=%s", bi.Branch))
}
if bi.Date != nil {
attrs = append(attrs, fmt.Sprintf("built-at=%s", bi.Date.Time().Format(time.RFC3339)))
}
if bi.BuiltBy != "" {
attrs = append(attrs, fmt.Sprintf("built-by=%s", bi.BuiltBy))
}
return fmt.Sprintf("%s (%s)", bi.Version, strings.Join(attrs, ", "))
}
func (bi *BuildInfos) ToString() string {
lines := ""
if bi.Client != nil {
lines += fmt.Sprintf("client: %s\n", bi.Client.ToString())
}
if bi.Daemon != nil {
lines += fmt.Sprintf("daemon: %s\n", bi.Daemon.ToString())
}
return lines
}