feat: refactor builder for datasource
This commit is contained in:
parent
b1b359f436
commit
f44659f703
24 changed files with 151 additions and 1426 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package build
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
|
@ -509,7 +510,7 @@ func generateServiceFiles(rootDir, generatorDir string) error {
|
|||
|
||||
oasFile := path.Join(generatorDir, "oas", fmt.Sprintf("%s%s.json", service.Name(), svcVersion.Name()))
|
||||
if _, oasErr := os.Stat(oasFile); os.IsNotExist(oasErr) {
|
||||
slog.Warn(" coulc not find matching oas", "svc", service.Name(), "version", svcVersion.Name())
|
||||
slog.Warn(" could not find matching oas", "svc", service.Name(), "version", svcVersion.Name())
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -648,6 +649,15 @@ func generateServiceFiles(rootDir, generatorDir string) error {
|
|||
return err
|
||||
}
|
||||
}
|
||||
|
||||
tfAnoErr := handleTfTagForDatasourceFile(
|
||||
path.Join(tgtFolder, fmt.Sprintf("%s_data_source_gen.go", resource)),
|
||||
scName,
|
||||
resource,
|
||||
)
|
||||
if tfAnoErr != nil {
|
||||
return tfAnoErr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -655,6 +665,70 @@ func generateServiceFiles(rootDir, generatorDir string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// handleTfTagForDatasourceFile replaces existing "id" with "stf_original_api_id"
|
||||
func handleTfTagForDatasourceFile(filePath, service, resource string) error {
|
||||
slog.Info(" handle terraform tag for datasource", "service", service, "resource", resource)
|
||||
if !fileExists(filePath) {
|
||||
slog.Warn(" could not find file, skipping", "path", filePath)
|
||||
return nil
|
||||
}
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
tmp, err := os.CreateTemp("", "replace-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tmp.Close()
|
||||
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
resLine, err := handleLine(sc.Text())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.WriteString(tmp, resLine+"\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if scErr := sc.Err(); scErr != nil {
|
||||
return scErr
|
||||
}
|
||||
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Rename(tmp.Name(), filePath); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleLine(line string) (string, error) {
|
||||
schemaRegex := regexp.MustCompile(`(\s+")(id)(": schema.[a-zA-Z0-9]+Attribute{)`)
|
||||
|
||||
schemaMatches := schemaRegex.FindAllStringSubmatch(line, -1)
|
||||
if schemaMatches != nil {
|
||||
return fmt.Sprintf("%stf_original_api_id%s", schemaMatches[0][1], schemaMatches[0][3]), nil
|
||||
}
|
||||
|
||||
modelRegex := regexp.MustCompile(`(\s+Id\s+types.[a-zA-Z0-9]+\s+.tfsdk:")(id)(".)`)
|
||||
modelMatches := modelRegex.FindAllStringSubmatch(line, -1)
|
||||
if modelMatches != nil {
|
||||
return fmt.Sprintf("%stf_original_api_id%s", modelMatches[0][1], modelMatches[0][3]), nil
|
||||
}
|
||||
|
||||
return line, nil
|
||||
}
|
||||
|
||||
func checkCommands(commands []string) error {
|
||||
for _, commandName := range commands {
|
||||
if !commandExists(commandName) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue