Skip to content

Commit 5038ff6

Browse files
committed
v0.0.19: add version command, misc bug fixes
1 parent 0c2f9bd commit 5038ff6

File tree

7 files changed

+112
-5
lines changed

7 files changed

+112
-5
lines changed

api/api.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/anchordotdev/cli"
1717
"github.com/anchordotdev/cli/keyring"
18+
"github.com/anchordotdev/cli/version"
1819
"golang.org/x/exp/slices"
1920
)
2021

@@ -40,7 +41,9 @@ func NewClient(cfg *cli.Config) (*Session, error) {
4041
Client: &http.Client{
4142
Transport: urlRewriter{
4243
RoundTripper: responseChecker{
43-
RoundTripper: new(http.Transport),
44+
RoundTripper: userAgentSetter{
45+
RoundTripper: new(http.Transport),
46+
},
4447
},
4548
URL: cfg.API.URL,
4649
},
@@ -345,6 +348,16 @@ func (r urlRewriter) RoundTrip(req *http.Request) (*http.Response, error) {
345348
return r.RoundTripper.RoundTrip(req)
346349
}
347350

351+
type userAgentSetter struct {
352+
http.RoundTripper
353+
}
354+
355+
func (s userAgentSetter) RoundTrip(req *http.Request) (*http.Response, error) {
356+
req.Header.Set("User-Agent", version.UserAgent())
357+
358+
return s.RoundTripper.RoundTrip(req)
359+
}
360+
348361
type mediaTypes []string
349362

350363
func (s mediaTypes) Matches(val string) bool {

cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ type Config struct {
8282
Keyring struct {
8383
MockMode bool `env:"ANCHOR_CLI_KEYRING_MOCK_MODE"`
8484
}
85+
86+
Version struct{} `cmd:"version"`
8587
}
8688

8789
type UI struct {

cmd/anchor/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/anchordotdev/cli/lcl"
1616
"github.com/anchordotdev/cli/trust"
1717
"github.com/anchordotdev/cli/ui"
18+
versionpkg "github.com/anchordotdev/cli/version"
1819
)
1920

2021
var (
@@ -166,6 +167,13 @@ var (
166167
},
167168
},
168169
},
170+
{
171+
UI: versionpkg.Command{}.UI(),
172+
173+
Name: "version",
174+
Use: "version",
175+
Short: "Show version info",
176+
},
169177
},
170178

171179
Preflight: versionCheck,
@@ -175,11 +183,15 @@ var (
175183

176184
// Version info set by GoReleaser via ldflags
177185

178-
version = "dev"
179-
commit = "none" //lint:ignore U1000 set by GoReleaser
180-
date = "unknown" //lint:ignore U1000 set by GoReleaser
186+
version, commit, date string
181187
)
182188

189+
func init() {
190+
if version != "" {
191+
versionpkg.Set(version, commit, date)
192+
}
193+
}
194+
183195
func main() {
184196
ctx, cancel := context.WithCancel(context.Background())
185197
defer cancel()
@@ -190,7 +202,7 @@ func main() {
190202
}
191203

192204
func versionCheck(ctx context.Context) error {
193-
if version == "dev" {
205+
if version == "" {
194206
return nil
195207
}
196208

lcl/lcl.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/anchordotdev/cli/auth"
1515
"github.com/anchordotdev/cli/lcl/models"
1616
"github.com/anchordotdev/cli/ui"
17+
"github.com/anchordotdev/cli/version"
1718
)
1819

1920
type Command struct {
@@ -118,6 +119,7 @@ func provisionCert(eab *api.Eab, domains []string, acmeURL string) (*tls.Certifi
118119
HostPolicy: autocert.HostWhitelist(domains...),
119120
Client: &acme.Client{
120121
DirectoryURL: acmeURL,
122+
UserAgent: version.UserAgent(),
121123
},
122124
ExternalAccountBinding: &acme.ExternalAccountBinding{
123125
KID: eab.Kid,

version/command.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package version
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/muesli/termenv"
8+
9+
"github.com/anchordotdev/cli"
10+
)
11+
12+
type Command struct{}
13+
14+
func (c Command) UI() cli.UI {
15+
return cli.UI{
16+
RunTTY: c.run,
17+
}
18+
}
19+
20+
func (c Command) run(ctx context.Context, tty termenv.File) error {
21+
_, err := fmt.Fprintln(tty, String())
22+
return err
23+
}

version/command_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package version
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/anchordotdev/cli/api/apitest"
8+
)
9+
10+
func TestCommand(t *testing.T) {
11+
ctx, cancel := context.WithCancel(context.Background())
12+
defer cancel()
13+
14+
cmd := new(Command)
15+
buf, err := apitest.RunTTY(ctx, cmd.UI())
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
20+
if want, got := String()+"\n", buf.String(); want != got {
21+
t.Errorf("want output %q, got %q", want, got)
22+
}
23+
}

version/version.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
var info = struct {
9+
version, commit, date string
10+
11+
os, arch string
12+
}{
13+
version: "dev",
14+
commit: "none",
15+
date: "unknown",
16+
os: runtime.GOOS,
17+
arch: runtime.GOARCH,
18+
}
19+
20+
func Set(version, commit, date string) {
21+
info.version = version
22+
info.commit = commit
23+
info.date = date
24+
}
25+
26+
func String() string {
27+
return fmt.Sprintf("%s (%s/%s) Commit: %s BuildDate: %s", info.version, info.os, info.arch, info.commit, info.date)
28+
}
29+
30+
func UserAgent() string {
31+
return "Anchor CLI " + String()
32+
}

0 commit comments

Comments
 (0)