From 637b2fb8b9a5bb48bb1e42c750e2151748b55323 Mon Sep 17 00:00:00 2001 From: Zach Peacock <1316813+thoom@users.noreply.github.com> Date: Wed, 9 Feb 2022 16:57:56 -0700 Subject: [PATCH] Fixes Update User-Agent String #41 --- Dockerfile | 2 +- client/utils.go | 9 +++++++-- client/utils_test.go | 18 +++++++++++++++++- output/output.go | 4 ++-- scripts/build.sh | 10 +++++----- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1032d24..083d8cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ COPY . /thoom/gulp WORKDIR /thoom/gulp RUN apk add --update --no-cache git ca-certificates RUN go get -d ./... -RUN CGO_ENABLED=0 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$BUILD_VERSION-buildkit" -o gulp +RUN CGO_ENABLED=0 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$BUILD_VERSION" -o gulp RUN touch /tmp/hosts FROM scratch diff --git a/client/utils.go b/client/utils.go index 1be36d3..824e574 100644 --- a/client/utils.go +++ b/client/utils.go @@ -2,6 +2,7 @@ package client import ( "fmt" + "runtime" "strings" "time" ) @@ -40,7 +41,7 @@ func BuildHeaders(reqHeaders []string, configHeaders map[string]string, includeJ headers := make(map[string]string) // Set the default User-Agent and Accept type - headers["USER-AGENT"] = fmt.Sprintf("thoom.Gulp/%s", GetVersion()) + headers["USER-AGENT"] = CreateUserAgent() headers["ACCEPT"] = "application/json;q=1.0, */*;q=0.8" if includeJSON { @@ -66,9 +67,13 @@ func BuildHeaders(reqHeaders []string, configHeaders map[string]string, includeJ // GetVersion builds the version from the build branch func GetVersion() string { version := buildVersion - if version == "" { + if version == "" || version == "snapshot" { version = defaultVersion } return version } + +func CreateUserAgent() string { + return fmt.Sprintf("thoom.Gulp/%s (%s %s)", GetVersion(), strings.Title(runtime.GOOS), strings.ToUpper(runtime.GOARCH)) +} diff --git a/client/utils_test.go b/client/utils_test.go index 275d6b0..37ce31b 100644 --- a/client/utils_test.go +++ b/client/utils_test.go @@ -2,6 +2,8 @@ package client import ( "fmt" + "runtime" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -14,7 +16,7 @@ func TestBuildHeadersBase(t *testing.T) { assert.Equal(3, len(headers)) assert.Contains(headers, "USER-AGENT") - assert.Equal("thoom.Gulp/"+GetVersion(), headers["USER-AGENT"]) + assert.Equal(CreateUserAgent(), headers["USER-AGENT"]) assert.Contains(headers, "ACCEPT") assert.Equal("application/json;q=1.0, */*;q=0.8", headers["ACCEPT"]) @@ -111,9 +113,23 @@ func TestGetVersion(t *testing.T) { assert.Equal(defaultVersion, GetVersion()) } +func TestGetVersionSnapshot(t *testing.T) { + assert := assert.New(t) + + buildVersion = "snapshot" + assert.Equal(defaultVersion, GetVersion()) +} + func TestGetVersionEnv(t *testing.T) { assert := assert.New(t) buildVersion = "TestVersion" assert.Equal("TestVersion", GetVersion()) } + +func TestCreateUserAgent(t *testing.T) { + assert := assert.New(t) + + expected := fmt.Sprintf("thoom.Gulp/%s (%s %s)", GetVersion(), strings.Title(runtime.GOOS), strings.ToUpper(runtime.GOARCH)) + assert.Equal(expected, CreateUserAgent()) +} diff --git a/output/output.go b/output/output.go index 1db42ac..dd1bbae 100644 --- a/output/output.go +++ b/output/output.go @@ -91,10 +91,10 @@ func (bo *BuffOut) PrintErr(txt string, err error) { // PrintVersion will output the current version and colophon func (bo *BuffOut) PrintVersion(version string) { bo.PrintBlock(fmt.Sprintf(`thoom.Gulp -gulp version: %s +gulp version: %s-%s-%s go version: %s author: Z.d.Peacock -link: https://github.com/thoom/gulp`, version, runtime.Version())) +link: https://github.com/thoom/gulp`, version, strings.Title(runtime.GOOS), strings.ToUpper(runtime.GOARCH), runtime.Version())) fmt.Fprintln(bo.Out, "") } diff --git a/scripts/build.sh b/scripts/build.sh index cc4ab4d..17961a7 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,5 +1,5 @@ -env GOOS=linux GOARCH=386 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION-linux386" -o gulp && tar cfz gulp.linux-386.tar.gz gulp -env GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION-linux64" -o gulp && tar cfz gulp.linux-amd64.tar.gz gulp -env GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION-darwin-amd64" -o gulp && tar cfz gulp.darwin-amd64.tar.gz gulp -env GOOS=darwin GOARCH=arm64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION-darwin-arm64" -o gulp && tar cfz gulp.darwin-arm64.tar.gz gulp -env GOOS=windows GOARCH=amd64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION-windows" -o gulp && zip gulp.windows.zip gulp \ No newline at end of file +env GOOS=linux GOARCH=386 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION" -o gulp && tar cfz gulp.linux-386.tar.gz gulp +env GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION" -o gulp && tar cfz gulp.linux-amd64.tar.gz gulp +env GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION" -o gulp && tar cfz gulp.darwin-amd64.tar.gz gulp +env GOOS=darwin GOARCH=arm64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION" -o gulp && tar cfz gulp.darwin-arm64.tar.gz gulp +env GOOS=windows GOARCH=amd64 go build -ldflags "-X github.com/thoom/gulp/client.buildVersion=$RELEASE_VERSION" -o gulp && zip gulp.windows.zip gulp \ No newline at end of file