github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/command/state_pull.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  // StatePullCommand is a Command implementation that shows a single resource.
    13  type StatePullCommand struct {
    14  	Meta
    15  	StateMeta
    16  }
    17  
    18  func (c *StatePullCommand) Run(args []string) int {
    19  	args = c.Meta.process(args, true)
    20  
    21  	cmdFlags := c.Meta.flagSet("state pull")
    22  	if err := cmdFlags.Parse(args); err != nil {
    23  		return cli.RunResultHelp
    24  	}
    25  	args = cmdFlags.Args()
    26  
    27  	// Load the backend
    28  	b, err := c.Backend(nil)
    29  	if err != nil {
    30  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    31  		return 1
    32  	}
    33  
    34  	// Get the state
    35  	state, err := b.State()
    36  	if err != nil {
    37  		c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
    38  		return 1
    39  	}
    40  	if err := state.RefreshState(); err != nil {
    41  		c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
    42  		return 1
    43  	}
    44  
    45  	var buf bytes.Buffer
    46  	if err := terraform.WriteState(state.State(), &buf); err != nil {
    47  		c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
    48  		return 1
    49  	}
    50  
    51  	c.Ui.Output(buf.String())
    52  	return 0
    53  }
    54  
    55  func (c *StatePullCommand) Help() string {
    56  	helpText := `
    57  Usage: terraform state pull [options]
    58  
    59    Pull the state from its location and output it to stdout.
    60  
    61    This command "pulls" the current state and outputs it to stdout.
    62    The primary use of this is for state stored remotely. This command
    63    will still work with local state but is less useful for this.
    64  
    65  `
    66  	return strings.TrimSpace(helpText)
    67  }
    68  
    69  func (c *StatePullCommand) Synopsis() string {
    70  	return "Pull current state and output to stdout"
    71  }