From f2bc77ffd881e300f9ea3a544b4419be2873c6d0 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 20 Jul 2021 09:57:37 +0200 Subject: [PATCH] Check if newer version is available (#439) * Check if newer version is available * Update README * Update internal/version/check_update.go Co-authored-by: Jaime Soriano Pastor Co-authored-by: Jaime Soriano Pastor --- README.md | 18 ++++++---- cmd/root.go | 19 ++++++++--- internal/github/client.go | 8 ++++- internal/version/check_update.go | 56 ++++++++++++++++++++++++++++++++ tools/readme/readme.md.tmpl | 18 ++++++---- 5 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 internal/version/check_update.go diff --git a/README.md b/README.md index 207a8d7d3..181b29e30 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,9 @@ Currently, `elastic-package` only supports packages of type [Elastic Integration ## Getting started -Download and build the latest master of `elastic-package` binary: - -```bash -git clone https://github.com/elastic/elastic-package.git -make build -``` +Download latest release from the [Releases](https://github.com/elastic/elastic-package/releases/latest) page. -Alternatively, you may use `go get` but you will not be able to use the `elastic-package version` command. +Alternatively, you may use `go get` but you will not be able to use the `elastic-package version` command or check updates. ```bash go get github.com/elastic/elastic-package @@ -39,6 +34,15 @@ Run the `help` command and see available commands: elastic-package help ``` +## Development + +Download and build the latest master of `elastic-package` binary: + +```bash +git clone https://github.com/elastic/elastic-package.git +make build +``` + ## Commands `elastic-package` currently offers the commands listed below. diff --git a/cmd/root.go b/cmd/root.go index 83899e853..d831c7d79 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,6 +11,7 @@ import ( "github.com/elastic/elastic-package/internal/cobraext" "github.com/elastic/elastic-package/internal/logger" + "github.com/elastic/elastic-package/internal/version" ) var commands = []*cobraext.Command{ @@ -36,10 +37,15 @@ var commands = []*cobraext.Command{ // RootCmd creates and returns root cmd for elastic-package func RootCmd() *cobra.Command { rootCmd := &cobra.Command{ - Use: "elastic-package", - Short: "elastic-package - Command line tool for developing Elastic Integrations", - SilenceUsage: true, - PersistentPreRunE: processPersistentFlags, + Use: "elastic-package", + Short: "elastic-package - Command line tool for developing Elastic Integrations", + SilenceUsage: true, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + return cobraext.ComposeCommandActions(cmd, args, + processPersistentFlags, + checkVersionUpdate, + ) + }, } rootCmd.PersistentFlags().BoolP(cobraext.VerboseFlagName, "v", false, cobraext.VerboseFlagDescription) @@ -69,3 +75,8 @@ func processPersistentFlags(cmd *cobra.Command, args []string) error { } return nil } + +func checkVersionUpdate(cmd *cobra.Command, args []string) error { + version.CheckUpdate() + return nil +} diff --git a/internal/github/client.go b/internal/github/client.go index d47a7e547..6acb79595 100644 --- a/internal/github/client.go +++ b/internal/github/client.go @@ -6,6 +6,7 @@ package github import ( "context" + "net/http" "github.com/pkg/errors" @@ -13,7 +14,7 @@ import ( "golang.org/x/oauth2" ) -// Client method creates new instance of the GitHub API client. +// Client function creates new instance of the GitHub API client. func Client() (*github.Client, error) { authToken, err := AuthToken() if err != nil { @@ -24,6 +25,11 @@ func Client() (*github.Client, error) { ))), nil } +// UnauthorizedClient function returns unauthorized instance of Github API client. +func UnauthorizedClient() *github.Client { + return github.NewClient(new(http.Client)) +} + // User method returns the GitHub authenticated user. func User(client *github.Client) (string, error) { user, _, err := client.Users.Get(context.Background(), "") diff --git a/internal/version/check_update.go b/internal/version/check_update.go new file mode 100644 index 000000000..0fe25b520 --- /dev/null +++ b/internal/version/check_update.go @@ -0,0 +1,56 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package version + +import ( + "context" + + "github.com/Masterminds/semver" + + "github.com/elastic/elastic-package/internal/github" + "github.com/elastic/elastic-package/internal/logger" +) + +const ( + repositoryOwner = "elastic" + repositoryName = "elastic-package" +) + +// CheckUpdate function checks using Github Release API if newer version is available. +func CheckUpdate() { + if Tag == "" { + logger.Debugf("Distribution built without a version tag, can't determine release chronology. Please consider using official releases at " + + "https://github.com/elastic/elastic-package/releases") + return + } + + githubClient := github.UnauthorizedClient() + release, _, err := githubClient.Repositories.GetLatestRelease(context.TODO(), repositoryOwner, repositoryName) + if err != nil { + logger.Debugf("Error: cant't check latest release, %v", err) + return + } + + if release.TagName == nil || *release.TagName == "" { + logger.Debugf("Error: release tag is empty") + return + } + + currentVersion, err := semver.NewVersion(Tag[1:]) // strip "v" prefix + if err != nil { + logger.Debugf("Error: cant't parse current version tage, %v", err) + return + } + + releaseVersion, err := semver.NewVersion((*release.TagName)[1:]) // strip "v" prefix + if err != nil { + logger.Debugf("Error: cant't parse current version tage, %v", err) + return + } + + if currentVersion.LessThan(releaseVersion) { + logger.Infof("New version is available - %s. Download from: %s", *release.TagName, *release.HTMLURL) + } +} diff --git a/tools/readme/readme.md.tmpl b/tools/readme/readme.md.tmpl index 0beb436d0..bd7d2bbe8 100644 --- a/tools/readme/readme.md.tmpl +++ b/tools/readme/readme.md.tmpl @@ -11,14 +11,9 @@ Currently, `elastic-package` only supports packages of type [Elastic Integration ## Getting started -Download and build the latest master of `elastic-package` binary: - -```bash -git clone https://github.com/elastic/elastic-package.git -make build -``` +Download latest release from the [Releases](https://github.com/elastic/elastic-package/releases/latest) page. -Alternatively, you may use `go get` but you will not be able to use the `elastic-package version` command. +Alternatively, you may use `go get` but you will not be able to use the `elastic-package version` command or check updates. ```bash go get github.com/elastic/elastic-package @@ -39,6 +34,15 @@ Run the `help` command and see available commands: elastic-package help ``` +## Development + +Download and build the latest master of `elastic-package` binary: + +```bash +git clone https://github.com/elastic/elastic-package.git +make build +``` + ## Commands `elastic-package` currently offers the commands listed below.