refactor(gen_v2): split into 7 Go packages (types, templates, helpers, params, loader, writers, main)

This commit is contained in:
“Naeel”
2026-07-05 09:13:07 +04:00
parent e8935f1ebf
commit f3616d0fe1
10 changed files with 542 additions and 2789 deletions
+8 -40
View File
@@ -16,10 +16,12 @@
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"terraform-provider-nubes/tools/gen_v2/loader"
"terraform-provider-nubes/tools/gen_v2/writers"
)
func main() {
@@ -36,61 +38,27 @@ func main() {
outDir = filepath.Join(root, "internal", "resources_gen")
}
instanceResources, subresources, actions, err := loadSpecs(resourcesDir)
instanceResources, subresources, actions, err := loader.LoadSpecs(resourcesDir)
if err != nil {
panic(err)
}
for _, svc := range instanceResources {
if err := writeInstanceResource(outDir, svc); err != nil {
if err := writers.WriteInstanceResource(outDir, svc); err != nil {
panic(err)
}
}
for _, sr := range subresources {
if err := writeSubresource(outDir, sr); err != nil {
if err := writers.WriteSubresource(outDir, sr); err != nil {
panic(err)
}
}
for _, act := range actions {
if err := writeActionResource(outDir, act); err != nil {
if err := writers.WriteActionResource(outDir, act); err != nil {
panic(err)
}
}
if err := writeRegistry(outDir, instanceResources, subresources, actions); err != nil {
if err := writers.WriteRegistry(outDir, instanceResources, subresources, actions); err != nil {
panic(err)
}
}
// knownKinds — допустимые значения Kind операций.
var knownKinds = map[string]bool{
"instance": true,
"subresource": true,
"action": true,
}
// validateSpec проверяет YAML-спек на обязательные поля и неизвестные kinds.
// P1.4: fail-fast — паника при неизвестном kind вместо тихого игнорирования.
func validateSpec(path string, spec *ServiceSpec) error {
if spec.Name == "" {
return fmt.Errorf("missing required field: name")
}
if spec.ServiceID <= 0 {
return fmt.Errorf("missing required field: service_id")
}
for i, op := range spec.Operations {
if op.Kind == "" {
return fmt.Errorf("operation[%d] %q: missing required field: kind", i, op.Name)
}
if !knownKinds[op.Kind] {
return fmt.Errorf("operation[%d] %q: unknown kind %q (valid: instance, subresource, action)", i, op.Name, op.Kind)
}
if op.Action == "" {
return fmt.Errorf("operation[%d] %q (kind=%s): missing required field: action", i, op.Name, op.Kind)
}
if op.Kind == "subresource" && op.Subresource == "" {
return fmt.Errorf("operation[%d] %q (kind=subresource): missing required field: subresource", i, op.Name)
}
}
return nil
}