terraform/command/remote_pull.go

87 lines
1.9 KiB
Go
Raw Normal View History

2014-10-08 21:08:35 +02:00
package command
import (
"flag"
"fmt"
"strings"
2015-02-22 20:02:19 +01:00
"github.com/hashicorp/terraform/state"
2014-10-08 21:08:35 +02:00
)
type RemotePullCommand struct {
2014-10-08 21:08:35 +02:00
Meta
}
func (c *RemotePullCommand) Run(args []string) int {
2014-10-08 21:08:35 +02:00
args = c.Meta.process(args, false)
cmdFlags := flag.NewFlagSet("pull", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
2015-02-22 20:02:19 +01:00
// Read out our state
s, err := c.State()
if err != nil {
2015-02-22 20:02:19 +01:00
c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err))
return 1
}
2015-02-22 20:02:19 +01:00
localState := s.State()
// If remote state isn't enabled, it is a problem.
if !localState.IsRemote() {
c.Ui.Error("Remote state not enabled!")
return 1
2014-10-08 21:08:35 +02:00
}
2015-02-22 20:02:19 +01:00
// We need the CacheState structure in order to do anything
var cache *state.CacheState
if bs, ok := s.(*state.BackupState); ok {
if cs, ok := bs.Real.(*state.CacheState); ok {
cache = cs
}
}
if cache == nil {
c.Ui.Error(fmt.Sprintf(
"Failed to extract internal CacheState from remote state.\n" +
"This is an internal error, please report it as a bug."))
return 1
}
// Refresh the state
if err := cache.RefreshState(); err != nil {
2014-10-08 21:08:35 +02:00
c.Ui.Error(fmt.Sprintf(
2015-02-22 20:02:19 +01:00
"Failed to refresh from remote state: %s", err))
2014-10-08 21:08:35 +02:00
return 1
}
// Use an error exit code if the update was not a success
2015-02-22 20:02:19 +01:00
change := cache.RefreshResult()
2014-10-08 21:08:35 +02:00
if !change.SuccessfulPull() {
c.Ui.Error(fmt.Sprintf("%s", change))
return 1
} else {
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"[reset][bold][green]%s", change)))
2014-10-08 21:08:35 +02:00
}
2015-02-22 20:02:19 +01:00
2014-10-08 21:08:35 +02:00
return 0
}
func (c *RemotePullCommand) Help() string {
2014-10-08 21:08:35 +02:00
helpText := `
Usage: terraform pull [options]
Refreshes the cached state file from the remote server.
2014-10-08 21:08:35 +02:00
2015-06-22 14:14:01 +02:00
Options:
-no-color If specified, output won't contain any color.
2014-10-08 21:08:35 +02:00
`
return strings.TrimSpace(helpText)
}
func (c *RemotePullCommand) Synopsis() string {
2014-10-08 21:08:35 +02:00
return "Refreshes the local state copy from the remote server"
}