Skip to content

Commit

Permalink
Fix passing of extra arguments to terraform plan
Browse files Browse the repository at this point in the history
Fix #25
  • Loading branch information
mbode committed Dec 5, 2021
1 parent 2ec0630 commit 1b02a30
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
### Removed
### Fixed
- Fix passing of extra arguments to `terraform plan` call
### Security


Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ terraform-state-mover # inside a Terraform root directory

Extra arguments after a `--` are passed to the `terraform plan` call. This makes the following possible:
```bash
terraform-state-mover -- -var key=value # setting variables
terraform-state-mover -- -var-file=variables.tfvars # using variable files
terraform-state-mover -- -var key=value # set variables
terraform-state-mover -- -var-file=variables.tfvars # use variable files
terraform-state-mover -- -refresh=false # skip state refresh
terraform-state-mover -- -parallelism=50 # speed up plan by using more concurrent operations
```

*Hint:*
Expand Down
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ func main() {
}

func action(ctx *cli.Context) error {
var args []string
if len(os.Args) >= 2 && os.Args[1] == "--" {
args = os.Args[2:]
var planArgs []string
for i, elem := range os.Args {
if "--" == elem {
planArgs = os.Args[i+1:]
}
}
cfg := readConfig(ctx)

changes, err := changes(cfg, args)
changes, err := changes(cfg, planArgs)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"strings"
)

func changes(cfg config, args []string) ([]ResChange, error) {
func changes(cfg config, planArgs []string) ([]ResChange, error) {
tfPlan, err := ioutil.TempFile("", "tfplan")
if err != nil {
return nil, err
}
tfPlanName := tfPlan.Name()
defer os.Remove(tfPlanName)

if err := terraformExec(cfg, true, args, "plan", "-out="+tfPlanName); err != nil {
if err := terraformExec(cfg, true, planArgs, "plan", "-out="+tfPlanName); err != nil {
return nil, err
}

Expand Down

0 comments on commit 1b02a30

Please sign in to comment.