Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor UserAgent. #219

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ builds:
- <<: &build_defaults
main: ./cmd/fastly
ldflags:
- -s -w -X "github.com/fastly/cli/pkg/version.AppVersion={{ .Version }}"
- -X "github.com/fastly/cli/pkg/version.GitRevision={{ .ShortCommit }}"
- -X "github.com/fastly/cli/pkg/version.GoVersion={{ .Env.GOVERSION }}"
- -s -w -X "github.com/fastly/cli/pkg/revision.AppVersion={{ .Version }}"
- -X "github.com/fastly/cli/pkg/revision.GitCommit={{ .ShortCommit }}"
- -X "github.com/fastly/cli/pkg/revision.GoVersion={{ .Env.GOVERSION }}"
id: macos
goos: [darwin]
goarch: [amd64]
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ VERSION ?= $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD
TESTARGS ?= ./{cmd,pkg}/...

LDFLAGS = -ldflags "\
-X 'github.com/fastly/cli/pkg/version.AppVersion=${VERSION}' \
-X 'github.com/fastly/cli/pkg/version.GitRevision=$(shell git rev-parse --short HEAD || echo unknown)' \
-X 'github.com/fastly/cli/pkg/version.GoVersion=$(shell go version)' \
-X 'github.com/fastly/cli/pkg/revision.AppVersion=${VERSION}' \
-X 'github.com/fastly/cli/pkg/revision.GitCommit=$(shell git rev-parse --short HEAD || echo unknown)' \
-X 'github.com/fastly/cli/pkg/revision.GoVersion=$(shell go version)' \
"

fastly:
Expand Down
5 changes: 3 additions & 2 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/fastly/cli/pkg/logging/sumologic"
"github.com/fastly/cli/pkg/logging/syslog"
"github.com/fastly/cli/pkg/logs"
"github.com/fastly/cli/pkg/revision"
"github.com/fastly/cli/pkg/service"
"github.com/fastly/cli/pkg/serviceversion"
"github.com/fastly/cli/pkg/stats"
Expand Down Expand Up @@ -724,10 +725,10 @@ func Run(args []string, env config.Environment, file config.File, configFilePath
return errors.RemediationError{Prefix: usage, Inner: fmt.Errorf("command not found")}
}

if versioner != nil && name != "update" && !version.IsPreRelease(version.AppVersion) {
if versioner != nil && name != "update" && !version.IsPreRelease(revision.AppVersion) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel() // push cancel on the defer stack first...
f := update.CheckAsync(ctx, file, configFilePath, version.AppVersion, versioner)
f := update.CheckAsync(ctx, file, configFilePath, revision.AppVersion, versioner)
defer f(out) // ...and the printing function second, so we hit the timeout
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/fastly/cli/pkg/api"
"github.com/fastly/cli/pkg/filesystem"
"github.com/fastly/cli/pkg/useragent"
toml "github.com/pelletier/go-toml"
)

Expand Down Expand Up @@ -227,6 +228,7 @@ func (f *File) Load(configEndpoint string, httpClient api.HTTPClient) error {
return err
}

req.Header.Set("User-Agent", useragent.Name)
resp, err := httpClient.Do(req)
if err != nil {
return err
Expand Down
32 changes: 32 additions & 0 deletions pkg/revision/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Package revision defines variables that will be populated with values from
// the Makefile at build time via LDFLAGS.
package revision

var (
// AppVersion is the semver for this version of the client, or
// "v0.0.0-unknown". Set by `make release`.
AppVersion string

// GitCommit is the short git SHA associated with this build, or
// "unknown". Set by `make release`.
GitCommit string

// GoVersion is the output of `go version` associated with this build, or
// "go version unknown". Set by `make release`.
GoVersion string
)

// None is the AppVersion string for local (unversioned) builds.
const None = "v0.0.0-unknown"

func init() {
if AppVersion == "" {
AppVersion = None
}
if GitCommit == "" {
GitCommit = "unknown"
}
if GoVersion == "" {
GoVersion = "go version unknown"
}
}
4 changes: 2 additions & 2 deletions pkg/update/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/fastly/cli/pkg/api"
"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/filesystem"
"github.com/fastly/cli/pkg/revision"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/cli/pkg/version"
)

// RootCommand is the parent command for all subcommands in this package.
Expand All @@ -35,7 +35,7 @@ func NewRootCommand(parent common.Registerer, v Versioner, client api.HTTPClient
func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
progress := text.NewQuietProgress(out)

current, latest, shouldUpdate, err := Check(context.Background(), version.AppVersion, c.versioner)
current, latest, shouldUpdate, err := Check(context.Background(), revision.AppVersion, c.versioner)
if err != nil {
return fmt.Errorf("error checking for latest version: %w", err)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/useragent/useragent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package useragent

import (
"fmt"

"github.com/fastly/cli/pkg/revision"
)

// Name is the user agent which we report in all HTTP requests.
var Name = fmt.Sprintf("%s/%s", "FastlyCLI", revision.AppVersion)
41 changes: 5 additions & 36 deletions pkg/version/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,17 @@ import (
"strings"

"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/revision"
"github.com/fastly/cli/pkg/useragent"
"github.com/fastly/go-fastly/v3/fastly"
)

var (
// AppVersion is the semver for this version of the client, or
// "v0.0.0-unknown". Set by `make release`.
AppVersion string

// GitRevision is the short git SHA associated with this build, or
// "unknown". Set by `make release`.
GitRevision string

// GoVersion is the output of `go version` associated with this build, or
// "go version unknown". Set by `make release`.
GoVersion string

// UserAgent is the user agent which we report in all HTTP requests to the
// API via go-fastly.
UserAgent string
)

// None is the AppVersion string for local (unversioned) builds.
const None = "v0.0.0-unknown"

func init() {
if AppVersion == "" {
AppVersion = None
}
if GitRevision == "" {
GitRevision = "unknown"
}
if GoVersion == "" {
GoVersion = "go version unknown"
}

UserAgent = fmt.Sprintf("FastlyCLI/%s", AppVersion)

// Override the go-fastly UserAgent value by prepending the CLI version.
//
// Results in a header similar too:
// User-Agent: FastlyCLI/0.1.0, FastlyGo/1.5.0 (1.13.0)
fastly.UserAgent = fmt.Sprintf("%s, %s", UserAgent, fastly.UserAgent)
fastly.UserAgent = fmt.Sprintf("%s, %s", useragent.Name, fastly.UserAgent)
}

// RootCommand is the parent command for all subcommands in this package.
Expand All @@ -65,8 +34,8 @@ func NewRootCommand(parent common.Registerer) *RootCommand {

// Exec implements the command interface.
func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
fmt.Fprintf(out, "Fastly CLI version %s (%s)\n", AppVersion, GitRevision)
fmt.Fprintf(out, "Built with %s\n", GoVersion)
fmt.Fprintf(out, "Fastly CLI version %s (%s)\n", revision.AppVersion, revision.GitCommit)
fmt.Fprintf(out, "Built with %s\n", revision.GoVersion)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/whoami/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/version"
"github.com/fastly/cli/pkg/useragent"
)

// RootCommand is the parent command for all subcommands in this package.
Expand Down Expand Up @@ -47,7 +47,7 @@ func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {

req.Header.Set("Fastly-Key", token)
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", version.UserAgent)
req.Header.Set("User-Agent", useragent.Name)
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("error executing API request: %w", err)
Expand Down