Change module path from forge.lthn.ai/core/api to dappco.re/go/core/api. Update all Go imports accordingly: - forge.lthn.ai/core/api -> dappco.re/go/core/api - forge.lthn.ai/core/go-io -> dappco.re/go/core/io - forge.lthn.ai/core/go-log -> dappco.re/go/core/log forge.lthn.ai/core/cli left as-is (not yet migrated). Local replace directives added for dappco.re paths until vanity URL server is configured. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
coreio "dappco.re/go/core/io"
|
|
coreerr "dappco.re/go/core/log"
|
|
)
|
|
|
|
// ExportSpec generates the OpenAPI spec and writes it to w.
|
|
// Format must be "json" or "yaml".
|
|
func ExportSpec(w io.Writer, format string, builder *SpecBuilder, groups []RouteGroup) error {
|
|
data, err := builder.Build(groups)
|
|
if err != nil {
|
|
return coreerr.E("ExportSpec", "build spec", err)
|
|
}
|
|
|
|
switch format {
|
|
case "json":
|
|
_, err = w.Write(data)
|
|
return err
|
|
case "yaml":
|
|
// Unmarshal JSON then re-marshal as YAML.
|
|
var obj any
|
|
if err := json.Unmarshal(data, &obj); err != nil {
|
|
return coreerr.E("ExportSpec", "unmarshal spec", err)
|
|
}
|
|
enc := yaml.NewEncoder(w)
|
|
enc.SetIndent(2)
|
|
if err := enc.Encode(obj); err != nil {
|
|
return coreerr.E("ExportSpec", "encode yaml", err)
|
|
}
|
|
return enc.Close()
|
|
default:
|
|
return coreerr.E("ExportSpec", "unsupported format "+format+": use \"json\" or \"yaml\"", nil)
|
|
}
|
|
}
|
|
|
|
// ExportSpecToFile writes the spec to the given path.
|
|
// The parent directory is created if it does not exist.
|
|
func ExportSpecToFile(path, format string, builder *SpecBuilder, groups []RouteGroup) error {
|
|
if err := coreio.Local.EnsureDir(filepath.Dir(path)); err != nil {
|
|
return coreerr.E("ExportSpecToFile", "create directory", err)
|
|
}
|
|
f, err := os.Create(path)
|
|
if err != nil {
|
|
return coreerr.E("ExportSpecToFile", "create file", err)
|
|
}
|
|
defer f.Close()
|
|
return ExportSpec(f, format, builder, groups)
|
|
}
|