-
Notifications
You must be signed in to change notification settings - Fork 110
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
[RSDK-4975] CLI update warning #3585
Merged
mattjperez
merged 17 commits into
viamrobotics:main
from
mattjperez:rsdk-4975-cli-upgrade-warning
Feb 21, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
11bee49
add rconfig.dateCompiled
mattjperez b1be8aa
check update command and hook
mattjperez 8c6657b
pass build-go,linter-go, add PR suggestions
mattjperez 4bd69bc
store check date to cache
mattjperez bfd68a1
fix date formatting error
mattjperez dbc4038
fix lint, remove comments
mattjperez 5f6f2ea
revert dependency doc
mattjperez 0f9db00
set version info to (dev) when no tag
mattjperez 00c75ca
refactor
mattjperez 8cab699
check every three days
mattjperez eff887e
restrict api call to every three days
mattjperez 89ccedf
fmt
mattjperez 841a745
remove repetitive logic
mattjperez ccf5bcd
add early return, quiet todo
mattjperez 205cd07
rebase, rerun build,lint
mattjperez 6d83f4a
rename func
mattjperez b408dea
rename struct
mattjperez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,19 @@ package cli | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"runtime/debug" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/fullstorydev/grpcurl" | ||
"github.com/google/uuid" | ||
"github.com/jhump/protoreflect/grpcreflect" | ||
|
@@ -40,6 +43,10 @@ import ( | |
"go.viam.com/rdk/services/shell" | ||
) | ||
|
||
const ( | ||
rdkReleaseURL = "https://api.github.com/repos/viamrobotics/rdk/releases/latest" | ||
) | ||
|
||
// viamClient wraps a cli.Context and provides all the CLI command functionality | ||
// needed to talk to the app and data services but not directly to robot parts. | ||
type viamClient struct { | ||
|
@@ -380,6 +387,129 @@ func RobotsPartShellAction(c *cli.Context) error { | |
) | ||
} | ||
|
||
// checkUpdateResponse holds the values used to hold release information. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename this too |
||
type getLatestReleaseResponse struct { | ||
Name string `json:"name"` | ||
TagName string `json:"tag_name"` | ||
TarballURL string `json:"tarball_url"` | ||
} | ||
|
||
func getLatestReleaseVersion() (string, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
resp := getLatestReleaseResponse{} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rdkReleaseURL, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
client := http.DefaultClient | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = json.NewDecoder(res.Body).Decode(&resp) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
defer utils.UncheckedError(res.Body.Close()) | ||
return resp.TagName, err | ||
} | ||
|
||
// CheckUpdateAction is the corresponding Action for 'check-update'. | ||
func CheckUpdateAction(c *cli.Context) error { | ||
if c.Bool(quietFlag) { | ||
return nil | ||
} | ||
|
||
dateCompiledRaw := rconfig.DateCompiled | ||
mattjperez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// `go build` will not set the compilation flags needed for this check | ||
if dateCompiledRaw == "" { | ||
return nil | ||
} | ||
|
||
dateCompiled, err := time.Parse("2006-01-02", dateCompiledRaw) | ||
if err != nil { | ||
warningf(c.App.ErrWriter, "CLI Update Check: failed to parse compilation date: %w", err) | ||
mattjperez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
|
||
// install is less than six weeks old | ||
if time.Since(dateCompiled) < time.Hour*24*7*6 { | ||
return nil | ||
} | ||
|
||
conf, err := configFromCache() | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
utils.UncheckedError(err) | ||
return nil | ||
} | ||
conf = &config{} | ||
} | ||
|
||
var lastCheck time.Time | ||
if conf.LastUpdateCheck == "" { | ||
conf.LastUpdateCheck = time.Now().Format("2006-01-02") | ||
} else { | ||
lastCheck, err = time.Parse("2006-01-02", conf.LastUpdateCheck) | ||
if err != nil { | ||
warningf(c.App.ErrWriter, "CLI Update Check: failed to parse date of last check: %w", err) | ||
return nil | ||
} | ||
} | ||
|
||
// The latest version info is cached to limit api calls to once every three days | ||
if time.Since(lastCheck) < time.Hour*24*3 && conf.LatestVersion != "" { | ||
warningf(c.App.ErrWriter, "CLI Update Check: Your CLI is more than 6 weeks old. "+ | ||
"Consider updating to version: %s", conf.LatestVersion) | ||
return nil | ||
} | ||
|
||
latestRelease, err := getLatestReleaseVersion() | ||
if err != nil { | ||
warningf(c.App.ErrWriter, "CLI Update Check: failed to get latest release information: %w", err) | ||
return nil | ||
} | ||
|
||
latestVersion, err := semver.NewVersion(latestRelease) | ||
if err != nil { | ||
warningf(c.App.ErrWriter, "CLI Update Check: failed to parse latest version: %w", err) | ||
return nil | ||
} | ||
|
||
conf.LatestVersion = latestVersion.String() | ||
|
||
err = storeConfigToCache(conf) | ||
if err != nil { | ||
utils.UncheckedError(err) | ||
} | ||
|
||
appVersion := rconfig.Version | ||
if appVersion == "(dev)" { | ||
warningf(c.App.ErrWriter, "CLI Update Check: Your CLI is more than 6 weeks old. "+ | ||
"Consider updating to version: %s", latestVersion.Original()) | ||
return nil | ||
} | ||
|
||
localVersion, err := semver.NewVersion(appVersion) | ||
if err != nil { | ||
warningf(c.App.ErrWriter, "CLI Update Check: failed to parse compiled version: %w", err) | ||
return nil | ||
} | ||
|
||
if localVersion.LessThan(latestVersion) { | ||
warningf(c.App.ErrWriter, "CLI Update Check: Your CLI is out of date. Consider updating to version %s", latestVersion.Original()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// VersionAction is the corresponding Action for 'version'. | ||
func VersionAction(c *cli.Context) error { | ||
info, ok := debug.ReadBuildInfo() | ||
|
@@ -408,10 +538,8 @@ func VersionAction(c *cli.Context) error { | |
if dep, ok := deps["go.viam.com/api"]; ok { | ||
apiVersion = dep.Version | ||
} | ||
|
||
appVersion := rconfig.Version | ||
if appVersion == "" { | ||
appVersion = "(dev)" | ||
} | ||
printf(c.App.Writer, "Version %s Git=%s API=%s", appVersion, version, apiVersion) | ||
return nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[opt] A friend of mine mentioned that using the go debug
vcs.time
would work as well.See: https://pkg.go.dev/runtime/debug#BuildSetting
We report this in
viam --debug version
Note: if you compile the cli with
go build ./viam/main.go
the vcs debug info won't be there. golang/go#51279I don't want to drag down this PR in refactors so I don't see a strong reason to do this now, but might be worth considering in the future.