github.com/sinangedik/terraform@v0.3.5/command/pull.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/remote"
     9  )
    10  
    11  type PullCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *PullCommand) Run(args []string) int {
    16  	args = c.Meta.process(args, false)
    17  	cmdFlags := flag.NewFlagSet("pull", flag.ContinueOnError)
    18  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    19  	if err := cmdFlags.Parse(args); err != nil {
    20  		return 1
    21  	}
    22  
    23  	// Recover the local state if any
    24  	local, _, err := remote.ReadLocalState()
    25  	if err != nil {
    26  		c.Ui.Error(fmt.Sprintf("%s", err))
    27  		return 1
    28  	}
    29  	if local == nil || local.Remote == nil {
    30  		c.Ui.Error("Remote state not enabled!")
    31  		return 1
    32  	}
    33  
    34  	// Attempt the state refresh
    35  	change, err := remote.RefreshState(local.Remote)
    36  	if err != nil {
    37  		c.Ui.Error(fmt.Sprintf(
    38  			"Failed to refresh from remote state: %v", err))
    39  		return 1
    40  	}
    41  
    42  	// Use an error exit code if the update was not a success
    43  	if !change.SuccessfulPull() {
    44  		c.Ui.Error(fmt.Sprintf("%s", change))
    45  		return 1
    46  	} else {
    47  		c.Ui.Output(fmt.Sprintf("%s", change))
    48  	}
    49  	return 0
    50  }
    51  
    52  func (c *PullCommand) Help() string {
    53  	helpText := `
    54  Usage: terraform pull [options]
    55  
    56    Refreshes the cached state file from the remote server.
    57  
    58  `
    59  	return strings.TrimSpace(helpText)
    60  }
    61  
    62  func (c *PullCommand) Synopsis() string {
    63  	return "Refreshes the local state copy from the remote server"
    64  }