* feat(help): Add CLI help command Fixes #136 * chore: remove binary * feat(mcp): Add TCP transport Fixes #126 * feat(io): Migrate pkg/mcp to use Medium abstraction Fixes #103 * chore(io): Migrate internal/cmd/docs/* to Medium abstraction Fixes #113 * chore(io): Migrate internal/cmd/dev/* to Medium abstraction Fixes #114 * chore(io): Migrate internal/cmd/setup/* to Medium abstraction * chore(io): Complete migration of internal/cmd/dev/* to Medium abstraction * chore(io): Migrate internal/cmd/sdk, pkgcmd, and workspace to Medium abstraction * style: fix formatting in internal/variants Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor(io): simplify local Medium implementation Rewrote to match the simpler TypeScript pattern: - path() sanitizes and returns string directly - Each method calls path() once - No complex symlink validation - Less code, less attack surface Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(mcp): update sandboxing tests for simplified Medium The simplified io/local.Medium implementation: - Sanitizes .. to . (no error, path is cleaned) - Allows absolute paths through (caller validates if needed) - Follows symlinks (no traversal blocking) Update tests to match this simplified behavior. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(updater): resolve PkgVersion duplicate declaration Remove var PkgVersion from updater.go since go generate creates const PkgVersion in version.go. Track version.go in git to ensure builds work without running go generate first. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package generators
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
|
|
coreio "github.com/host-uk/core/pkg/io"
|
|
)
|
|
|
|
// GoGenerator generates Go SDKs from OpenAPI specs.
|
|
type GoGenerator struct{}
|
|
|
|
// NewGoGenerator creates a new Go generator.
|
|
func NewGoGenerator() *GoGenerator {
|
|
return &GoGenerator{}
|
|
}
|
|
|
|
// Language returns the generator's target language identifier.
|
|
func (g *GoGenerator) Language() string {
|
|
return "go"
|
|
}
|
|
|
|
// Available checks if generator dependencies are installed.
|
|
func (g *GoGenerator) Available() bool {
|
|
_, err := exec.LookPath("oapi-codegen")
|
|
return err == nil
|
|
}
|
|
|
|
// Install returns instructions for installing the generator.
|
|
func (g *GoGenerator) Install() string {
|
|
return "go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest"
|
|
}
|
|
|
|
// Generate creates SDK from OpenAPI spec.
|
|
func (g *GoGenerator) Generate(ctx context.Context, opts Options) error {
|
|
if err := coreio.Local.EnsureDir(opts.OutputDir); err != nil {
|
|
return fmt.Errorf("go.Generate: failed to create output dir: %w", err)
|
|
}
|
|
|
|
if g.Available() {
|
|
return g.generateNative(ctx, opts)
|
|
}
|
|
return g.generateDocker(ctx, opts)
|
|
}
|
|
|
|
func (g *GoGenerator) generateNative(ctx context.Context, opts Options) error {
|
|
outputFile := filepath.Join(opts.OutputDir, "client.go")
|
|
|
|
cmd := exec.CommandContext(ctx, "oapi-codegen",
|
|
"-package", opts.PackageName,
|
|
"-generate", "types,client",
|
|
"-o", outputFile,
|
|
opts.SpecPath,
|
|
)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return fmt.Errorf("go.generateNative: %w", err)
|
|
}
|
|
|
|
goMod := fmt.Sprintf("module %s\n\ngo 1.21\n", opts.PackageName)
|
|
return coreio.Local.Write(filepath.Join(opts.OutputDir, "go.mod"), goMod)
|
|
}
|
|
|
|
func (g *GoGenerator) generateDocker(ctx context.Context, opts Options) error {
|
|
specDir := filepath.Dir(opts.SpecPath)
|
|
specName := filepath.Base(opts.SpecPath)
|
|
|
|
args := []string{"run", "--rm"}
|
|
args = append(args, dockerUserArgs()...)
|
|
args = append(args,
|
|
"-v", specDir+":/spec",
|
|
"-v", opts.OutputDir+":/out",
|
|
"openapitools/openapi-generator-cli", "generate",
|
|
"-i", "/spec/"+specName,
|
|
"-g", "go",
|
|
"-o", "/out",
|
|
"--additional-properties=packageName="+opts.PackageName,
|
|
)
|
|
|
|
cmd := exec.CommandContext(ctx, "docker", args...)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
return cmd.Run()
|
|
}
|