github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/command/refresh.go (about) 1 package command 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/errwrap" 9 "github.com/hashicorp/terraform/backend" 10 "github.com/hashicorp/terraform/config" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 // RefreshCommand is a cli.Command implementation that refreshes the state 15 // file. 16 type RefreshCommand struct { 17 Meta 18 } 19 20 func (c *RefreshCommand) Run(args []string) int { 21 args, err := c.Meta.process(args, true) 22 if err != nil { 23 return 1 24 } 25 26 cmdFlags := c.Meta.flagSet("refresh") 27 cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") 28 cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism") 29 cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path") 30 cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path") 31 cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state") 32 cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout") 33 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 34 if err := cmdFlags.Parse(args); err != nil { 35 return 1 36 } 37 38 configPath, err := ModulePath(cmdFlags.Args()) 39 if err != nil { 40 c.Ui.Error(err.Error()) 41 return 1 42 } 43 44 // Load the module 45 mod, err := c.Module(configPath) 46 if err != nil { 47 err = errwrap.Wrapf("Failed to load root config module: {{err}}", err) 48 c.showDiagnostics(err) 49 return 1 50 } 51 52 // Check for user-supplied plugin path 53 if c.pluginPath, err = c.loadPluginPath(); err != nil { 54 c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err)) 55 return 1 56 } 57 58 var conf *config.Config 59 if mod != nil { 60 conf = mod.Config() 61 } 62 63 // Load the backend 64 b, err := c.Backend(&BackendOpts{ 65 Config: conf, 66 }) 67 if err != nil { 68 c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err)) 69 return 1 70 } 71 72 // Build the operation 73 opReq := c.Operation() 74 opReq.Type = backend.OperationTypeRefresh 75 opReq.Module = mod 76 77 // Perform the operation 78 op, err := b.Operation(context.Background(), opReq) 79 if err != nil { 80 c.Ui.Error(fmt.Sprintf("Error starting operation: %s", err)) 81 return 1 82 } 83 84 // Wait for the operation to complete 85 <-op.Done() 86 if err := op.Err; err != nil { 87 c.showDiagnostics(err) 88 return 1 89 } 90 91 // Output the outputs 92 if outputs := outputsAsString(op.State, terraform.RootModulePath, nil, true); outputs != "" { 93 c.Ui.Output(c.Colorize().Color(outputs)) 94 } 95 96 return 0 97 } 98 99 func (c *RefreshCommand) Help() string { 100 helpText := ` 101 Usage: terraform refresh [options] [dir] 102 103 Update the state file of your infrastructure with metadata that matches 104 the physical resources they are tracking. 105 106 This will not modify your infrastructure, but it can modify your 107 state file to update metadata. This metadata might cause new changes 108 to occur when you generate a plan or call apply next. 109 110 Options: 111 112 -backup=path Path to backup the existing state file before 113 modifying. Defaults to the "-state-out" path with 114 ".backup" extension. Set to "-" to disable backup. 115 116 -input=true Ask for input for variables if not directly set. 117 118 -lock=true Lock the state file when locking is supported. 119 120 -lock-timeout=0s Duration to retry a state lock. 121 122 -no-color If specified, output won't contain any color. 123 124 -state=path Path to read and save state (unless state-out 125 is specified). Defaults to "terraform.tfstate". 126 127 -state-out=path Path to write updated state file. By default, the 128 "-state" path will be used. 129 130 -target=resource Resource to target. Operation will be limited to this 131 resource and its dependencies. This flag can be used 132 multiple times. 133 134 -var 'foo=bar' Set a variable in the Terraform configuration. This 135 flag can be set multiple times. 136 137 -var-file=foo Set variables in the Terraform configuration from 138 a file. If "terraform.tfvars" or any ".auto.tfvars" 139 files are present, they will be automatically loaded. 140 141 ` 142 return strings.TrimSpace(helpText) 143 } 144 145 func (c *RefreshCommand) Synopsis() string { 146 return "Update local state file against real resources" 147 }