From 1b8426f7ff7c6489b3a6163a3c73557c13d72123 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 22 Sep 2014 11:15:27 -0700 Subject: [PATCH] command: Get works --- command/get.go | 5 +++- command/get_test.go | 17 ++++++++++--- command/meta.go | 16 +++++++++++++ command/module_storage.go | 24 +++++++++++++++++++ command/module_storage_test.go | 11 +++++++++ command/test-fixtures/get/foo/main.tf | 0 command/test-fixtures/get/main.tf | 3 +++ .../modules/3287951ef4002ba5dabcc7ef3ea0b206 | 1 + 8 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 command/module_storage.go create mode 100644 command/module_storage_test.go create mode 100644 command/test-fixtures/get/foo/main.tf create mode 100644 command/test-fixtures/get/main.tf create mode 120000 command/test-fixtures/get/modules/3287951ef4002ba5dabcc7ef3ea0b206 diff --git a/command/get.go b/command/get.go index 394a9931c..2d3dec4a5 100644 --- a/command/get.go +++ b/command/get.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "strings" + + "github.com/hashicorp/terraform/config/module" ) // GetCommand is a Command implementation that takes a Terraform @@ -39,7 +41,8 @@ func (c *GetCommand) Run(args []string) int { } _, _, err := c.Context(contextOpts{ - Path: path, + Path: path, + GetMode: module.GetModeGet, }) if err != nil { c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) diff --git a/command/get_test.go b/command/get_test.go index dcde6d6b0..b779222f8 100644 --- a/command/get_test.go +++ b/command/get_test.go @@ -2,6 +2,7 @@ package command import ( "os" + "strings" "testing" "github.com/mitchellh/cli" @@ -17,11 +18,16 @@ func TestGet(t *testing.T) { } args := []string{ - testFixturePath("graph"), + testFixturePath("get"), } if code := c.Run(args); code != 0 { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } + + output := ui.OutputWriter.String() + if !strings.Contains(output, "Get: file://") { + t.Fatalf("doesn't look like get: %s", output) + } } func TestGet_multipleArgs(t *testing.T) { @@ -47,13 +53,13 @@ func TestGet_noArgs(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } - if err := os.Chdir(testFixturePath("graph")); err != nil { + if err := os.Chdir(testFixturePath("get")); err != nil { t.Fatalf("err: %s", err) } defer os.Chdir(cwd) ui := new(cli.MockUi) - c := &GraphCommand{ + c := &GetCommand{ Meta: Meta{ ContextOpts: testCtxConfig(testProvider()), Ui: ui, @@ -64,4 +70,9 @@ func TestGet_noArgs(t *testing.T) { if code := c.Run(args); code != 0 { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } + + output := ui.OutputWriter.String() + if !strings.Contains(output, "Get: file://") { + t.Fatalf("doesn't look like get: %s", output) + } } diff --git a/command/meta.go b/command/meta.go index e2708dcd8..c7708eceb 100644 --- a/command/meta.go +++ b/command/meta.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "path/filepath" "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/terraform" @@ -93,6 +94,10 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) { if err != nil { return nil, false, fmt.Errorf("Error loading config: %s", err) } + err = mod.Load(m.moduleStorage(copts.Path), copts.GetMode) + if err != nil { + return nil, false, fmt.Errorf("Error downloading modules: %s", err) + } opts.Config = mod.Config() opts.State = state @@ -152,6 +157,17 @@ func (m *Meta) flagSet(n string) *flag.FlagSet { return f } +// moduleStorage returns the module.Storage implementation used to store +// modules for commands. +func (m *Meta) moduleStorage(root string) module.Storage { + return &uiModuleStorage{ + Storage: &module.FolderStorage{ + StorageDir: filepath.Join(root, "modules"), + }, + Ui: m.Ui, + } +} + // process will process the meta-parameters out of the arguments. This // will potentially modify the args in-place. It will return the resulting // slice. diff --git a/command/module_storage.go b/command/module_storage.go new file mode 100644 index 000000000..0a33abe4a --- /dev/null +++ b/command/module_storage.go @@ -0,0 +1,24 @@ +package command + +import ( + "fmt" + + "github.com/hashicorp/terraform/config/module" + "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 module.Storage + Ui cli.Ui +} + +func (s *uiModuleStorage) Dir(source string) (string, bool, error) { + return s.Storage.Dir(source) +} + +func (s *uiModuleStorage) Get(source string, update bool) error { + s.Ui.Output(fmt.Sprintf("Get: %s", source)) + return s.Storage.Get(source, update) +} diff --git a/command/module_storage_test.go b/command/module_storage_test.go new file mode 100644 index 000000000..b77c2b5f7 --- /dev/null +++ b/command/module_storage_test.go @@ -0,0 +1,11 @@ +package command + +import ( + "testing" + + "github.com/hashicorp/terraform/config/module" +) + +func TestUiModuleStorage_impl(t *testing.T) { + var _ module.Storage = new(uiModuleStorage) +} diff --git a/command/test-fixtures/get/foo/main.tf b/command/test-fixtures/get/foo/main.tf new file mode 100644 index 000000000..e69de29bb diff --git a/command/test-fixtures/get/main.tf b/command/test-fixtures/get/main.tf new file mode 100644 index 000000000..0ce1c38d3 --- /dev/null +++ b/command/test-fixtures/get/main.tf @@ -0,0 +1,3 @@ +module "foo" { + source = "./foo" +} diff --git a/command/test-fixtures/get/modules/3287951ef4002ba5dabcc7ef3ea0b206 b/command/test-fixtures/get/modules/3287951ef4002ba5dabcc7ef3ea0b206 new file mode 120000 index 000000000..3cbb92882 --- /dev/null +++ b/command/test-fixtures/get/modules/3287951ef4002ba5dabcc7ef3ea0b206 @@ -0,0 +1 @@ +/Users/mitchellh/code/go/src/github.com/hashicorp/terraform/command/test-fixtures/get/foo \ No newline at end of file