Skip to content

Commit 65ecf31

Browse files
committed
user agent
1 parent 0434074 commit 65ecf31

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

internal/registry/registry.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"path/filepath"
13+
"rules-cli/internal/utils"
1314
"strings"
1415
)
1516

@@ -68,6 +69,8 @@ func (c *Client) GetRule(name, version string) (*RuleInfo, error) {
6869
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
6970
}
7071

72+
utils.SetUserAgent(req)
73+
7174
client := &http.Client{}
7275
resp, err := client.Do(req)
7376
if err != nil {
@@ -110,6 +113,8 @@ func (c *Client) DownloadRule(name, version, formatDir string) error {
110113
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
111114
}
112115

116+
utils.SetUserAgent(req)
117+
113118
client := &http.Client{}
114119
resp, err := client.Do(req)
115120
if err != nil {
@@ -167,6 +172,8 @@ func (c *Client) PublishRule(ownerSlug, ruleSlug, content string, visibility str
167172
req.Header.Set("Content-Type", "application/json")
168173
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
169174

175+
utils.SetUserAgent(req)
176+
170177
client := &http.Client{}
171178
resp, err := client.Do(req)
172179
if err != nil {
@@ -193,6 +200,7 @@ func (c *Client) downloadFromGitHub(repoPath string, formatDir string) error {
193200
return fmt.Errorf("failed to create request: %w", err)
194201
}
195202
req.Header.Set("Accept", "application/vnd.github.v3+json")
203+
utils.SetUserAgent(req)
196204

197205
// Send request
198206
client := &http.Client{}
@@ -303,4 +311,4 @@ func (c *Client) downloadFromGitHub(repoPath string, formatDir string) error {
303311
}
304312

305313
return nil
306-
}
314+
}

internal/utils/user_agent.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"runtime"
7+
)
8+
9+
// GetUserAgent returns the user agent string for HTTP requests
10+
// This includes the CLI name, version, and runtime information
11+
func GetUserAgent() string {
12+
// For now, use a hardcoded version. In the future, this could be injected
13+
// during the build process via ldflags
14+
version := "1.0.0"
15+
16+
// Include runtime information for better analytics
17+
return fmt.Sprintf("rules-cli/%s (%s; %s; %s)",
18+
version,
19+
runtime.GOOS,
20+
runtime.GOARCH,
21+
runtime.Version())
22+
}
23+
24+
// SetUserAgent sets the User-Agent header on an HTTP request
25+
func SetUserAgent(req *http.Request) {
26+
req.Header.Set("User-Agent", GetUserAgent())
27+
}

0 commit comments

Comments
 (0)