github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/command/push.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 PushCommand struct { 12 Meta 13 } 14 15 func (c *PushCommand) Run(args []string) int { 16 var force bool 17 args = c.Meta.process(args, false) 18 cmdFlags := flag.NewFlagSet("push", flag.ContinueOnError) 19 cmdFlags.BoolVar(&force, "force", false, "") 20 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 21 if err := cmdFlags.Parse(args); err != nil { 22 return 1 23 } 24 25 // Check for a remote state file 26 local, _, err := remote.ReadLocalState() 27 if err != nil { 28 c.Ui.Error(fmt.Sprintf("%s", err)) 29 return 1 30 } 31 if local == nil || local.Remote == nil { 32 c.Ui.Error("Remote state not enabled!") 33 return 1 34 } 35 36 // Attempt to push the state 37 change, err := remote.PushState(local.Remote, force) 38 if err != nil { 39 c.Ui.Error(fmt.Sprintf("Failed to push state: %v", err)) 40 return 1 41 } 42 43 // Use an error exit code if the update was not a success 44 if !change.SuccessfulPush() { 45 c.Ui.Error(fmt.Sprintf("%s", change)) 46 return 1 47 } else { 48 c.Ui.Output(fmt.Sprintf("%s", change)) 49 } 50 return 0 51 } 52 53 func (c *PushCommand) Help() string { 54 helpText := ` 55 Usage: terraform push [options] 56 57 Uploads the latest state to the remote server. 58 59 Options: 60 61 -force Forces the upload of the local state, ignoring any 62 conflicts. This should be used carefully, as force pushing 63 can cause remote state information to be lost. 64 65 ` 66 return strings.TrimSpace(helpText) 67 } 68 69 func (c *PushCommand) Synopsis() string { 70 return "Uploads the the local state to the remote server" 71 }