github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/command/debug_json2dot.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/dag"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  // DebugJSON2DotCommand is a Command implementation that translates a json
    13  // graph debug log to Dot format.
    14  type DebugJSON2DotCommand struct {
    15  	Meta
    16  }
    17  
    18  func (c *DebugJSON2DotCommand) Run(args []string) int {
    19  	args = c.Meta.process(args, true)
    20  	cmdFlags := c.Meta.flagSet("debug json2dot")
    21  
    22  	if err := cmdFlags.Parse(args); err != nil {
    23  		return cli.RunResultHelp
    24  	}
    25  
    26  	fileName := cmdFlags.Arg(0)
    27  	if fileName == "" {
    28  		return cli.RunResultHelp
    29  	}
    30  
    31  	f, err := os.Open(fileName)
    32  	if err != nil {
    33  		c.Ui.Error(fmt.Sprintf(errInvalidLog, err))
    34  		return cli.RunResultHelp
    35  	}
    36  
    37  	dot, err := dag.JSON2Dot(f)
    38  	if err != nil {
    39  		c.Ui.Error(fmt.Sprintf(errInvalidLog, err))
    40  		return cli.RunResultHelp
    41  	}
    42  
    43  	c.Ui.Output(string(dot))
    44  	return 0
    45  }
    46  
    47  func (c *DebugJSON2DotCommand) Help() string {
    48  	helpText := `
    49  Usage: terraform debug json2dot input.json
    50  
    51    Translate a graph debug file to dot format.
    52  
    53    This command takes a single json graph log file and converts it to a single
    54    dot graph written to stdout.
    55  `
    56  	return strings.TrimSpace(helpText)
    57  }
    58  
    59  func (c *DebugJSON2DotCommand) Synopsis() string {
    60  	return "Convert json graph log to dot"
    61  }
    62  
    63  const errInvalidLog = `Error parsing log file: %[1]s`