mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-13 09:32:24 -06:00
command/graph: takes config dir as arg
This commit is contained in:
parent
10d17c4d40
commit
8f7244695f
@ -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 {
|
||||
|
@ -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
57
command/graph_test.go
Normal 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())
|
||||
}
|
||||
}
|
3
command/test-fixtures/graph/main.tf
Normal file
3
command/test-fixtures/graph/main.tf
Normal file
@ -0,0 +1,3 @@
|
||||
resource "test_instance" "foo" {
|
||||
ami = "bar"
|
||||
}
|
Loading…
Reference in New Issue
Block a user