Skip to content

Commit

Permalink
Check if newer version is available (#439)
Browse files Browse the repository at this point in the history
* Check if newer version is available

* Update README

* Update internal/version/check_update.go

Co-authored-by: Jaime Soriano Pastor <[email protected]>

Co-authored-by: Jaime Soriano Pastor <[email protected]>
  • Loading branch information
mtojek and jsoriano authored Jul 20, 2021
1 parent 57299f2 commit f2bc77f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
19 changes: 15 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)

Expand Down Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion internal/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ package github

import (
"context"
"net/http"

"github.com/pkg/errors"

"github.com/google/go-github/v32/github"
"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 {
Expand All @@ -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(), "")
Expand Down
56 changes: 56 additions & 0 deletions internal/version/check_update.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
18 changes: 11 additions & 7 deletions tools/readme/readme.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit f2bc77f

Please sign in to comment.