Skip to content

Commit

Permalink
Add UserAgent headers
Browse files Browse the repository at this point in the history
  • Loading branch information
lloesche committed Oct 7, 2024
1 parent 851dbd3 commit 937025b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ jobs:
echo "OS_NAME=$OS_NAME" >> $GITHUB_ENV
- name: Build Binary (amd64)
run: |
GOARCH=amd64 go build -ldflags "-s -w -X main.version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-amd64-${{ github.ref_name }}$SUFFIX"
GOARCH=amd64 go build -ldflags "-s -w -X github.com/someengineering/fixctl/config.Version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-amd64-${{ github.ref_name }}$SUFFIX"
shell: bash
- name: Build Binary (arm64)
run: |
GOARCH=arm64 go build -ldflags "-s -w -X main.version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-arm64-${{ github.ref_name }}$SUFFIX"
GOARCH=arm64 go build -ldflags "-s -w -X github.com/someengineering/fixctl/config.Version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-arm64-${{ github.ref_name }}$SUFFIX"
shell: bash
- name: Generate Universal Binary (macOS)
if: matrix.os == 'macos-latest'
Expand Down
4 changes: 4 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"net/url"
"strings"

"github.com/someengineering/fixctl/config"
)

func LoginAndGetJWT(apiEndpoint, username, password string) (string, error) {
Expand All @@ -21,6 +23,7 @@ func LoginAndGetJWT(apiEndpoint, username, password string) (string, error) {
return "", fmt.Errorf("creating login request failed: %w", err)
}

req.Header.Set("User-Agent", config.GetUserAgent())
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")

Expand Down Expand Up @@ -57,6 +60,7 @@ func GetJWTFromToken(apiEndpoint, fixToken string) (string, error) {
if err != nil {
return "", err
}
req.Header.Set("User-Agent", config.GetUserAgent())
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
Expand Down
6 changes: 2 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/someengineering/fixctl/auth"
"github.com/someengineering/fixctl/config"
"github.com/someengineering/fixctl/format"
"github.com/someengineering/fixctl/search"
"github.com/someengineering/fixctl/utils"
Expand Down Expand Up @@ -36,6 +37,7 @@ var (

func init() {
cobra.OnInitialize(initConfig)
rootCmd.Version = config.Version

rootCmd.PersistentFlags().StringVar(&apiEndpoint, "endpoint", "https://app.fix.security", "API endpoint URL (env FIX_ENDPOINT)")
rootCmd.PersistentFlags().StringVar(&fixToken, "token", "", "Auth token (env FIX_TOKEN)")
Expand Down Expand Up @@ -158,7 +160,3 @@ func executeSearch(cmd *cobra.Command, args []string) {
func Execute() error {
return rootCmd.Execute()
}

func SetVersion(version string) {
rootCmd.Version = version
}
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package config

var Version string = "dev"

func GetUserAgent() string {
return "fixctl-" + Version
}
37 changes: 37 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"testing"
)

func TestVersion(t *testing.T) {
originalVersion := Version

newVersion := "v1.2.3"
Version = newVersion
if Version != newVersion {
t.Errorf("Expected version %s, but got %s", newVersion, Version)
}

Version = originalVersion
if Version != originalVersion {
t.Errorf("Expected version %s, but got %s", originalVersion, Version)
}
}

func TestGetUserAgent(t *testing.T) {
originalVersion := Version

expectedUserAgent := "fixctl-dev"
if userAgent := GetUserAgent(); userAgent != expectedUserAgent {
t.Errorf("Expected User-Agent %s, but got %s", expectedUserAgent, userAgent)
}

Version = "v1.2.3"
expectedUserAgent = "fixctl-v1.2.3"
if userAgent := GetUserAgent(); userAgent != expectedUserAgent {
t.Errorf("Expected User-Agent %s, but got %s", expectedUserAgent, userAgent)
}

Version = originalVersion
}
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import (
"github.com/someengineering/fixctl/cmd"
)

var version = "dev"

func main() {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})

cmd.SetVersion(version)
if err := cmd.Execute(); err != nil {
logrus.Errorln("Error:", err)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"

"github.com/sirupsen/logrus"
"github.com/someengineering/fixctl/config"
"github.com/someengineering/fixctl/utils"
)

Expand Down Expand Up @@ -41,6 +42,7 @@ func SearchGraph(apiEndpoint, fixJWT, workspaceID, searchStr string, withEdges b
return
}

req.Header.Set("User-Agent", config.GetUserAgent())
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/ndjson")
req.AddCookie(&http.Cookie{
Expand Down

0 comments on commit 937025b

Please sign in to comment.