Fix provider review findings

This commit is contained in:
“Naeel”
2026-08-31 20:19:31 +03:00
parent 75c868e0b5
commit 86871498a2
13 changed files with 327 additions and 141 deletions
@@ -4,7 +4,6 @@ package helpers
import (
"fmt"
"go/format"
"os"
"strings"
"unicode"
@@ -168,14 +167,13 @@ func EnsureGoIdent(s string) string {
return "R" + s
}
// FormatSourceOrWarn форматирует Go-код. При ошибке пишет warning в stderr.
func FormatSourceOrWarn(filePath string, src []byte) []byte {
// FormatSourceOrWarn форматирует Go-код и возвращает ошибку при сбое gofmt.
func FormatSourceOrWarn(filePath string, src []byte) ([]byte, error) {
formatted, err := format.Source(src)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: gofmt failed for %s: %v — writing unformatted code\n", filePath, err)
return src
return nil, fmt.Errorf("gofmt failed for %s: %w", filePath, err)
}
return formatted
return formatted, nil
}
// PlanModifierType возвращает суффикс типа план-модификатора (Bool/Int64/String).
@@ -24,7 +24,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Code generated by tools/gen_v2. DO NOT EDIT.
// Code generated by TOOLS/resource-generator. DO NOT EDIT.
// Service: {{.ServiceName}}
// Action: {{.ActionName}}
@@ -29,7 +29,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Code generated by tools/gen_v2. DO NOT EDIT.
// Code generated by TOOLS/resource-generator. DO NOT EDIT.
// Service: {{.Name}}
// Service ID: {{.ServiceID}}
@@ -29,7 +29,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Code generated by tools/gen_v2. DO NOT EDIT.
// Code generated by TOOLS/resource-generator. DO NOT EDIT.
// Service: {{.ServiceName}}
// Subresource: {{.SubName}}
@@ -439,4 +439,3 @@ func (r *{{ToCamel (printf "%s_%s" .ServiceName .SubName)}}Resource) Configure(_
r.client = client
}
`
@@ -59,7 +59,10 @@ func WriteInstanceResource(outDir string, svc types.GenResource) error {
return err
}
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
formatted, err := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
if err != nil {
return err
}
return os.WriteFile(filePath, formatted, 0644)
}
@@ -91,7 +94,10 @@ func WriteSubresource(outDir string, sr types.GenSubresource) error {
return err
}
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
formatted, err := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
if err != nil {
return err
}
return os.WriteFile(filePath, formatted, 0644)
}
@@ -121,7 +127,10 @@ func WriteActionResource(outDir string, act types.GenAction) error {
return err
}
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
formatted, err := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
if err != nil {
return err
}
return os.WriteFile(filePath, formatted, 0644)
}
@@ -131,7 +140,7 @@ func WriteRegistry(outDir string, services []types.GenResource, subs []types.Gen
var buf bytes.Buffer
buf.WriteString("package resources_gen\n\n")
buf.WriteString("import \"github.com/hashicorp/terraform-plugin-framework/resource\"\n\n")
buf.WriteString("// Code generated by tools/gen_v2. DO NOT EDIT.\n")
buf.WriteString("// Code generated by TOOLS/resource-generator. DO NOT EDIT.\n")
buf.WriteString("func AllResources() []func() resource.Resource {\n")
buf.WriteString("\treturn []func() resource.Resource{\n")
for _, svc := range services {
@@ -147,7 +156,10 @@ func WriteRegistry(outDir string, services []types.GenResource, subs []types.Gen
buf.WriteString("}\n")
regPath := filepath.Join(outDir, "registry.go")
formatted := helpers.FormatSourceOrWarn(regPath, buf.Bytes())
formatted, err := helpers.FormatSourceOrWarn(regPath, buf.Bytes())
if err != nil {
return err
}
return os.WriteFile(regPath, formatted, 0644)
}