Skip to content

Commit

Permalink
Move recursive flag to tfupdate.Option
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Sep 21, 2019
1 parent cd07311 commit 30d8883
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions command/provider.go
Expand Up @@ -38,8 +38,8 @@ func (c *ProviderCommand) Run(args []string) int {
return 1
}

option := tfupdate.NewOption("provider", c.target)
err := tfupdate.UpdateFileOrDir(c.Fs, c.path, c.recursive, option)
option := tfupdate.NewOption("provider", c.target, c.recursive)
err := tfupdate.UpdateFileOrDir(c.Fs, c.path, option)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
4 changes: 2 additions & 2 deletions command/terraform.go
Expand Up @@ -38,8 +38,8 @@ func (c *TerraformCommand) Run(args []string) int {
return 1
}

option := tfupdate.NewOption("terraform", c.target)
err := tfupdate.UpdateFileOrDir(c.Fs, c.path, c.recursive, option)
option := tfupdate.NewOption("terraform", c.target, c.recursive)
err := tfupdate.UpdateFileOrDir(c.Fs, c.path, option)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
10 changes: 5 additions & 5 deletions tfupdate/file.go
Expand Up @@ -49,7 +49,7 @@ func UpdateFile(fs afero.Fs, filename string, o Option) error {
// If a recursive flag is true, it checks and updates recursively.
// skip hidden directories such as .terraform or .git.
// It also skips a file without .tf extension.
func UpdateDir(fs afero.Fs, dirname string, recursive bool, o Option) error {
func UpdateDir(fs afero.Fs, dirname string, o Option) error {
log.Printf("[DEBUG] check dir: %s", dirname)
dir, err := afero.ReadDir(fs, dirname)
if err != nil {
Expand All @@ -61,7 +61,7 @@ func UpdateDir(fs afero.Fs, dirname string, recursive bool, o Option) error {

if entry.IsDir() {
// if an entry is a directory
if !recursive {
if !o.recursive {
// skip directory if a recursive flag is false
continue
}
Expand All @@ -70,7 +70,7 @@ func UpdateDir(fs afero.Fs, dirname string, recursive bool, o Option) error {
continue
}

err := UpdateDir(fs, path, recursive, o)
err := UpdateDir(fs, path, o)
if err != nil {
return err
}
Expand All @@ -93,15 +93,15 @@ func UpdateDir(fs afero.Fs, dirname string, recursive bool, o Option) error {
}

// UpdateFileOrDir updates version constraints in a given file or directory.
func UpdateFileOrDir(fs afero.Fs, path string, recursive bool, o Option) error {
func UpdateFileOrDir(fs afero.Fs, path string, o Option) error {
isDir, err := afero.IsDir(fs, path)
if err != nil {
return fmt.Errorf("failed to open path: %s", err)
}

if isDir {
// if an entry is a directory
return UpdateDir(fs, path, recursive, o)
return UpdateDir(fs, path, o)
}

// if an entry is a file
Expand Down
45 changes: 21 additions & 24 deletions tfupdate/file_test.go
Expand Up @@ -138,7 +138,6 @@ func TestUpdateDirExist(t *testing.T) {
src2 string
o Option
checkdir string
recursive bool
want1 string
want2 string
}{
Expand All @@ -157,11 +156,11 @@ provider "aws" {
version = "2.11.0"
}
`,
checkdir: "a/b",
recursive: false,
checkdir: "a/b",
o: Option{
updateType: "terraform",
target: "0.12.7",
recursive: false,
},
want1: `
terraform {
Expand Down Expand Up @@ -189,11 +188,11 @@ provider "aws" {
version = "2.11.0"
}
`,
checkdir: "a",
recursive: true,
checkdir: "a",
o: Option{
updateType: "terraform",
target: "0.12.7",
recursive: true,
},
want1: `
terraform {
Expand Down Expand Up @@ -221,11 +220,11 @@ provider "aws" {
version = "2.11.0"
}
`,
checkdir: "a",
recursive: false,
checkdir: "a",
o: Option{
updateType: "terraform",
target: "0.12.7",
recursive: false,
},
want1: `
terraform {
Expand Down Expand Up @@ -253,11 +252,11 @@ provider "aws" {
version = "2.11.0"
}
`,
checkdir: "a",
recursive: true,
checkdir: "a",
o: Option{
updateType: "terraform",
target: "0.12.7",
recursive: true,
},
want1: `
terraform {
Expand Down Expand Up @@ -285,11 +284,11 @@ provider "aws" {
version = "2.11.0"
}
`,
checkdir: "a",
recursive: true,
checkdir: "a",
o: Option{
updateType: "terraform",
target: "0.12.7",
recursive: true,
},
want1: `
terraform {
Expand Down Expand Up @@ -317,11 +316,11 @@ provider "aws" {
version = "2.11.0"
}
`,
checkdir: "a/b",
recursive: false,
checkdir: "a/b",
o: Option{
updateType: "terraform",
target: "0.12.7",
recursive: false,
},
want1: `
terraform {
Expand Down Expand Up @@ -354,10 +353,10 @@ provider "aws" {
t.Fatalf("failed to write file: %s", err)
}

err = UpdateDir(fs, tc.checkdir, tc.recursive, tc.o)
err = UpdateDir(fs, tc.checkdir, tc.o)

if err != nil {
t.Errorf("UpdateDir() with dirname = %s, recursive = %t, o = %#v returns an unexpected error: %+v", tc.checkdir, tc.recursive, tc.o, err)
t.Errorf("UpdateDir() with dirname = %s, o = %#v returns an unexpected error: %+v", tc.checkdir, tc.o, err)
}

got1, err := afero.ReadFile(fs, filepath.Join(dirname, tc.filename1))
Expand All @@ -366,7 +365,7 @@ provider "aws" {
}

if string(got1) != tc.want1 {
t.Errorf("UpdateDir() with dirname = %s, recursive = %t, o = %#v returns %s, but want = %s", dirname, tc.recursive, tc.o, string(got1), tc.want1)
t.Errorf("UpdateDir() with dirname = %s, o = %#v returns %s, but want = %s", dirname, tc.o, string(got1), tc.want1)
}

got2, err := afero.ReadFile(fs, filepath.Join(dirname, tc.filename2))
Expand All @@ -375,21 +374,20 @@ provider "aws" {
}

if string(got2) != tc.want2 {
t.Errorf("UpdateDir() with dirname = %s, recursive = %t, o = %#v returns %s, but want = %s", dirname, tc.recursive, tc.o, string(got2), tc.want2)
t.Errorf("UpdateDir() with dirname = %s, o = %#v returns %s, but want = %s", dirname, tc.o, string(got2), tc.want2)
}
}
}

func TestUpdateDirNotFound(t *testing.T) {
fs := afero.NewMemMapFs()
dirname := "not_found"
recursive := false
o := Option{}

err := UpdateDir(fs, dirname, recursive, o)
err := UpdateDir(fs, dirname, o)

if err == nil {
t.Errorf("UpdateDir() with dirname = %s, recursive = %t, o = %#v expects to return an error, but no error", dirname, recursive, o)
t.Errorf("UpdateDir() with dirname = %s, o = %#v expects to return an error, but no error", dirname, o)
}
}

Expand All @@ -400,7 +398,6 @@ terraform {
required_version = "0.12.6"
}
`
recursive := false
o := Option{
updateType: "terraform",
target: "0.12.7",
Expand Down Expand Up @@ -435,10 +432,10 @@ terraform {
t.Fatalf("failed to write file: %s", err)
}

err = UpdateFileOrDir(fs, tc.path, recursive, o)
err = UpdateFileOrDir(fs, tc.path, o)

if err != nil {
t.Errorf("UpdateFileOrDir() with path = %s, recursive = %t, o = %#v returns an unexpected error: %+v", tc.path, recursive, o, err)
t.Errorf("UpdateFileOrDir() with path = %s, o = %#v returns an unexpected error: %+v", tc.path, o, err)
}

got, err := afero.ReadFile(fs, filepath.Join(dirname, filename))
Expand All @@ -447,7 +444,7 @@ terraform {
}

if string(got) != want {
t.Errorf("UpdateFileOrDir() with path = %s, recursive = %t, o = %#v returns %s, but want = %s", tc.path, recursive, o, string(got), want)
t.Errorf("UpdateFileOrDir() with path = %s, o = %#v returns %s, but want = %s", tc.path, o, string(got), want)
}
}
}
7 changes: 6 additions & 1 deletion tfupdate/update.go
Expand Up @@ -23,10 +23,14 @@ type Updater interface {
type Option struct {
// A type of updater. Valid value is terraform or provider.
updateType 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

// If a recursive flag is true, it checks and updates directories recursively.
recursive bool
}

// NewUpdater is a factory method which returns an Updater implementation.
Expand All @@ -48,10 +52,11 @@ func NewUpdater(o Option) (Updater, error) {
}

// NewOption returns an option.
func NewOption(updateType string, target string) Option {
func NewOption(updateType string, target string, recursive bool) Option {
return Option{
updateType: updateType,
target: target,
recursive: recursive,
}
}

Expand Down

0 comments on commit 30d8883

Please sign in to comment.