Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5e22769

Browse files
committed
feat: Add buildinfo package to embed version
This also resolves build time and commit hash using theGo 1.18 debug/buildinfo package. An external URL is outputtedon running version as well to easily route the caller to arelease or commit.
1 parentccba2ba commit5e22769

File tree

6 files changed

+137
-3
lines changed

6 files changed

+137
-3
lines changed

‎.goreleaser.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ before:
1616
builds:
1717
-id:coder-slim
1818
dir:cmd/coder
19-
ldflags:["-s -w"]
19+
ldflags:["-s -w -X github.com/coder/coder/cli/buildinfo.tag={{ .Version }}"]
2020
env:[CGO_ENABLED=0]
2121
goos:[darwin, linux, windows]
2222
goarch:[amd64]
@@ -28,7 +28,7 @@ builds:
2828
-id:coder
2929
dir:cmd/coder
3030
flags:[-tags=embed]
31-
ldflags:["-s -w"]
31+
ldflags:["-s -w -X github.com/coder/coder/cli/buildinfo.tag={{ .Version }}"]
3232
env:[CGO_ENABLED=0]
3333
goos:[darwin, linux, windows]
3434
goarch:[amd64, arm64]
@@ -58,3 +58,6 @@ nfpms:
5858

5959
release:
6060
ids:[coder, packages]
61+
62+
snapshot:
63+
name_template:'{{ .Version }}-devel+{{ .ShortCommit }}'

‎.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"coderd",
66
"coderdtest",
77
"codersdk",
8+
"devel",
89
"drpc",
910
"drpcconn",
1011
"drpcmux",

‎cli/buildinfo/buildinfo.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package buildinfo
2+
3+
import (
4+
"path"
5+
"runtime/debug"
6+
"sync"
7+
"time"
8+
9+
"golang.org/x/mod/semver"
10+
)
11+
12+
var (
13+
buildInfo*debug.BuildInfo
14+
buildInfoValidbool
15+
readBuildInfo sync.Once
16+
17+
// Injected with ldflags at build!
18+
tagstring
19+
)
20+
21+
// Version returns the semantic version of the build.
22+
// Use golang.org/x/mod/semver to compare versions.
23+
funcVersion()string {
24+
revision,valid:=revision()
25+
ifvalid {
26+
revision="+"+revision[:7]
27+
}
28+
iftag=="" {
29+
return"v0.0.0-devel"+revision
30+
}
31+
ifsemver.Build(tag)=="" {
32+
tag+=revision
33+
}
34+
return"v"+tag
35+
}
36+
37+
// ExternalURL returns a URL referencing the current Coder version.
38+
// For production builds, this will link directly to a release.
39+
// For development builds, this will link to a commit.
40+
funcExternalURL()string {
41+
repo:="https://github.com/coder/coder"
42+
revision,valid:=revision()
43+
if!valid {
44+
returnrepo
45+
}
46+
returnpath.Join(repo,"commit",revision)
47+
}
48+
49+
// Time returns when the Git revision was published.
50+
funcTime() (time.Time,bool) {
51+
value,valid:=find("vcs.time")
52+
if!valid {
53+
return time.Time{},false
54+
}
55+
parsed,err:=time.Parse(time.RFC3339,value)
56+
iferr!=nil {
57+
panic("couldn't parse time: "+err.Error())
58+
}
59+
returnparsed,true
60+
}
61+
62+
// revision returns the Git hash of the build.
63+
funcrevision() (string,bool) {
64+
returnfind("vcs.revision")
65+
}
66+
67+
// find panics if a setting with the specific key was not
68+
// found in the build info.
69+
funcfind(keystring) (string,bool) {
70+
readBuildInfo.Do(func() {
71+
buildInfo,buildInfoValid=debug.ReadBuildInfo()
72+
})
73+
if!buildInfoValid {
74+
panic("couldn't read build info")
75+
}
76+
for_,setting:=rangebuildInfo.Settings {
77+
ifsetting.Key!=key {
78+
continue
79+
}
80+
returnsetting.Value,true
81+
}
82+
return"",false
83+
}

‎cli/buildinfo/buildinfo_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package buildinfo_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"golang.org/x/mod/semver"
8+
9+
"github.com/coder/coder/cli/buildinfo"
10+
)
11+
12+
funcTestBuildInfo(t*testing.T) {
13+
t.Parallel()
14+
t.Run("Version",func(t*testing.T) {
15+
t.Parallel()
16+
version:=buildinfo.Version()
17+
require.True(t,semver.IsValid(version))
18+
prerelease:=semver.Prerelease(version)
19+
require.Equal(t,"-devel",prerelease)
20+
require.Equal(t,"v0",semver.Major(version))
21+
})
22+
t.Run("ExternalURL",func(t*testing.T) {
23+
t.Parallel()
24+
require.Equal(t,"https://github.com/coder/coder",buildinfo.ExternalURL())
25+
})
26+
// Tests don't include Go build info.
27+
t.Run("NoTime",func(t*testing.T) {
28+
t.Parallel()
29+
_,valid:=buildinfo.Time()
30+
require.False(t,valid)
31+
})
32+
}

‎cli/root.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"net/url"
55
"os"
66
"strings"
7+
"time"
78

89
"github.com/charmbracelet/lipgloss"
910
"github.com/kirsle/configdir"
1011
"github.com/mattn/go-isatty"
1112
"github.com/spf13/cobra"
1213

14+
"github.com/coder/coder/cli/buildinfo"
1315
"github.com/coder/coder/cli/cliui"
1416
"github.com/coder/coder/cli/config"
1517
"github.com/coder/coder/codersdk"
@@ -28,6 +30,7 @@ const (
2830
funcRoot()*cobra.Command {
2931
cmd:=&cobra.Command{
3032
Use:"coder",
33+
Version:buildinfo.Version(),
3134
SilenceUsage:true,
3235
Long:` ▄█▀ ▀█▄
3336
▄▄ ▀▀▀ █▌ ██▀▀█▄ ▐█
@@ -55,6 +58,7 @@ func Root() *cobra.Command {
5558
`Flags:`,header.Render("Flags:"),
5659
`Additional help topics:`,header.Render("Additional help:"),
5760
).Replace(cmd.UsageTemplate()))
61+
cmd.SetVersionTemplate(versionTemplate())
5862

5963
cmd.AddCommand(
6064
configSSH(),
@@ -142,3 +146,14 @@ func isTTY(cmd *cobra.Command) bool {
142146
}
143147
returnisatty.IsTerminal(file.Fd())
144148
}
149+
150+
funcversionTemplate()string {
151+
template:=`Coder {{printf "%s" .Version}}`
152+
buildTime,valid:=buildinfo.Time()
153+
ifvalid {
154+
template+=" "+buildTime.Format(time.UnixDate)
155+
}
156+
template+="\r\n"+buildinfo.ExternalURL()
157+
template+="\r\n"
158+
returntemplate
159+
}

‎go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ require (
234234
github.com/zclconf/go-ctyv1.10.0// indirect
235235
github.com/zeebo/errsv1.2.2// indirect
236236
go.opencensus.iov0.23.0// indirect
237-
golang.org/x/modv0.5.1// indirect
237+
golang.org/x/modv0.5.1
238238
golang.org/x/netv0.0.0-20220325170049-de3da57026de// indirect
239239
golang.org/x/termv0.0.0-20210927222741-03fcf44c2211
240240
golang.org/x/textv0.3.7// indirect

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp