From 30d888351725f1773cfde2d972b695f339f25a34 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Sat, 21 Sep 2019 23:30:00 +0900 Subject: [PATCH] Move recursive flag to tfupdate.Option --- command/provider.go | 4 ++-- command/terraform.go | 4 ++-- tfupdate/file.go | 10 +++++----- tfupdate/file_test.go | 45 ++++++++++++++++++++----------------------- tfupdate/update.go | 7 ++++++- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/command/provider.go b/command/provider.go index 4df9488..e6b01e7 100644 --- a/command/provider.go +++ b/command/provider.go @@ -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 diff --git a/command/terraform.go b/command/terraform.go index 7a86f71..743ed89 100644 --- a/command/terraform.go +++ b/command/terraform.go @@ -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 diff --git a/tfupdate/file.go b/tfupdate/file.go index 7ac0ca2..b448ed6 100644 --- a/tfupdate/file.go +++ b/tfupdate/file.go @@ -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 { @@ -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 } @@ -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 } @@ -93,7 +93,7 @@ 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) @@ -101,7 +101,7 @@ func UpdateFileOrDir(fs afero.Fs, path string, recursive bool, o Option) error { 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 diff --git a/tfupdate/file_test.go b/tfupdate/file_test.go index edd2574..d5b6adb 100644 --- a/tfupdate/file_test.go +++ b/tfupdate/file_test.go @@ -138,7 +138,6 @@ func TestUpdateDirExist(t *testing.T) { src2 string o Option checkdir string - recursive bool want1 string want2 string }{ @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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)) @@ -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)) @@ -375,7 +374,7 @@ 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) } } } @@ -383,13 +382,12 @@ provider "aws" { 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) } } @@ -400,7 +398,6 @@ terraform { required_version = "0.12.6" } ` - recursive := false o := Option{ updateType: "terraform", target: "0.12.7", @@ -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)) @@ -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) } } } diff --git a/tfupdate/update.go b/tfupdate/update.go index 4b8cb00..998fe1e 100644 --- a/tfupdate/update.go +++ b/tfupdate/update.go @@ -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. @@ -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, } }