Skip to content

Commit

Permalink
Unify version constraints of tfupdate.Option to a target string
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Sep 12, 2019
1 parent 232366a commit f70bd9f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
11 changes: 5 additions & 6 deletions command/provider.go
Expand Up @@ -22,18 +22,17 @@ func (c *ProviderCommand) Run(args []string) int {
return 1
}

if len(cmdFlags.Args()) != 2 {
c.UI.Error("The provider command expects <NAME> and <VERSION>")
if len(cmdFlags.Args()) != 1 {
c.UI.Error("The provider command expects <NAME>@<VERSION>")
c.UI.Error(c.Help())
return 1
}

updateType := "provider"
name := cmdFlags.Args()[0]
version := cmdFlags.Args()[1]
target := cmdFlags.Args()[0]
filename := c.path

option := tfupdate.NewOption(updateType, name, version)
option := tfupdate.NewOption(updateType, target)
err := tfupdate.UpdateFile(filename, option)
if err != nil {
c.UI.Error(err.Error())
Expand All @@ -46,7 +45,7 @@ func (c *ProviderCommand) Run(args []string) int {
// Help returns long-form help text.
func (c *ProviderCommand) Help() string {
helpText := `
Usage: tfupdate provider [options] <NAME> <VERSION>
Usage: tfupdate provider [options] <NAME>@<VERSION>
Options:
Expand Down
5 changes: 2 additions & 3 deletions command/terraform.go
Expand Up @@ -29,11 +29,10 @@ func (c *TerraformCommand) Run(args []string) int {
}

updateType := "terraform"
name := ""
version := cmdFlags.Args()[0]
target := cmdFlags.Args()[0]
filename := c.path

option := tfupdate.NewOption(updateType, name, version)
option := tfupdate.NewOption(updateType, target)
err := tfupdate.UpdateFile(filename, option)
if err != nil {
c.UI.Error(err.Error())
Expand Down
21 changes: 13 additions & 8 deletions tfupdate/updater.go
@@ -1,6 +1,8 @@
package tfupdate

import (
"strings"

"github.com/hashicorp/hcl2/hclwrite"
"github.com/pkg/errors"
)
Expand All @@ -14,20 +16,24 @@ type Updater interface {

// Option is a set of parameters to update.
type Option struct {
// A type of updater. Valid value is terraform or provider.
updateType string
name string
version string
// A target to be updated.
// If an updateType is terraform, Set a version.
// If an updateType is provider, Set a name@version.
target string
}

// NewUpdater is a factory method which returns an Updater implementation.
func NewUpdater(o Option) (Updater, error) {
switch o.updateType {
case "terraform":
return NewTerraformUpdater(o.version)
return NewTerraformUpdater(o.target)
case "provider":
s := strings.Split(o.target, "@")
return &ProviderUpdater{
name: o.name,
version: o.version,
name: s[0],
version: s[1],
}, nil
case "module":
return nil, errors.Errorf("failed to new updater. module is not currently supported.")
Expand All @@ -37,10 +43,9 @@ func NewUpdater(o Option) (Updater, error) {
}

// NewOption returns an option.
func NewOption(updateType string, name string, version string) Option {
func NewOption(updateType string, target string) Option {
return Option{
updateType: updateType,
name: name,
version: version,
target: target,
}
}
12 changes: 4 additions & 8 deletions tfupdate/updater_test.go
Expand Up @@ -14,8 +14,7 @@ func TestNewUpdater(t *testing.T) {
{
o: Option{
updateType: "terraform",
name: "",
version: "0.12.7",
target: "0.12.7",
},
want: &TerraformUpdater{
version: "0.12.7",
Expand All @@ -25,8 +24,7 @@ func TestNewUpdater(t *testing.T) {
{
o: Option{
updateType: "provider",
name: "aws",
version: "2.23.0",
target: "aws@2.23.0",
},
want: &ProviderUpdater{
name: "aws",
Expand All @@ -37,17 +35,15 @@ func TestNewUpdater(t *testing.T) {
{
o: Option{
updateType: "module",
name: "terraform-aws-modules/vpc/aws",
version: "2.14.0",
target: "terraform-aws-modules/vpc/aws@2.14.0",
},
want: nil,
ok: false,
},
{
o: Option{
updateType: "hoge",
name: "",
version: "0.0.1",
target: "0.0.1",
},
want: nil,
ok: false,
Expand Down

0 comments on commit f70bd9f

Please sign in to comment.