github.com/jdextraze/terraform@v0.6.17-0.20160511153921-e33847c8a8af/command/state_show.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    10  	"github.com/ryanuber/columnize"
    11  )
    12  
    13  // StateShowCommand is a Command implementation that shows a single resource.
    14  type StateShowCommand struct {
    15  	Meta
    16  	StateMeta
    17  }
    18  
    19  func (c *StateShowCommand) Run(args []string) int {
    20  	args = c.Meta.process(args, true)
    21  
    22  	cmdFlags := c.Meta.flagSet("state show")
    23  	cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
    24  	if err := cmdFlags.Parse(args); err != nil {
    25  		return cli.RunResultHelp
    26  	}
    27  	args = cmdFlags.Args()
    28  
    29  	state, err := c.State()
    30  	if err != nil {
    31  		c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
    32  		return cli.RunResultHelp
    33  	}
    34  
    35  	stateReal := state.State()
    36  	if stateReal == nil {
    37  		c.Ui.Error(fmt.Sprintf(errStateNotFound))
    38  		return 1
    39  	}
    40  
    41  	filter := &terraform.StateFilter{State: stateReal}
    42  	results, err := filter.Filter(args...)
    43  	if err != nil {
    44  		c.Ui.Error(fmt.Sprintf(errStateFilter, err))
    45  		return 1
    46  	}
    47  
    48  	instance, err := c.filterInstance(results)
    49  	if err != nil {
    50  		c.Ui.Error(err.Error())
    51  		return 1
    52  	}
    53  	is := instance.Value.(*terraform.InstanceState)
    54  
    55  	// Sort the keys
    56  	keys := make([]string, 0, len(is.Attributes))
    57  	for k, _ := range is.Attributes {
    58  		keys = append(keys, k)
    59  	}
    60  	sort.Strings(keys)
    61  
    62  	// Build the output
    63  	output := make([]string, 0, len(is.Attributes)+1)
    64  	output = append(output, fmt.Sprintf("id | %s", is.ID))
    65  	for _, k := range keys {
    66  		if k != "id" {
    67  			output = append(output, fmt.Sprintf("%s | %s", k, is.Attributes[k]))
    68  		}
    69  	}
    70  
    71  	// Output
    72  	config := columnize.DefaultConfig()
    73  	config.Glue = " = "
    74  	c.Ui.Output(columnize.Format(output, config))
    75  	return 0
    76  }
    77  
    78  func (c *StateShowCommand) Help() string {
    79  	helpText := `
    80  Usage: terraform state show [options] ADDRESS
    81  
    82    Shows the attributes of a resource in the Terraform state.
    83  
    84    This command shows the attributes of a single resource in the Terraform
    85    state. The address argument must be used to specify a single resource.
    86    You can view the list of available resources with "terraform state list".
    87  
    88  Options:
    89  
    90    -state=statefile    Path to a Terraform state file to use to look
    91                        up Terraform-managed resources. By default it will
    92                        use the state "terraform.tfstate" if it exists.
    93  
    94  `
    95  	return strings.TrimSpace(helpText)
    96  }
    97  
    98  func (c *StateShowCommand) Synopsis() string {
    99  	return "Show a resource in the state"
   100  }