Skip to content

Commit

Permalink
Move version logic into own package
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Jun 28, 2018
1 parent 768e047 commit edbbfd1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 47 deletions.
47 changes: 10 additions & 37 deletions 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",
Expand All @@ -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
}
17 changes: 17 additions & 0 deletions 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"
)
21 changes: 11 additions & 10 deletions pkg/github/github.go
Expand Up @@ -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
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -65,34 +65,35 @@ 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
}

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
}

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
}
67 changes: 67 additions & 0 deletions 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
}

0 comments on commit edbbfd1

Please sign in to comment.