github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/command/state_pull.go (about) 1 package command 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 8 "github.com/muratcelep/terraform/not-internal/states/statefile" 9 "github.com/muratcelep/terraform/not-internal/states/statemgr" 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) 20 cmdFlags := c.Meta.defaultFlagSet("state pull") 21 if err := cmdFlags.Parse(args); err != nil { 22 c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) 23 return 1 24 } 25 26 // Load the backend 27 b, backendDiags := c.Backend(nil) 28 if backendDiags.HasErrors() { 29 c.showDiagnostics(backendDiags) 30 return 1 31 } 32 33 // This is a read-only command 34 c.ignoreRemoteVersionConflict(b) 35 36 // Get the state manager for the current workspace 37 env, err := c.Workspace() 38 if err != nil { 39 c.Ui.Error(fmt.Sprintf("Error selecting workspace: %s", err)) 40 return 1 41 } 42 stateMgr, err := b.StateMgr(env) 43 if err != nil { 44 c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) 45 return 1 46 } 47 if err := stateMgr.RefreshState(); err != nil { 48 c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err)) 49 return 1 50 } 51 52 // Get a statefile object representing the latest snapshot 53 stateFile := statemgr.Export(stateMgr) 54 55 if stateFile != nil { // we produce no output if the statefile is nil 56 var buf bytes.Buffer 57 err = statefile.Write(stateFile, &buf) 58 if err != nil { 59 c.Ui.Error(fmt.Sprintf("Failed to write state: %s", err)) 60 return 1 61 } 62 63 c.Ui.Output(buf.String()) 64 } 65 66 return 0 67 } 68 69 func (c *StatePullCommand) Help() string { 70 helpText := ` 71 Usage: terraform [global options] state pull [options] 72 73 Pull the state from its location, upgrade the local copy, and output it 74 to stdout. 75 76 This command "pulls" the current state and outputs it to stdout. 77 As part of this process, Terraform will upgrade the state format of the 78 local copy to the current version. 79 80 The primary use of this is for state stored remotely. This command 81 will still work with local state but is less useful for this. 82 83 ` 84 return strings.TrimSpace(helpText) 85 } 86 87 func (c *StatePullCommand) Synopsis() string { 88 return "Pull current state and output to stdout" 89 }