Skip to content

Commit 39cbf4e

Browse files
committed
set user agent across all calls
Signed-off-by: Lee Briggs <[email protected]>
1 parent ca3c417 commit 39cbf4e

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

cmd/tscli/list/nameservers/cli.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ import (
1616

1717
func Command() *cobra.Command {
1818
return &cobra.Command{
19-
Use: "nameservers",
20-
Short: "List custom DNS nameservers for the tailnet",
19+
Use: "nameservers",
20+
Aliases: []string{"ns"},
21+
Short: "List custom DNS nameservers for the tailnet",
2122
RunE: func(cmd *cobra.Command, _ []string) error {
2223
client, err := tscli.New()
2324
if err != nil {
2425
return err
2526
}
2627

27-
var raw json.RawMessage // <- receives the body untouched
28+
var raw json.RawMessage
2829
if _, err := tscli.Do(
2930
context.Background(),
3031
client,

pkg/tscli/client.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"os"
1919
"strings"
2020

21+
"github.com/jaxxstorm/tscli/pkg/version"
2122
"github.com/spf13/viper"
2223
tsapi "tailscale.com/client/tailscale/v2"
2324
)
@@ -27,6 +28,11 @@ const (
2728
defaultContentType = "application/json"
2829
)
2930

31+
// getUserAgent returns the properly formatted user agent string
32+
func getUserAgent() string {
33+
return fmt.Sprintf("tscli/%s (Go client)", version.GetVersion())
34+
}
35+
3036
func New() (*tsapi.Client, error) {
3137
tailnet := viper.GetString("tailnet")
3238
apiKey := viper.GetString("api-key")
@@ -37,15 +43,27 @@ func New() (*tsapi.Client, error) {
3743
return nil, fmt.Errorf("api-key is required")
3844
}
3945

40-
httpClient := &http.Client{}
46+
userAgent := getUserAgent()
47+
48+
// Create a custom transport that ensures UserAgent is always set
49+
transport := &userAgentTransport{
50+
rt: http.DefaultTransport,
51+
userAgent: userAgent,
52+
}
53+
54+
// Wrap with debug logging if enabled
4155
if viper.GetBool("debug") {
42-
httpClient.Transport = &logTransport{rt: http.DefaultTransport}
56+
transport.rt = &logTransport{rt: transport.rt}
57+
}
58+
59+
httpClient := &http.Client{
60+
Transport: transport,
4361
}
4462

4563
return &tsapi.Client{
4664
Tailnet: tailnet,
4765
APIKey: apiKey,
48-
UserAgent: "tscli",
66+
UserAgent: userAgent,
4967
HTTP: httpClient,
5068
}, nil
5169
}
@@ -173,3 +191,15 @@ func (t *logTransport) RoundTrip(req *http.Request) (*http.Response, error) {
173191
}
174192
return resp, nil
175193
}
194+
195+
// userAgentTransport wraps an http.RoundTripper to ensure UserAgent is always set
196+
type userAgentTransport struct {
197+
rt http.RoundTripper
198+
userAgent string
199+
}
200+
201+
func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
202+
// Always set our custom user agent
203+
req.Header.Set("User-Agent", t.userAgent)
204+
return t.rt.RoundTrip(req)
205+
}

0 commit comments

Comments
 (0)