go/pkg/sdk/detect.go
Snider adaa4131f9 refactor: strip to pure package library (#3)
- Fix remaining 187 pkg/ files referencing core/cli → core/go
- Move SDK library code from internal/cmd/sdk/ → pkg/sdk/ (new package)
- Create pkg/rag/helpers.go with convenience functions from internal/cmd/rag/
- Fix pkg/mcp/tools_rag.go to use pkg/rag instead of internal/cmd/rag
- Fix pkg/build/buildcmd/cmd_sdk.go and pkg/release/sdk.go to use pkg/sdk
- Remove all non-library content: main.go, internal/, cmd/, docker/,
  scripts/, tasks/, tools/, .core/, .forgejo/, .woodpecker/, Taskfile.yml
- Run go mod tidy to trim unused dependencies

core/go is now a pure Go package suite (library only).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Co-authored-by: Claude <developers@lethean.io>
Reviewed-on: #3
2026-02-16 14:23:45 +00:00

78 lines
2 KiB
Go

package sdk
import (
"fmt"
"path/filepath"
"strings"
coreio "forge.lthn.ai/core/go/pkg/io"
)
// commonSpecPaths are checked in order when no spec is configured.
var commonSpecPaths = []string{
"api/openapi.yaml",
"api/openapi.json",
"openapi.yaml",
"openapi.json",
"docs/api.yaml",
"docs/api.json",
"swagger.yaml",
"swagger.json",
}
// DetectSpec finds the OpenAPI spec file.
// Priority: config path -> common paths -> Laravel Scramble.
func (s *SDK) DetectSpec() (string, error) {
// 1. Check configured path
if s.config.Spec != "" {
specPath := filepath.Join(s.projectDir, s.config.Spec)
if coreio.Local.IsFile(specPath) {
return specPath, nil
}
return "", fmt.Errorf("sdk.DetectSpec: configured spec not found: %s", s.config.Spec)
}
// 2. Check common paths
for _, p := range commonSpecPaths {
specPath := filepath.Join(s.projectDir, p)
if coreio.Local.IsFile(specPath) {
return specPath, nil
}
}
// 3. Try Laravel Scramble detection
specPath, err := s.detectScramble()
if err == nil {
return specPath, nil
}
return "", fmt.Errorf("sdk.DetectSpec: no OpenAPI spec found (checked config, common paths, Scramble)")
}
// detectScramble checks for Laravel Scramble and exports the spec.
func (s *SDK) detectScramble() (string, error) {
composerPath := filepath.Join(s.projectDir, "composer.json")
if !coreio.Local.IsFile(composerPath) {
return "", fmt.Errorf("no composer.json")
}
// Check for scramble in composer.json
data, err := coreio.Local.Read(composerPath)
if err != nil {
return "", err
}
// Simple check for scramble package
if !containsScramble(data) {
return "", fmt.Errorf("scramble not found in composer.json")
}
// TODO: Run php artisan scramble:export
return "", fmt.Errorf("scramble export not implemented")
}
// containsScramble checks if composer.json includes scramble.
func containsScramble(content string) bool {
return strings.Contains(content, "dedoc/scramble") ||
strings.Contains(content, "\"scramble\"")
}