github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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, err := c.Meta.process(args, true)
    20  	if err != nil {
    21  		return 1
    22  	}
    23  	cmdFlags := c.Meta.flagSet("debug json2dot")
    24  
    25  	if err := cmdFlags.Parse(args); err != nil {
    26  		return cli.RunResultHelp
    27  	}
    28  
    29  	fileName := cmdFlags.Arg(0)
    30  	if fileName == "" {
    31  		return cli.RunResultHelp
    32  	}
    33  
    34  	f, err := os.Open(fileName)
    35  	if err != nil {
    36  		c.Ui.Error(fmt.Sprintf(errInvalidLog, err))
    37  		return cli.RunResultHelp
    38  	}
    39  
    40  	dot, err := dag.JSON2Dot(f)
    41  	if err != nil {
    42  		c.Ui.Error(fmt.Sprintf(errInvalidLog, err))
    43  		return cli.RunResultHelp
    44  	}
    45  
    46  	c.Ui.Output(string(dot))
    47  	return 0
    48  }
    49  
    50  func (c *DebugJSON2DotCommand) Help() string {
    51  	helpText := `
    52  Usage: terraform debug json2dot input.json
    53  
    54    Translate a graph debug file to dot format.
    55  
    56    This command takes a single json graph log file and converts it to a single
    57    dot graph written to stdout.
    58  `
    59  	return strings.TrimSpace(helpText)
    60  }
    61  
    62  func (c *DebugJSON2DotCommand) Synopsis() string {
    63  	return "Convert json graph log to dot"
    64  }
    65  
    66  const errInvalidLog = `Error parsing log file: %[1]s`