github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/command/remote_pull.go (about) 1 package command 2 3 import ( 4 "flag" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/terraform/state" 9 ) 10 11 type RemotePullCommand struct { 12 Meta 13 } 14 15 func (c *RemotePullCommand) 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 // Read out our state 24 s, err := c.State() 25 if err != nil { 26 c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err)) 27 return 1 28 } 29 localState := s.State() 30 31 // If remote state isn't enabled, it is a problem. 32 if !localState.IsRemote() { 33 c.Ui.Error("Remote state not enabled!") 34 return 1 35 } 36 37 // We need the CacheState structure in order to do anything 38 var cache *state.CacheState 39 if bs, ok := s.(*state.BackupState); ok { 40 if cs, ok := bs.Real.(*state.CacheState); ok { 41 cache = cs 42 } 43 } 44 if cache == nil { 45 c.Ui.Error(fmt.Sprintf( 46 "Failed to extract internal CacheState from remote state.\n" + 47 "This is an internal error, please report it as a bug.")) 48 return 1 49 } 50 51 // Refresh the state 52 if err := cache.RefreshState(); err != nil { 53 c.Ui.Error(fmt.Sprintf( 54 "Failed to refresh from remote state: %s", err)) 55 return 1 56 } 57 58 // Use an error exit code if the update was not a success 59 change := cache.RefreshResult() 60 if !change.SuccessfulPull() { 61 c.Ui.Error(fmt.Sprintf("%s", change)) 62 return 1 63 } else { 64 c.Ui.Output(c.Colorize().Color(fmt.Sprintf( 65 "[reset][bold][green]%s", change))) 66 } 67 68 return 0 69 } 70 71 func (c *RemotePullCommand) Help() string { 72 helpText := ` 73 Usage: terraform pull [options] 74 75 Refreshes the cached state file from the remote server. 76 77 ` 78 return strings.TrimSpace(helpText) 79 } 80 81 func (c *RemotePullCommand) Synopsis() string { 82 return "Refreshes the local state copy from the remote server" 83 }