-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from CircleCI-Public/update
Add an `update check` command to check for updates.
- Loading branch information
Showing
6 changed files
with
93 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"unicode/utf8" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newUpdateCommand() *cobra.Command { | ||
update := &cobra.Command{ | ||
Use: "update", | ||
Short: "Update the tool", | ||
} | ||
|
||
update.AddCommand(&cobra.Command{ | ||
Use: "check", | ||
Short: "Check if there are any updates available", | ||
RunE: checkForUpdates, | ||
}) | ||
|
||
return update | ||
} | ||
|
||
func trimFirstRune(s string) string { | ||
_, i := utf8.DecodeRuneInString(s) | ||
return s[i:] | ||
} | ||
|
||
func checkForUpdates(cmd *cobra.Command, args []string) error { | ||
|
||
url := "https://api.github.com/repos/CircleCI-Public/circleci-cli/releases/latest" | ||
|
||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Set("User-Agent", version.UserAgent()) | ||
|
||
client := http.Client{} | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var release struct { | ||
// There are other fields in this response that we could use to download the | ||
// binaries on behalf of the user. | ||
// https://developer.github.com/v3/repos/releases/#get-the-latest-release | ||
HTML string `json:"html_url"` | ||
Tag string `json:"tag_name"` | ||
} | ||
|
||
if err := json.Unmarshal(body, &release); err != nil { | ||
return err | ||
} | ||
|
||
latest := trimFirstRune(release.Tag) | ||
|
||
Logger.Debug("Latest version: %s", latest) | ||
Logger.Debug("Current Version: %s", version.Version) | ||
|
||
if latest == version.Version { | ||
Logger.Info("Already up-to-date.") | ||
} else { | ||
Logger.Infof("A new release is available (%s)", release.Tag) | ||
Logger.Infof("You are running %s", version.Version) | ||
Logger.Infof("You can download it from %s", release.HTML) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,4 @@ func newVersionCommand() *cobra.Command { | |
Logger.Infof("%s (%s)", version.Version, version.Commit) | ||
}, | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,9 +1,18 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// These vars set by `goreleaser`: | ||
var ( | ||
// Version is the current Git tag (the v prefix is stripped) or the name of the snapshot, if you’re using the --snapshot flag | ||
Version = "local-dev-build" | ||
// Commit is the current git commit SHA | ||
Commit = "dirty-local-tree" | ||
) | ||
|
||
// UserAgent returns the user agent that should be user for external requests | ||
func UserAgent() string { | ||
return fmt.Sprintf("circleci-cli/%s-%s", Version, Commit) | ||
} |