Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wellsiau-aws committed May 31, 2024
1 parent d83cd54 commit f2d299a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 113 deletions.
50 changes: 25 additions & 25 deletions internal/provider/generators/schema_rename/main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package main

import (
"fmt"
"path/filepath"
"fmt"
"os"
"strings"

"github.com/hashicorp/terraform-provider-awscc/internal/provider/generators/schema_rename/rename"
"path/filepath"
"strings"

"github.com/hashicorp/terraform-provider-awscc/internal/provider/generators/schema_rename/rename"
)

func main() {
baseDir := "./internal/service/cloudformation/schemas/"
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
baseDir := "./internal/service/cloudformation/schemas/"
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() && filepath.Ext(path) == ".json" {
generator := rename.NewGenerator()
// Skip files that start with "AWS_CloudFormation_"
if !strings.HasPrefix(info.Name(), "AWS_CloudFormation_") {
err = generator.RenameCfnSchemaFile(path)
if err != nil {
return err
}
}
}
if !info.IsDir() && filepath.Ext(path) == ".json" {
generator := rename.NewGenerator()
// Skip files that start with "AWS_CloudFormation_"
if !strings.HasPrefix(info.Name(), "AWS_CloudFormation_") {
err = generator.RenameCfnSchemaFile(path)
if err != nil {
return err
}
}
}

return nil
})
return nil
})

if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
}
}
176 changes: 88 additions & 88 deletions internal/provider/generators/schema_rename/rename/rename.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package rename

import (
"encoding/json"
"io"
"os"
"strings"
"reflect"
"github.com/hashicorp/terraform-provider-awscc/internal/provider/generators/common"
"encoding/json"
"io"
"os"
"reflect"
"strings"

"github.com/hashicorp/terraform-provider-awscc/internal/provider/generators/common"
)

type Generator struct {
Expand All @@ -22,95 +22,95 @@ func NewGenerator() *Generator {

const filePermMode = 0644 // Read/write for owner, read for group/others

func (g *Generator) RenameCfnSchemaFile (filePath string) error {
// Open JSON schema file from base directory
file, err := os.Open(filePath)
if err != nil {
g.Errorf("Error opening file: %v", err)
return err
}
defer file.Close()
func (g *Generator) RenameCfnSchemaFile(filePath string) error {
// Open JSON schema file from base directory
file, err := os.Open(filePath)
if err != nil {
g.Errorf("Error opening file: %v", err)
return err
}
defer file.Close()

// Read the JSON data
data, err := io.ReadAll(file)
if err != nil {
g.Errorf("Error reading file: %v", err)
return err
}
// Read the JSON data
data, err := io.ReadAll(file)
if err != nil {
g.Errorf("Error reading file: %v", err)
return err
}

// Unmarshal the JSON data into a map while preserving the order
var jsonData map[string]interface{}
err = json.Unmarshal(data, &jsonData)
if err != nil {
g.Errorf("Error unmarshaling JSON: %v", err)
return err
}
// Unmarshal the JSON data into a map while preserving the order
var jsonData map[string]interface{}
err = json.Unmarshal(data, &jsonData)
if err != nil {
g.Errorf("Error unmarshaling JSON: %v", err)
return err
}

// Create a copy of the original JSON data
originalData := make(map[string]interface{})
for k, v := range jsonData {
originalData[k] = v
}
// Create a copy of the original JSON data
originalData := make(map[string]interface{})
for k, v := range jsonData {
originalData[k] = v
}

// Replace "CloudFormation" with "Terraform" in the description
err = updateDescription(jsonData)
if err != nil {
g.Errorf("Error updating description: %v", err)
return err
}
// Replace "CloudFormation" with "Terraform" in the description
err = updateDescription(jsonData)
if err != nil {
g.Errorf("Error updating description: %v", err)
return err
}

// Check if the JSON data has changed
if reflect.DeepEqual(jsonData, originalData) {
// No changes detected, skip writing the file
return nil
}
// Check if the JSON data has changed
if reflect.DeepEqual(jsonData, originalData) {
// No changes detected, skip writing the file
return nil
}

// Marshal the updated JSON data while preserving the order
updatedDataBytes, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
g.Errorf("Error marshaling JSON: %v", err)
return err
}
// Marshal the updated JSON data while preserving the order
updatedDataBytes, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
g.Errorf("Error marshaling JSON: %v", err)
return err
}

// Write the updated JSON data back to the file
err = os.WriteFile(filePath, updatedDataBytes, filePermMode)
if err != nil {
g.Errorf("Error writing file: %v", err)
return err
}
// Write the updated JSON data back to the file
err = os.WriteFile(filePath, updatedDataBytes, filePermMode)
if err != nil {
g.Errorf("Error writing file: %v", err)
return err
}

g.Infof("File %s updated successfully\n", filePath)
return nil
g.Infof("File %s updated successfully\n", filePath)
return nil
}

func updateDescription(data map[string]interface{}) error {
for key, value := range data {
if key == "description" {
description, ok := value.(string)
if ok {
updatedDescription := strings.ReplaceAll(description, "AWS CloudFormation", "Terraform")
data[key] = updatedDescription
}
} else {
switch v := value.(type) {
case map[string]interface{}:
err := updateDescription(v)
if err != nil {
return err
}
case []interface{}:
for i, item := range v {
switch item := item.(type) {
case map[string]interface{}:
err := updateDescription(item)
if err != nil {
return err
}
v[i] = item
}
}
}
}
}
return nil
}
for key, value := range data {
if key == "description" {
description, ok := value.(string)
if ok {
updatedDescription := strings.ReplaceAll(description, "AWS CloudFormation", "Terraform")
data[key] = updatedDescription
}
} else {
switch v := value.(type) {
case map[string]interface{}:
err := updateDescription(v)
if err != nil {
return err
}
case []interface{}:
for i, item := range v {
switch item := item.(type) {
case map[string]interface{}:
err := updateDescription(item)
if err != nil {
return err
}
v[i] = item
}
}
}
}
}
return nil
}

0 comments on commit f2d299a

Please sign in to comment.