Skip to content

Commit

Permalink
Get the latest release version automatically from GitHub Release
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Oct 17, 2019
1 parent f00a15d commit c654f9f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
18 changes: 14 additions & 4 deletions README.md
Expand Up @@ -13,7 +13,7 @@ That is why I wrote a tool which parses Terraform configurations and updates all

- Update version constraints of Terraform core and providers
- Update all your Terraform configurations recursively under a given directory
- Get the latest release version from GitHub Release
- Get the latest release version automatically from GitHub Release

# Supported Terraform version

Expand Down Expand Up @@ -84,7 +84,13 @@ If you want to update all your Terraform configurations under the current direct
run a command like this:

```
$ tfupdate terraform -v 0.12.8 ./ -r
$ tfupdate terraform -v 0.12.8 -r ./
```

If the version is omitted, the latest version is automatically checked and set.

```
$ tfupdate terraform -r ./
```

# Usage
Expand All @@ -107,7 +113,8 @@ Arguments
PATH A path of file or directory to update
Options:
-v --version A new version constraint
-v --version A new version constraint (default: latest)
If the version is omitted, the latest version is automatically checked and set.
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
If you want to ignore multiple directories, set the flag multiple times.
Expand All @@ -122,7 +129,10 @@ Arguments
PATH A path of file or directory to update
Options:
-v --version A new version constraint
-v --version A new version constraint (default: latest)
If the version is omitted, the latest version is automatically checked and set.
Getting the latest version automatically is supported only for official providers.
If you have an unofficial provider, use release latest command.
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
If you want to ignore multiple directories, set the flag multiple times.
Expand Down
25 changes: 23 additions & 2 deletions command/provider.go
Expand Up @@ -2,8 +2,10 @@ package command

import (
"fmt"
"log"
"strings"

"github.com/minamijoyo/tfupdate/release"
"github.com/minamijoyo/tfupdate/tfupdate"
flag "github.com/spf13/pflag"
)
Expand Down Expand Up @@ -39,7 +41,23 @@ func (c *ProviderCommand) Run(args []string) int {
c.name = cmdFlags.Arg(0)
c.path = cmdFlags.Arg(1)

option, err := tfupdate.NewOption("provider", c.name, c.version, c.recursive, c.ignorePaths)
v := c.version
if v == "latest" {
r, err := release.NewOfficialProviderRelease(c.name)
if err != nil {
c.UI.Error(err.Error())
return 1
}

v, err = r.Latest()
if err != nil {
c.UI.Error(err.Error())
return 1
}
}

log.Printf("[INFO] Update provider %s to %s", c.name, v)
option, err := tfupdate.NewOption("provider", c.name, v, c.recursive, c.ignorePaths)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand All @@ -64,7 +82,10 @@ Arguments
PATH A path of file or directory to update
Options:
-v --version A new version constraint
-v --version A new version constraint (default: latest)
If the version is omitted, the latest version is automatically checked and set.
Getting the latest version automatically is supported only for official providers.
If you have an unofficial provider, use release latest command.
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
If you want to ignore multiple directories, set the flag multiple times.
Expand Down
23 changes: 21 additions & 2 deletions command/terraform.go
Expand Up @@ -2,8 +2,10 @@ package command

import (
"fmt"
"log"
"strings"

"github.com/minamijoyo/tfupdate/release"
"github.com/minamijoyo/tfupdate/tfupdate"
flag "github.com/spf13/pflag"
)
Expand Down Expand Up @@ -37,7 +39,23 @@ func (c *TerraformCommand) Run(args []string) int {

c.path = cmdFlags.Arg(0)

option, err := tfupdate.NewOption("terraform", "", c.version, c.recursive, c.ignorePaths)
v := c.version
if v == "latest" {
r, err := release.NewTerraformRelease()
if err != nil {
c.UI.Error(err.Error())
return 1
}

v, err = r.Latest()
if err != nil {
c.UI.Error(err.Error())
return 1
}
}

log.Printf("[INFO] Update terraform to %s", v)
option, err := tfupdate.NewOption("terraform", "", v, c.recursive, c.ignorePaths)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand All @@ -61,7 +79,8 @@ Arguments
PATH A path of file or directory to update
Options:
-v --version A new version constraint
-v --version A new version constraint (default: latest)
If the version is omitted, the latest version is automatically checked and set.
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
If you want to ignore multiple directories, set the flag multiple times.
Expand Down
27 changes: 27 additions & 0 deletions release/provider.go
@@ -0,0 +1,27 @@
package release

import "fmt"

// OfficialProviderRelease is a release implementation which provides version information with GitHub Release.
type OfficialProviderRelease struct {
gh *GitHubRelease
}

// NewOfficialProviderRelease is a factory method which returns an OfficialProviderRelease instance.
func NewOfficialProviderRelease(name string) (Release, error) {
owner := "terraform-providers"
repo := fmt.Sprintf("terraform-provider-%s", name)
r, err := NewGitHubRelease(owner, repo)
if err != nil {
return nil, err
}

return &OfficialProviderRelease{
gh: r.(*GitHubRelease),
}, nil
}

// Latest returns a latest version.
func (r *OfficialProviderRelease) Latest() (string, error) {
return r.gh.Latest()
}
23 changes: 23 additions & 0 deletions release/terraform.go
@@ -0,0 +1,23 @@
package release

// TerraformRelease is a release implementation which provides version information with GitHub Release.
type TerraformRelease struct {
gh *GitHubRelease
}

// NewTerraformRelease is a factory method which returns an TerraformRelease instance.
func NewTerraformRelease() (Release, error) {
r, err := NewGitHubRelease("hashicorp", "terraform")
if err != nil {
return nil, err
}

return &TerraformRelease{
gh: r.(*GitHubRelease),
}, nil
}

// Latest returns a latest version.
func (r *TerraformRelease) Latest() (string, error) {
return r.gh.Latest()
}

0 comments on commit c654f9f

Please sign in to comment.