Skip to content

Commit

Permalink
Retain comments at the beginning of the definition file
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Sep 16, 2024
1 parent 193b432 commit e1f0b41
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions gen/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package main

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -148,7 +149,18 @@ func main() {
outputPath := definitionsPath + yamlconfig.SnakeCase(resourceName) + ".yaml"

existingConfig := yamlconfig.YamlConfig{}
comments := ""
if yamlFile, err := os.ReadFile(outputPath); err == nil {
// retain comments at the beginning of the definition file
scanner := bufio.NewScanner(bytes.NewReader(yamlFile))
for scanner.Scan() {
line := scanner.Text()
if line[0] == '#' {
comments += line + "\n"
} else {
break
}
}
existingConfig = yamlconfig.YamlConfig{}
err = yaml.Unmarshal(yamlFile, &existingConfig)
if err != nil {
Expand All @@ -165,8 +177,8 @@ func main() {
if err != nil {
panic(err)
}

os.WriteFile(outputPath, yamlBytes.Bytes(), 0644)
writeBytes := []byte(comments + yamlBytes.String())
os.WriteFile(outputPath, writeBytes, 0644)
}

func toStringSlice(i []interface{}) []string {
Expand Down

0 comments on commit e1f0b41

Please sign in to comment.