From edbbfd177b53f5256958a6a4ee82b93d1d0cd03a Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Thu, 28 Jun 2018 14:43:50 -0400 Subject: [PATCH] Move version logic into own package --- cmd/version.go | 47 +++++++------------------------ messages/messages.go | 17 +++++++++++ pkg/github/github.go | 21 +++++++------- version/version.go | 67 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 47 deletions(-) create mode 100644 messages/messages.go create mode 100644 version/version.go diff --git a/cmd/version.go b/cmd/version.go index 7aad17aab..a946f1ea3 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,16 +1,14 @@ package cmd import ( - "fmt" - - "github.com/astronomerio/astro-cli/pkg/github" + "github.com/astronomerio/astro-cli/version" "github.com/spf13/cobra" ) var ( - version string - gitCommit string - versionCmd = &cobra.Command{ + currVersion string + currCommit string + versionCmd = &cobra.Command{ Use: "version", Short: "Astronomer CLI version", Long: "Astronomer CLI version", @@ -31,42 +29,17 @@ func init() { } func printVersion(cmd *cobra.Command, args []string) error { - fmt.Printf("Astro CLI Version: v%s\n", version) - fmt.Printf("Git Commit: %s\n", gitCommit) + err := version.PrintVersion(currVersion, currCommit) + if err != nil { + return err + } return nil } func upgradeCheck(cmd *cobra.Command, args []string) error { - if len(version) == 0 { - fmt.Println(`Your current Astronomer CLI is not bound to a git commit. - This is likely the result of building from source. You can install the latest tagged release with the following command - - $ curl -sL https://install.astronomer.io | sudo bash`) - return nil - } - - repoLatestTag, err := github.RepoLatestRequest("astronomerio", "astro-cli") - if err != nil { - fmt.Println(err) - } - - currentTag, err := github.RepoTagRequest("astronomerio", "astro-cli", string("v")+version) + err := version.CheckForUpdate(currVersion, currCommit) if err != nil { - fmt.Println(err) + return err } - - currentPub := currentTag.PublishedAt.Format("2006.01.02") - latestPub := repoLatestTag.PublishedAt.Format("2006.01.02") - - if repoLatestTag.TagName > version { - fmt.Printf("Astro CLI Version: v%s (%s)\n", version, currentPub) - fmt.Printf("Astro CLI Latest: %s (%s)\n", repoLatestTag.TagName, latestPub) - fmt.Println(`There is a more recent version of the Astronomer CLI available. -You can install the latest tagged release with the following command - - $ curl -sL https://install.astronomer.io | sudo bash`) - return nil - } - return nil } diff --git a/messages/messages.go b/messages/messages.go new file mode 100644 index 000000000..9da6c3a26 --- /dev/null +++ b/messages/messages.go @@ -0,0 +1,17 @@ +package messages + +var ( + ERROR_INVALID_CLI_VERSION = "Astronomer CLI version is not valid" + ERROR_GITHUB_JSON_MARSHALLING = "Failed to JSON decode Github response from %s" + + INFO_CURR_CLI_VERSION = "Astro CLI Version: v%s" + INFO_CURR_CLI_COMMIT = "Git Commit: %s" + INFO_CURR_CLI_VERSION_DATE = INFO_CURR_CLI_VERSION + " (%s)" + INFO_LATEST_CLI_VERSION = "Astro CLI Latest: %s " + INFO_LATEST_CLI_VERSION_DATE = INFO_LATEST_CLI_VERSION + " (%s)" + INFO_CLI_INSTALL_CMD = "\t$ curl -sL https://install.astronomer.io | sudo bash" + INFO_UPGRADE_CLI = "There is a more recent version of the Astronomer CLI available.\nYou can install the latest tagged release with the following command" + INFO_UNTAGGED_VERSION = "Your current Astronomer CLI is not tagged.\nThis is likely the result of building from source. You can install the latest tagged release with the following command" + + NA = "N/A" +) diff --git a/pkg/github/github.go b/pkg/github/github.go index 160ca1766..4c325d447 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -7,13 +7,13 @@ import ( "strings" "time" + "github.com/astronomerio/astro-cli/messages" "github.com/astronomerio/astro-cli/pkg/httputil" "github.com/pkg/errors" ) var ( - http = httputil.NewHTTPClient() - api = NewGithubClient(http) + httpclient = httputil.NewHTTPClient() ) // RepoLatestResponse represents a tag info response from Github API @@ -30,9 +30,9 @@ type Client struct { } // NewGithubClient returns a HTTP client for interfacing with github -func NewGithubClient(c *httputil.HTTPClient) *Client { +func NewGithubClient() *Client { return &Client{ - HTTPClient: c, + HTTPClient: httpclient, } } @@ -65,10 +65,10 @@ func (c *Client) GithubRequest(url string, method string) (*httputil.HTTPRespons } // RepoLatestRequest Makes a request to grab the latest release of a github repository -func RepoLatestRequest(orgName string, repoName string) (*RepoLatestResponse, error) { +func (c *Client) RepoLatestRequest(orgName string, repoName string) (*RepoLatestResponse, error) { url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", orgName, repoName) - response, err := api.GithubRequest(url, "GET") + response, err := c.GithubRequest(url, "GET") if err != nil { return nil, err } @@ -76,15 +76,16 @@ func RepoLatestRequest(orgName string, repoName string) (*RepoLatestResponse, er decode := RepoLatestResponse{} err = json.NewDecoder(strings.NewReader(response.Body)).Decode(&decode) if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("Failed to JSON decode Github response from %s", url)) + return nil, errors.Wrap(err, fmt.Sprintf(messages.ERROR_GITHUB_JSON_MARSHALLING, url)) } return &decode, nil } // RepoTagRequest makes a request to grab a specific tag of a github repository -func RepoTagRequest(orgName string, repoName string, tagName string) (*RepoLatestResponse, error) { +func (c *Client) RepoTagRequest(orgName string, repoName string, tagName string) (*RepoLatestResponse, error) { url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/tags/%s", orgName, repoName, tagName) - response, err := api.GithubRequest(url, "GET") + + response, err := c.GithubRequest(url, "GET") if err != nil { return nil, err } @@ -92,7 +93,7 @@ func RepoTagRequest(orgName string, repoName string, tagName string) (*RepoLates decode := RepoLatestResponse{} err = json.NewDecoder(strings.NewReader(response.Body)).Decode(&decode) if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("Failed to JSON decode Github response from %s", url)) + return nil, errors.Wrap(err, fmt.Sprintf(messages.ERROR_GITHUB_JSON_MARSHALLING, url)) } return &decode, nil } diff --git a/version/version.go b/version/version.go new file mode 100644 index 000000000..1e719c80f --- /dev/null +++ b/version/version.go @@ -0,0 +1,67 @@ +package version + +import ( + "errors" + "fmt" + + "github.com/astronomerio/astro-cli/messages" + "github.com/astronomerio/astro-cli/pkg/github" +) + +var ( + api = github.NewGithubClient() +) + +// PrintVersion outputs current cli version and git commit if exists +func PrintVersion(version, gitCommit string) error { + if !isValidVersion(version) { + return errors.New(messages.ERROR_INVALID_CLI_VERSION) + } + + fmt.Printf(messages.INFO_CURR_CLI_VERSION+"\n", version) + fmt.Printf(messages.INFO_CURR_CLI_COMMIT+"\n", gitCommit) + return nil +} + +// CheckForUpdate checks current version against latest on github +func CheckForUpdate(version, gitCommit string) error { + if !isValidVersion(version) { + fmt.Println(messages.INFO_UNTAGGED_VERSION) + fmt.Println(messages.INFO_CLI_INSTALL_CMD) + return nil + } + + // fetch latest cli version + latestTagResp, err := api.RepoLatestRequest("astronomerio", "astro-cli") + if err != nil { + fmt.Println(err) + latestTagResp.TagName = messages.NA + } + + // fetch meta data around current cli version + currentTagResp, err := api.RepoTagRequest("astronomerio", "astro-cli", string("v")+version) + if err != nil { + fmt.Println(err) + } + + currentPub := currentTagResp.PublishedAt.Format("2006.01.02") + latestPub := latestTagResp.PublishedAt.Format("2006.01.02") + latestTag := latestTagResp.TagName + + if latestTagResp.TagName > version { + fmt.Printf(messages.INFO_CURR_CLI_VERSION_DATE+"\n", version, currentPub) + fmt.Printf(messages.INFO_LATEST_CLI_VERSION_DATE+"\n", latestTag, latestPub) + fmt.Println(messages.INFO_UPGRADE_CLI) + fmt.Println(messages.INFO_CLI_INSTALL_CMD) + return nil + } + + return nil +} + +func isValidVersion(version string) bool { + if len(version) == 0 { + return false + } + return true +}