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

Track CLI version. #257

Merged
merged 6 commits into from
Apr 16, 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
4 changes: 4 additions & 0 deletions cmd/fastly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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/revision"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/cli/pkg/update"
)
Expand Down Expand Up @@ -52,6 +53,7 @@ func main() {
// Extract a subset of configuration options from the local application directory.
var file config.File
err := file.Read(configFilePath)

if err != nil {
if verboseOutput {
if err == config.ErrLegacyConfig {
Expand All @@ -67,7 +69,9 @@ func main() {
}
text.Break(out)
}
}

if err != nil || file.CLI.Version != revision.SemVer(revision.AppVersion) {
err := file.Load(config.RemoteEndpoint, httpClient)
if err != nil {
errors.RemediationError{
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Run(args []string, env config.Environment, file config.File, configFilePath
configureRoot := configure.NewRootCommand(app, configFilePath, configure.APIClientFactory(cf), &globals)
whoamiRoot := whoami.NewRootCommand(app, httpClient, &globals)
versionRoot := version.NewRootCommand(app)
updateRoot := update.NewRootCommand(app, cliVersioner, httpClient)
updateRoot := update.NewRootCommand(app, configFilePath, cliVersioner, httpClient, &globals)

serviceRoot := service.NewRootCommand(app, &globals)
serviceCreate := service.NewCreateCommand(serviceRoot.CmdClause, &globals)
Expand Down
3 changes: 2 additions & 1 deletion pkg/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
func Stale(lastVersionCheck string, dur string) bool {
d, err := time.ParseDuration("-" + dur)
if err != nil {
// if there is no duration provided, then we should presume the loading of
// If there is no duration provided, then we should presume the loading of
// remote configuration failed and that we should retry that operation.
return true
}

if t, _ := time.Parse(time.RFC3339, lastVersionCheck); !t.Before(time.Now().Add(d)) {
return false
}

return true
}
3 changes: 3 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/revision"
"github.com/fastly/cli/pkg/useragent"
toml "github.com/pelletier/go-toml"
)
Expand Down Expand Up @@ -176,6 +177,7 @@ type CLI struct {
RemoteConfig string `toml:"remote_config"`
TTL string `toml:"ttl"`
LastChecked string `toml:"last_checked"`
Version string `toml:"version"`
}

// User represents user specific configuration.
Expand Down Expand Up @@ -248,6 +250,7 @@ func (f *File) Load(configEndpoint string, httpClient api.HTTPClient) error {
return err
}

f.CLI.Version = revision.SemVer(revision.AppVersion)
f.CLI.LastChecked = time.Now().Format(time.RFC3339)

if f.Legacy.Token != "" {
Expand Down
6 changes: 6 additions & 0 deletions pkg/configure/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestConfigure(t *testing.T) {
last_checked = ""
remote_config = ""
ttl = ""
version = ""

[fastly]
api_endpoint = "http://local.dev"
Expand Down Expand Up @@ -106,6 +107,7 @@ func TestConfigure(t *testing.T) {
last_checked = ""
remote_config = ""
ttl = ""
version = ""

[fastly]
api_endpoint = "http://staging.dev"
Expand Down Expand Up @@ -148,6 +150,7 @@ func TestConfigure(t *testing.T) {
last_checked = ""
remote_config = ""
ttl = ""
version = ""

[fastly]
api_endpoint = "https://api.fastly.com"
Expand Down Expand Up @@ -193,6 +196,7 @@ func TestConfigure(t *testing.T) {
last_checked = ""
remote_config = ""
ttl = ""
version = ""

[fastly]
api_endpoint = "https://api.fastly.com"
Expand Down Expand Up @@ -236,6 +240,7 @@ func TestConfigure(t *testing.T) {
last_checked = ""
remote_config = ""
ttl = ""
version = ""

[fastly]
api_endpoint = "https://api.fastly.com"
Expand Down Expand Up @@ -282,6 +287,7 @@ func TestConfigure(t *testing.T) {
last_checked = ""
remote_config = ""
ttl = ""
version = ""

[fastly]
api_endpoint = "https://api.fastly.com"
Expand Down
14 changes: 14 additions & 0 deletions pkg/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// the Makefile at build time via LDFLAGS.
package revision

import "strings"

var (
// AppVersion is the semver for this version of the client, or
// "v0.0.0-unknown". Set by `make release`.
Expand Down Expand Up @@ -30,3 +32,15 @@ func init() {
GoVersion = "go version unknown"
}
}

// SemVer accepts the application revision version, which is prefixed with a
// `v` and also has a commit hash following the semantic version, and returns
// just the semantic version.
//
// e.g. v1.0.0-xyz --> 1.0.0
func SemVer(av string) string {
av = strings.TrimPrefix(av, "v")
seg := strings.Split(av, "-")

return seg[0]
}
12 changes: 12 additions & 0 deletions pkg/revision/revision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package revision

import "testing"

func TestSemVer(t *testing.T) {
got := SemVer("v1.0.0-xyz")
want := "1.0.0"

if got != want {
t.Fatalf("want %s, got %s", want, got)
}
}
17 changes: 14 additions & 3 deletions pkg/update/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/fastly/cli/pkg/api"
"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/filesystem"
"github.com/fastly/cli/pkg/revision"
"github.com/fastly/cli/pkg/text"
Expand All @@ -18,16 +19,19 @@ import (
// It should be installed under the primary root command.
type RootCommand struct {
common.Base
cliVersioner Versioner
client api.HTTPClient
cliVersioner Versioner
client api.HTTPClient
configFilePath string
}

// NewRootCommand returns a new command registered in the parent.
func NewRootCommand(parent common.Registerer, cliVersioner Versioner, client api.HTTPClient) *RootCommand {
func NewRootCommand(parent common.Registerer, configFilePath string, cliVersioner Versioner, client api.HTTPClient, globals *config.Data) *RootCommand {
var c RootCommand
c.Globals = globals
c.CmdClause = parent.Command("update", "Update the CLI to the latest version")
c.cliVersioner = cliVersioner
c.client = client
c.configFilePath = configFilePath
return &c
}

Expand Down Expand Up @@ -75,6 +79,13 @@ func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
}
}

c.Globals.File.CLI.Version = latest.String()

// Write the file data to disk.
if err := c.Globals.File.Write(c.configFilePath); err != nil {
return fmt.Errorf("error saving config file: %w", err)
}

Integralist marked this conversation as resolved.
Show resolved Hide resolved
progress.Done()

text.Success(out, "Updated %s to %s.", currentPath, latest)
Expand Down