From f2a7b94692eb03358a129d094c8dcaf4ee576a4a Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 27 Oct 2017 12:58:24 -0400 Subject: [PATCH] use the new ModuleStorage in the command package Update the command package to use the new module storage. Move the old command output strings into the module storage itself. This could be moved back later either by using ui callbacks, or designing a module storage interface once we know what the final requirements will look like. --- command/command_test.go | 8 +++++--- command/get.go | 2 +- command/meta.go | 14 +++++++------- command/meta_new.go | 2 +- command/module_storage.go | 29 ----------------------------- command/module_storage_test.go | 11 ----------- config/module/storage.go | 8 ++++++++ helper/resource/testing.go | 6 +++--- 8 files changed, 25 insertions(+), 55 deletions(-) delete mode 100644 command/module_storage.go delete mode 100644 command/module_storage_test.go diff --git a/command/command_test.go b/command/command_test.go index 598805a2cf..e867ae45d1 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -19,7 +19,6 @@ import ( "syscall" "testing" - "github.com/hashicorp/go-getter" "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/terraform" @@ -108,8 +107,11 @@ func testModule(t *testing.T, name string) *module.Tree { t.Fatalf("err: %s", err) } - s := &getter.FolderStorage{StorageDir: tempDir(t)} - if err := mod.Load(s, module.GetModeGet); err != nil { + s := &module.ModuleStorage{ + StorageDir: tempDir(t), + Mode: module.GetModeGet, + } + if err := mod.Load(s); err != nil { t.Fatalf("err: %s", err) } diff --git a/command/get.go b/command/get.go index d51ae39b31..ba8729d79a 100644 --- a/command/get.go +++ b/command/get.go @@ -81,7 +81,7 @@ func getModules(m *Meta, path string, mode module.GetMode) error { return fmt.Errorf("Error loading configuration: %s", err) } - err = mod.Load(m.moduleStorage(m.DataDir()), mode) + err = mod.Load(m.moduleStorage(m.DataDir(), mode)) if err != nil { return fmt.Errorf("Error loading modules: %s", err) } diff --git a/command/meta.go b/command/meta.go index bb58ab6541..24493116e1 100644 --- a/command/meta.go +++ b/command/meta.go @@ -16,11 +16,11 @@ import ( "strings" "time" - "github.com/hashicorp/go-getter" "github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend/local" "github.com/hashicorp/terraform/command/format" "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/helper/experiment" "github.com/hashicorp/terraform/helper/variables" "github.com/hashicorp/terraform/helper/wrappedstreams" @@ -368,12 +368,12 @@ func (m *Meta) flagSet(n string) *flag.FlagSet { // moduleStorage returns the module.Storage implementation used to store // modules for commands. -func (m *Meta) moduleStorage(root string) getter.Storage { - return &uiModuleStorage{ - Storage: &getter.FolderStorage{ - StorageDir: filepath.Join(root, "modules"), - }, - Ui: m.Ui, +func (m *Meta) moduleStorage(root string, mode module.GetMode) *module.ModuleStorage { + return &module.ModuleStorage{ + StorageDir: filepath.Join(root, "modules"), + Services: m.Services, + Ui: m.Ui, + Mode: mode, } } diff --git a/command/meta_new.go b/command/meta_new.go index 9447caf3b8..5fc3cdca36 100644 --- a/command/meta_new.go +++ b/command/meta_new.go @@ -45,7 +45,7 @@ func (m *Meta) Module(path string) (*module.Tree, error) { return nil, err } - err = mod.Load(m.moduleStorage(m.DataDir()), module.GetModeNone) + err = mod.Load(m.moduleStorage(m.DataDir(), module.GetModeNone)) if err != nil { return nil, errwrap.Wrapf("Error loading modules: {{err}}", err) } diff --git a/command/module_storage.go b/command/module_storage.go deleted file mode 100644 index 5bb832897d..0000000000 --- a/command/module_storage.go +++ /dev/null @@ -1,29 +0,0 @@ -package command - -import ( - "fmt" - - "github.com/hashicorp/go-getter" - "github.com/mitchellh/cli" -) - -// uiModuleStorage implements module.Storage and is just a proxy to output -// to the UI any Get operations. -type uiModuleStorage struct { - Storage getter.Storage - Ui cli.Ui -} - -func (s *uiModuleStorage) Dir(key string) (string, bool, error) { - return s.Storage.Dir(key) -} - -func (s *uiModuleStorage) Get(key string, source string, update bool) error { - updateStr := "" - if update { - updateStr = " (update)" - } - - s.Ui.Output(fmt.Sprintf("Get: %s%s", source, updateStr)) - return s.Storage.Get(key, source, update) -} diff --git a/command/module_storage_test.go b/command/module_storage_test.go deleted file mode 100644 index 97a5ed7ae3..0000000000 --- a/command/module_storage_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package command - -import ( - "testing" - - "github.com/hashicorp/go-getter" -) - -func TestUiModuleStorage_impl(t *testing.T) { - var _ getter.Storage = new(uiModuleStorage) -} diff --git a/config/module/storage.go b/config/module/storage.go index f4cf2bd0f6..9280ce9ae0 100644 --- a/config/module/storage.go +++ b/config/module/storage.go @@ -190,6 +190,14 @@ func (m ModuleStorage) getStorage(key string, src string) (string, bool, error) StorageDir: m.StorageDir, } + if m.Ui != nil { + update := "" + if m.Mode == GetModeUpdate { + update = " (update)" + } + m.Ui.Output(fmt.Sprintf("Get: %s%s", src, update)) + } + // Get the module with the level specified if we were told to. if m.Mode > GetModeNone { log.Printf("[DEBUG] fetching %q with key %q", src, key) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index aa0aa51fba..20fc0145bf 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -15,7 +15,6 @@ import ( "testing" "github.com/davecgh/go-spew/spew" - "github.com/hashicorp/go-getter" "github.com/hashicorp/go-multierror" "github.com/hashicorp/logutils" "github.com/hashicorp/terraform/config/module" @@ -751,10 +750,11 @@ func testModule( } // Load the modules - modStorage := &getter.FolderStorage{ + modStorage := &module.ModuleStorage{ StorageDir: filepath.Join(cfgPath, ".tfmodules"), + Mode: module.GetModeGet, } - err = mod.Load(modStorage, module.GetModeGet) + err = mod.Load(modStorage) if err != nil { return nil, fmt.Errorf("Error downloading modules: %s", err) }