Skip to content

Commit c946233

Browse files
committed
add comment
1 parent 9ba1d4e commit c946233

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

internal/hcl/hcl.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/hashicorp/hcl/v2"
7+
"github.com/hashicorp/hcl/v2/hclsyntax"
78
"github.com/hashicorp/hcl/v2/hclwrite"
89
)
910

@@ -16,17 +17,21 @@ func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
1617
if err != nil {
1718
return nil, err
1819
}
19-
body := parser.Body()
20-
for _, resource := range body.Blocks() {
20+
for _, resource := range parser.Body().Blocks() {
2121
isResource := resource.Type() == "resource"
2222
labels := resource.Labels()
2323
resourceName := labels[0]
2424
if !isResource || resourceName != "mongodbatlas_cluster" {
2525
continue
2626
}
27+
resourceBody := resource.Body()
28+
2729
// TODO: Do the full transformation
2830
labels[0] = "mongodbatlas_advanced_cluster"
2931
resource.SetLabels(labels)
32+
resourceBody.AppendNewline()
33+
appendComment(resourceBody, "Generated by atlas-cli-plugin-terraform.")
34+
appendComment(resourceBody, "Please confirm that all references to this resource are updated.")
3035
}
3136
return parser.Bytes(), nil
3237
}
@@ -38,3 +43,13 @@ func getParser(config []byte) (*hclwrite.File, error) {
3843
}
3944
return parser, nil
4045
}
46+
47+
func appendComment(body *hclwrite.Body, comment string) {
48+
tokens := hclwrite.Tokens{
49+
&hclwrite.Token{
50+
Type: hclsyntax.TokenComment,
51+
Bytes: []byte("# " + comment + "\n"),
52+
},
53+
}
54+
body.AppendUnstructuredTokens(tokens)
55+
}

internal/hcl/testdata/clu2adv/basic.out.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ resource "resource1" "res1" {
44

55
resource "mongodbatlas_advanced_cluster" "cluster1" {
66
name = "name2"
7+
8+
# Generated by atlas-cli-plugin-terraform.
9+
# Please confirm that all references to this resource are updated.
710
}
811

912
data "resource2" "res2" {

0 commit comments

Comments
 (0)