command: Get works

This commit is contained in:
Mitchell Hashimoto 2014-09-22 11:15:27 -07:00
parent ed538a9594
commit 1b8426f7ff
8 changed files with 73 additions and 4 deletions

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"github.com/hashicorp/terraform/config/module"
) )
// GetCommand is a Command implementation that takes a Terraform // GetCommand is a Command implementation that takes a Terraform
@ -39,7 +41,8 @@ func (c *GetCommand) Run(args []string) int {
} }
_, _, err := c.Context(contextOpts{ _, _, err := c.Context(contextOpts{
Path: path, Path: path,
GetMode: module.GetModeGet,
}) })
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err))

View File

@ -2,6 +2,7 @@ package command
import ( import (
"os" "os"
"strings"
"testing" "testing"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -17,11 +18,16 @@ func TestGet(t *testing.T) {
} }
args := []string{ args := []string{
testFixturePath("graph"), testFixturePath("get"),
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 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) { func TestGet_multipleArgs(t *testing.T) {
@ -47,13 +53,13 @@ func TestGet_noArgs(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) 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) t.Fatalf("err: %s", err)
} }
defer os.Chdir(cwd) defer os.Chdir(cwd)
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &GraphCommand{ c := &GetCommand{
Meta: Meta{ Meta: Meta{
ContextOpts: testCtxConfig(testProvider()), ContextOpts: testCtxConfig(testProvider()),
Ui: ui, Ui: ui,
@ -64,4 +70,9 @@ func TestGet_noArgs(t *testing.T) {
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 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)
}
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -93,6 +94,10 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) {
if err != nil { if err != nil {
return nil, false, fmt.Errorf("Error loading config: %s", err) 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.Config = mod.Config()
opts.State = state opts.State = state
@ -152,6 +157,17 @@ func (m *Meta) flagSet(n string) *flag.FlagSet {
return f 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 // process will process the meta-parameters out of the arguments. This
// will potentially modify the args in-place. It will return the resulting // will potentially modify the args in-place. It will return the resulting
// slice. // slice.

24
command/module_storage.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

View File

@ -0,0 +1,3 @@
module "foo" {
source = "./foo"
}

View File

@ -0,0 +1 @@
/Users/mitchellh/code/go/src/github.com/hashicorp/terraform/command/test-fixtures/get/foo