github.com/opentofu/opentofu@v1.7.1/internal/command/state_pull.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package command 7 8 import ( 9 "bytes" 10 "fmt" 11 "strings" 12 13 "github.com/opentofu/opentofu/internal/encryption" 14 "github.com/opentofu/opentofu/internal/states/statefile" 15 "github.com/opentofu/opentofu/internal/states/statemgr" 16 ) 17 18 // StatePullCommand is a Command implementation that shows a single resource. 19 type StatePullCommand struct { 20 Meta 21 StateMeta 22 } 23 24 func (c *StatePullCommand) Run(args []string) int { 25 args = c.Meta.process(args) 26 cmdFlags := c.Meta.defaultFlagSet("state pull") 27 if err := cmdFlags.Parse(args); err != nil { 28 c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) 29 return 1 30 } 31 32 if diags := c.Meta.checkRequiredVersion(); diags != nil { 33 c.showDiagnostics(diags) 34 return 1 35 } 36 37 // Load the encryption configuration 38 enc, encDiags := c.Encryption() 39 if encDiags.HasErrors() { 40 c.showDiagnostics(encDiags) 41 return 1 42 } 43 44 // Load the backend 45 b, backendDiags := c.Backend(nil, enc.State()) 46 if backendDiags.HasErrors() { 47 c.showDiagnostics(backendDiags) 48 return 1 49 } 50 51 // This is a read-only command 52 c.ignoreRemoteVersionConflict(b) 53 54 // Get the state manager for the current workspace 55 env, err := c.Workspace() 56 if err != nil { 57 c.Ui.Error(fmt.Sprintf("Error selecting workspace: %s", err)) 58 return 1 59 } 60 stateMgr, err := b.StateMgr(env) 61 if err != nil { 62 c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) 63 return 1 64 } 65 if err := stateMgr.RefreshState(); err != nil { 66 c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err)) 67 return 1 68 } 69 70 // Get a statefile object representing the latest snapshot 71 stateFile := statemgr.Export(stateMgr) 72 73 if stateFile != nil { // we produce no output if the statefile is nil 74 var buf bytes.Buffer 75 err = statefile.Write(stateFile, &buf, encryption.StateEncryptionDisabled()) // Don't encrypt to stdout 76 if err != nil { 77 c.Ui.Error(fmt.Sprintf("Failed to write state: %s", err)) 78 return 1 79 } 80 81 c.Ui.Output(buf.String()) 82 } 83 84 return 0 85 } 86 87 func (c *StatePullCommand) Help() string { 88 helpText := ` 89 Usage: tofu [global options] state pull [options] 90 91 Pull the state from its location, upgrade the local copy, and output it 92 to stdout. 93 94 This command "pulls" the current state and outputs it to stdout. 95 As part of this process, OpenTofu will upgrade the state format of the 96 local copy to the current version. 97 98 The primary use of this is for state stored remotely. This command 99 will still work with local state but is less useful for this. 100 101 ` 102 return strings.TrimSpace(helpText) 103 } 104 105 func (c *StatePullCommand) Synopsis() string { 106 return "Pull current state and output to stdout" 107 }