Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get the latest release version from GitHub Release
- Loading branch information
1 parent
f30ba8a
commit 2a04fa2
Showing
6 changed files
with
164 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/minamijoyo/tfupdate/release" | ||
flag "github.com/spf13/pflag" | ||
) | ||
|
||
// ReleaseCommand is a command which gets the latest release version. | ||
type ReleaseCommand struct { | ||
Meta | ||
url string | ||
} | ||
|
||
// Run runs the procedure of this command. | ||
func (c *ReleaseCommand) Run(args []string) int { | ||
cmdFlags := flag.NewFlagSet("release", flag.ContinueOnError) | ||
|
||
if err := cmdFlags.Parse(args); err != nil { | ||
c.UI.Error(fmt.Sprintf("failed to parse arguments: %s", err)) | ||
return 1 | ||
} | ||
|
||
if len(cmdFlags.Args()) != 1 { | ||
c.UI.Error(fmt.Sprintf("The command expects 1 argument, but got %#v", cmdFlags.Args())) | ||
c.UI.Error(c.Help()) | ||
return 1 | ||
} | ||
|
||
c.url = cmdFlags.Arg(0) | ||
|
||
r, err := release.NewRelease("github", c.url) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
v, err := release.ResolveVersionAlias(r, "latest") | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
c.UI.Output(v) | ||
return 0 | ||
} | ||
|
||
// Help returns long-form help text. | ||
func (c *ReleaseCommand) Help() string { | ||
helpText := ` | ||
Usage: tfupdate release [options] <URL> | ||
Arguments | ||
URL A URL of the release repository | ||
(e.g. https://github.com/terraform-providers/terraform-provider-aws) | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
// Synopsis returns one-line help text. | ||
func (c *ReleaseCommand) Synopsis() string { | ||
return "Get the latest release version" | ||
} |
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,52 @@ | ||
package release | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/google/go-github/v28/github" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// GitHubRelease is a release implementation which provides version information with GitHub Release. | ||
type GitHubRelease struct { | ||
client *github.Client | ||
owner string | ||
repo string | ||
} | ||
|
||
// NewGitHubRelease is a factory method which returns an GitHubRelease instance. | ||
func NewGitHubRelease(url string) (Release, error) { | ||
re := regexp.MustCompile(`https://github.com/(.+)/(.+)`) | ||
matched := re.FindStringSubmatch(url) | ||
if len(matched) != 3 { | ||
return nil, errors.Errorf("failed to parse url: %s, matched: %#v", url, matched) | ||
} | ||
owner := matched[1] | ||
repo := matched[2] | ||
|
||
return &GitHubRelease{ | ||
client: github.NewClient(nil), | ||
owner: owner, | ||
repo: repo, | ||
}, nil | ||
} | ||
|
||
// Latest returns a latest version. | ||
func (r *GitHubRelease) Latest() (string, error) { | ||
release, _, err := r.client.Repositories.GetLatestRelease(context.Background(), r.owner, r.repo) | ||
|
||
if err != nil { | ||
return "", fmt.Errorf("failed to get the latest release from github.com/%s/%s: %s", r.owner, r.repo, err) | ||
} | ||
|
||
name := *release.Name | ||
|
||
// if a name starts with `v`, remove it. | ||
if name[0] == 'v' { | ||
return name[1:], nil | ||
} | ||
|
||
return name, 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package release | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Release is an interface which provides version information. | ||
type Release interface { | ||
// Latest returns a latest version. | ||
Latest() (string, error) | ||
} | ||
|
||
// NewRelease is a factory method which returns a Release implementation. | ||
func NewRelease(releaseType string, url string) (Release, error) { | ||
switch releaseType { | ||
case "github": | ||
return NewGitHubRelease(url) | ||
default: | ||
return nil, errors.Errorf("failed to new release. unknown type: %s", releaseType) | ||
} | ||
} | ||
|
||
// ResolveVersionAlias resolves a version alias. | ||
func ResolveVersionAlias(r Release, alias string) (string, error) { | ||
switch alias { | ||
case "latest": | ||
return r.Latest() | ||
default: | ||
// if an alias does not match keywords, just return alias as a version. | ||
return alias, nil | ||
} | ||
} |