command/graph: takes config dir as arg

This commit is contained in:
Mitchell Hashimoto 2014-07-11 20:38:03 -07:00
parent 10d17c4d40
commit 8f7244695f
4 changed files with 73 additions and 4 deletions

View File

@ -13,7 +13,7 @@ import (
const fixtureDir = "./test-fixtures"
func testFixturePath(name string) string {
return filepath.Join(fixtureDir, name, "main.tf")
return filepath.Join(fixtureDir, name)
}
func testCtxConfig(p terraform.ResourceProvider) *terraform.ContextOpts {

View File

@ -1,9 +1,9 @@
package command
import (
"bytes"
"flag"
"fmt"
"os"
"strings"
"github.com/hashicorp/terraform/config"
@ -33,7 +33,9 @@ func (c *GraphCommand) Run(args []string) int {
return 1
}
conf, err := config.Load(args[0])
path := args[0]
conf, err := config.LoadDir(path)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading config: %s", err))
return 1
@ -48,11 +50,14 @@ func (c *GraphCommand) Run(args []string) int {
return 1
}
buf := new(bytes.Buffer)
nodes := make([]digraph.Node, len(g.Nouns))
for i, n := range g.Nouns {
nodes[i] = n
}
digraph.GenerateDot(nodes, os.Stdout)
digraph.GenerateDot(nodes, buf)
c.Ui.Output(buf.String())
return 0
}
@ -66,6 +71,10 @@ Usage: terraform graph [options] PATH
shown. If the path is a plan file, then the dependency graph of the
plan itself is shown.
The graph is outputted in DOT format. The typical program that can
read this format is GraphViz, but many web services are also available
to read this format.
`
return strings.TrimSpace(helpText)
}

57
command/graph_test.go Normal file
View File

@ -0,0 +1,57 @@
package command
import (
"strings"
"testing"
"github.com/mitchellh/cli"
)
func TestGraph(t *testing.T) {
ui := new(cli.MockUi)
c := &GraphCommand{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
}
args := []string{
testFixturePath("graph"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
output := ui.OutputWriter.String()
if !strings.Contains(output, "digraph {") {
t.Fatalf("doesn't look like digraph: %s", output)
}
}
func TestGraph_noArgs(t *testing.T) {
ui := new(cli.MockUi)
c := &ApplyCommand{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
}
args := []string{}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}
func TestGraph_multipleArgs(t *testing.T) {
ui := new(cli.MockUi)
c := &ApplyCommand{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
}
args := []string{
"bad",
"bad",
}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}

View File

@ -0,0 +1,3 @@
resource "test_instance" "foo" {
ami = "bar"
}