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)
}
+39 -8
View File
@@ -16,6 +16,8 @@
package main
import (
"errors"
"fmt"
"os"
"strings"
@@ -23,37 +25,66 @@ import (
"resource-generator/internal/writers"
)
type serviceWriteErr struct {
kind string
name string
err error
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}
func run() error {
resourcesDir := strings.TrimSpace(os.Getenv("NUBES_RESOURCES_DIR"))
if resourcesDir == "" {
panic("NUBES_RESOURCES_DIR is required — must point to generated/{stand}/resources_yaml/")
return fmt.Errorf("NUBES_RESOURCES_DIR is required — must point to generated/{stand}/resources_yaml/")
}
outDir := strings.TrimSpace(os.Getenv("NUBES_RESOURCES_GEN_DIR"))
if outDir == "" {
panic("NUBES_RESOURCES_GEN_DIR is required — must point to generated/{stand}/go/")
return fmt.Errorf("NUBES_RESOURCES_GEN_DIR is required — must point to generated/{stand}/go/")
}
instanceResources, subresources, actions, err := loader.LoadSpecs(resourcesDir)
if err != nil {
panic(err)
return err
}
var writeErrs []serviceWriteErr
for _, svc := range instanceResources {
if err := writers.WriteInstanceResource(outDir, svc); err != nil {
panic(err)
writeErrs = append(writeErrs, serviceWriteErr{kind: "instance", name: svc.Name, err: err})
}
}
for _, sr := range subresources {
if err := writers.WriteSubresource(outDir, sr); err != nil {
panic(err)
name := fmt.Sprintf("%s_%s", sr.ServiceName, sr.SubName)
writeErrs = append(writeErrs, serviceWriteErr{kind: "subresource", name: name, err: err})
}
}
for _, act := range actions {
if err := writers.WriteActionResource(outDir, act); err != nil {
panic(err)
name := fmt.Sprintf("%s_%s", act.ServiceName, act.ActionName)
writeErrs = append(writeErrs, serviceWriteErr{kind: "action", name: name, err: err})
}
}
if err := writers.WriteRegistry(outDir, instanceResources, subresources, actions); err != nil {
panic(err)
if len(writeErrs) > 0 {
var b strings.Builder
b.WriteString("generation failed for one or more resources:\n")
for _, we := range writeErrs {
b.WriteString(fmt.Sprintf("- %s %s: %v\n", we.kind, we.name, we.err))
}
return errors.New(strings.TrimSpace(b.String()))
}
if err := writers.WriteRegistry(outDir, instanceResources, subresources, actions); err != nil {
return err
}
return nil
}