Skip to content

Commit

Permalink
Merge pull request #51 from Pix4D/pci-3008-allow-multi-word-address
Browse files Browse the repository at this point in the history
import: use quote formatting verb (%q)
  • Loading branch information
aliculPix4D authored Feb 21, 2023
2 parents 5d529f9 + 6cd2898 commit 7cd408b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
11 changes: 7 additions & 4 deletions cmdimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ type Definitions struct {

// Keep track of the asymmetry of import subcommand.
// When importing, the up direction wants two parameters:
// terraform import res-address res-id
//
// terraform import res-address res-id
//
// while the down direction wants only one parameter:
// terraform state rm res-address
//
// terraform state rm res-address
type ImportElement struct {
Addr string
ID string
Expand Down Expand Up @@ -178,7 +181,7 @@ func importUpScript(elements []ImportElement, out io.Writer) error {
cmd := "terraform import"
fmt.Fprintf(out, importScriptHeader, cmd, len(elements))
for _, elem := range elements {
fmt.Fprintf(out, "%s \\\n '%s' %s\n\n", cmd, elem.Addr, elem.ID)
fmt.Fprintf(out, "%s \\\n %q %q\n\n", cmd, elem.Addr, elem.ID)
}
return nil
}
Expand All @@ -187,7 +190,7 @@ func importDownScript(elements []ImportElement, out io.Writer) error {
cmd := "terraform state rm"
fmt.Fprintf(out, importScriptHeader, cmd, len(elements))
for _, elem := range elements {
fmt.Fprintf(out, "%s \\\n '%s'\n\n", cmd, elem.Addr)
fmt.Fprintf(out, "%s \\\n %q\n\n", cmd, elem.Addr)
}
return nil
}
Expand Down
23 changes: 13 additions & 10 deletions testdata/import/08_import_down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,38 @@
# DO NOT EDIT. Generated by terravalet.
# WARNING: check the order of resources before running this script.
#
# This script will "terraform state rm" 9 items.
# This script will "terraform state rm" 10 items.

# Uncomment this if you want to stop the script at first error
# set -e

terraform state rm \
'module.github.github_branch_protection_v3.settings["test-import-bar:master"]'
"module.github.github_issue_label.all_labels[\"test-import-bar.multi word label\"]"

terraform state rm \
'module.github.github_branch_default.default["test-import-bar"]'
"module.github.github_branch_protection_v3.settings[\"test-import-bar:master\"]"

terraform state rm \
'module.github.github_branch_protection_v3.settings["test-import-foo:master"]'
"module.github.github_branch_default.default[\"test-import-bar\"]"

terraform state rm \
'module.github.github_branch_default.default["test-import-foo"]'
"module.github.github_branch_protection_v3.settings[\"test-import-foo:master\"]"

terraform state rm \
'module.github.github_branch_protection_v3.settings["test-import-gh:master"]'
"module.github.github_branch_default.default[\"test-import-foo\"]"

terraform state rm \
'module.github.github_branch_default.default["test-import-gh"]'
"module.github.github_branch_protection_v3.settings[\"test-import-gh:master\"]"

terraform state rm \
'module.github.github_repository.repos["test-import-gh"]'
"module.github.github_branch_default.default[\"test-import-gh\"]"

terraform state rm \
'module.github.github_repository.repos["test-import-foo"]'
"module.github.github_repository.repos[\"test-import-gh\"]"

terraform state rm \
'module.github.github_repository.repos["test-import-bar"]'
"module.github.github_repository.repos[\"test-import-foo\"]"

terraform state rm \
"module.github.github_repository.repos[\"test-import-bar\"]"

21 changes: 20 additions & 1 deletion testdata/import/08_import_src-plan.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@
"name": "test-import-bar"
}
}
}
},
{
"address":"module.github.github_issue_label.all_labels[\"test-import-bar.multi word label\"]",
"module_address":"module.github",
"mode":"managed",
"type":"github_issue_label",
"name":"all_labels",
"index":"test-import-bar.multi word label",
"provider_name":"registry.terraform.io/hashicorp/github",
"change":{
"actions":[
"create"
],
"before":null,
"after":{
"name":"multi word label",
"repository":"test-import-bar"
}
}
}
]
}
23 changes: 13 additions & 10 deletions testdata/import/08_import_up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,38 @@
# DO NOT EDIT. Generated by terravalet.
# WARNING: check the order of resources before running this script.
#
# This script will "terraform import" 9 items.
# This script will "terraform import" 10 items.

# Uncomment this if you want to stop the script at first error
# set -e

terraform import \
'module.github.github_repository.repos["test-import-bar"]' test-import-bar
"module.github.github_repository.repos[\"test-import-bar\"]" "test-import-bar"

terraform import \
'module.github.github_repository.repos["test-import-foo"]' test-import-foo
"module.github.github_repository.repos[\"test-import-foo\"]" "test-import-foo"

terraform import \
'module.github.github_repository.repos["test-import-gh"]' test-import-gh
"module.github.github_repository.repos[\"test-import-gh\"]" "test-import-gh"

terraform import \
'module.github.github_branch_default.default["test-import-gh"]' test-import-gh
"module.github.github_branch_default.default[\"test-import-gh\"]" "test-import-gh"

terraform import \
'module.github.github_branch_protection_v3.settings["test-import-gh:master"]' test-import-gh:master
"module.github.github_branch_protection_v3.settings[\"test-import-gh:master\"]" "test-import-gh:master"

terraform import \
'module.github.github_branch_default.default["test-import-foo"]' test-import-foo
"module.github.github_branch_default.default[\"test-import-foo\"]" "test-import-foo"

terraform import \
'module.github.github_branch_protection_v3.settings["test-import-foo:master"]' test-import-foo:master
"module.github.github_branch_protection_v3.settings[\"test-import-foo:master\"]" "test-import-foo:master"

terraform import \
'module.github.github_branch_default.default["test-import-bar"]' test-import-bar
"module.github.github_branch_default.default[\"test-import-bar\"]" "test-import-bar"

terraform import \
'module.github.github_branch_protection_v3.settings["test-import-bar:master"]' test-import-bar:master
"module.github.github_branch_protection_v3.settings[\"test-import-bar:master\"]" "test-import-bar:master"

terraform import \
"module.github.github_issue_label.all_labels[\"test-import-bar.multi word label\"]" "test-import-bar:multi word label"

0 comments on commit 7cd408b

Please sign in to comment.