refactor(service_spec_gen): split into 6 packages (types, client, config, normalize, spec, main) + .gitignore
- Split 741-line monolith into 6 independent packages - All functions exported, no circular deps - Build: go build -o bin/service_spec_gen ./tools/service_spec_gen/ - Script 01_generate_yamls.sh: go run → ./bin/service_spec_gen - Add bin/ to .gitignore
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
// service_spec_gen — генератор YAML-спеков сервисов из API Nubes.
|
||||
//
|
||||
// Читает список сервисов, дёргает API, собирает операции/параметры/MAN,
|
||||
// пишет один YAML на сервис в resources_yaml/.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"terraform-provider-nubes/tools/service_spec_gen/client"
|
||||
"terraform-provider-nubes/tools/service_spec_gen/config"
|
||||
"terraform-provider-nubes/tools/service_spec_gen/normalize"
|
||||
"terraform-provider-nubes/tools/service_spec_gen/spec"
|
||||
"terraform-provider-nubes/tools/service_spec_gen/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cli := client.New(cfg.APIEndpoint, cfg.APIToken)
|
||||
|
||||
services := cfg.Services
|
||||
if cfg.SingleServiceID > 0 {
|
||||
services = []config.ServiceRef{{ID: cfg.SingleServiceID, Name: cfg.SingleServiceName}}
|
||||
}
|
||||
|
||||
outputDir := cfg.OutputDir
|
||||
if outputDir == "" {
|
||||
repoRoot, err := config.FindRepoRoot()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
outputDir = filepath.Join(repoRoot, "resources_yaml")
|
||||
}
|
||||
if err := spec.ValidateOutputDir(outputDir); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := os.MkdirAll(outputDir, 0o755); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, svc := range services {
|
||||
if svc.ID <= 0 {
|
||||
continue
|
||||
}
|
||||
info, err := cli.GetService(svc.ID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARNING: service %d (%s) skipped: %v\n", svc.ID, svc.Name, err)
|
||||
continue
|
||||
}
|
||||
name := strings.TrimSpace(svc.Name)
|
||||
if name == "" {
|
||||
name = strings.TrimSpace(info.ShortName)
|
||||
}
|
||||
if name == "" {
|
||||
name = strings.TrimSpace(info.DisplayName)
|
||||
}
|
||||
name = normalize.ServiceName(name)
|
||||
if name == "" {
|
||||
name = fmt.Sprintf("service_%d", svc.ID)
|
||||
}
|
||||
|
||||
ops, hasSuspend, _, _, err := cli.CollectOperations(info.Operations)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
specYAML := types.ServiceSpec{
|
||||
Name: name,
|
||||
ServiceID: svc.ID,
|
||||
ServiceDisplayName: strings.TrimSpace(info.DisplayName),
|
||||
ServiceShortName: strings.TrimSpace(info.ShortName),
|
||||
ServiceMan: strings.TrimSpace(info.Man),
|
||||
Lifecycle: types.Lifecycle{
|
||||
SuspendOnDestroyDefault: hasSuspend,
|
||||
AdoptExistingOnCreateDefault: false,
|
||||
},
|
||||
Outputs: types.OutputSection{Params: spec.DefaultOutputParams()},
|
||||
Operations: ops,
|
||||
}
|
||||
|
||||
outPath := filepath.Join(outputDir, fmt.Sprintf("%d_%s.yaml", svc.ID, name))
|
||||
buf, err := yaml.Marshal(specYAML)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := os.WriteFile(outPath, buf, 0o644); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("written %s\n", outPath)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user