-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.go
More file actions
583 lines (511 loc) · 19 KB
/
generate.go
File metadata and controls
583 lines (511 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Package cmd contains all CLI commands for spec-forge.
package cmd
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"github.com/getkin/kin-openapi/openapi3"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/spencercjh/spec-forge/internal/cli"
"github.com/spencercjh/spec-forge/internal/config"
"github.com/spencercjh/spec-forge/internal/enricher"
"github.com/spencercjh/spec-forge/internal/enricher/processor"
"github.com/spencercjh/spec-forge/internal/enricher/provider"
"github.com/spencercjh/spec-forge/internal/extractor"
"github.com/spencercjh/spec-forge/internal/extractor/builtin" // registers built-in extractors
"github.com/spencercjh/spec-forge/internal/publisher"
"github.com/spencercjh/spec-forge/internal/validator"
)
const (
outputFormatYAML = "yaml"
outputFormatJSON = "json"
)
// generateCmd represents the generate command
var generateCmd = &cobra.Command{
Use: "generate [path]",
Short: "Generate OpenAPI spec from source code",
Long: `Generate OpenAPI specification by running the complete pipeline:
detect -> patch -> generate -> validate -> restore (optional)
This is the main command that orchestrates the entire workflow.
By default, the original pom.xml/build.gradle is restored after extraction
to preserve your project's formatting. Use --keep-patched to keep the changes.`,
Args: cobra.MaximumNArgs(1),
RunE: runGenerate,
}
func runGenerate(cmd *cobra.Command, args []string) error { //nolint:gocyclo // CLI command runner requires many branches
ctx := cmd.Context()
path := "."
if len(args) > 0 {
path = args[0]
}
cli.Statusf(os.Stderr, "Generating OpenAPI spec in %s...", path)
// Get all flag values from command (isolated per command instance)
//nolint:errcheck // flags are bound at command creation, errors not possible
keepPatched, _ := cmd.Flags().GetBool("keep-patched")
//nolint:errcheck
skipValidate, _ := cmd.Flags().GetBool("skip-validate")
//nolint:errcheck
timeout, _ := cmd.Flags().GetDuration("timeout")
//nolint:errcheck
skipEnrich, _ := cmd.Flags().GetBool("skip-enrich")
//nolint:errcheck
language, _ := cmd.Flags().GetString("language")
//nolint:errcheck
outputDirFlag, _ := cmd.Flags().GetString("output-dir")
//nolint:errcheck
outputFormatFlag, _ := cmd.Flags().GetString("output")
//nolint:errcheck
skipPublish, _ := cmd.Flags().GetBool("skip-publish")
//nolint:errcheck
publishTarget, _ := cmd.Flags().GetString("publish-target")
//nolint:errcheck
publishOverwrite, _ := cmd.Flags().GetBool("publish-overwrite")
//nolint:errcheck
overwriteOutput, _ := cmd.Flags().GetBool("overwrite-output")
//nolint:errcheck
protoImportPaths, _ := cmd.Flags().GetStringSlice("proto-import-path")
// Step 1: Detect framework - try all registered extractors
extractorImpl, info, err := builtin.DetectFramework(path)
if err != nil {
return errWrap("no supported framework detected", err)
}
cli.Statusf(os.Stderr, "Detected %s project (tool: %s, build: %s)", extractorImpl.Name(), info.BuildTool, info.BuildFilePath)
// Step 2: Patch project if needed
patchOpts := &extractor.PatchOptions{
KeepPatched: keepPatched,
}
patchResult, err := extractorImpl.Patch(path, patchOpts)
if err != nil {
return errWrap("patch failed", err)
}
// Step 3: If we patched the file and should restore later, defer the restore
if !keepPatched && patchResult.OriginalContent != "" {
defer func() {
slog.DebugContext(ctx, "Restoring original build file")
if restoreErr := extractorImpl.Restore(patchResult.BuildFilePath, patchResult.OriginalContent); restoreErr != nil {
slog.WarnContext(ctx, "failed to restore original file", "error", restoreErr)
} else {
slog.DebugContext(ctx, "Original build file restored")
}
}()
}
if patchResult.DependencyAdded {
cli.Successf(os.Stderr, "Dependencies added temporarily")
}
if patchResult.PluginAdded {
cli.Successf(os.Stderr, "Plugin added temporarily")
}
if patchResult.SpringBootConfigured {
cli.Successf(os.Stderr, "spring-boot-maven-plugin configured with start/stop goals")
}
// Step 4: Generate OpenAPI spec
// Determine output directory
// Precedence: flag > config > default (project root)
outputDir := outputDirFlag
if outputDir == "" {
outputDir = config.Get().Output.Dir
}
if outputDir == "" {
outputDir = path // Default to project root
}
// Determine output format
outputFormat := outputFormatFlag
if outputFormat == "" {
outputFormat = config.Get().Output.Format
}
if outputFormat == "" {
outputFormat = outputFormatYAML
}
// Normalize format value for consistent handling across extractors
outputFormat, err = normalizeOutputFormat(outputFormat)
if err != nil {
return errWrap("invalid output format", err)
}
genOpts := &extractor.GenerateOptions{
OutputDir: outputDir,
Format: outputFormat,
Timeout: timeout,
SkipTests: true,
ProtoImportPaths: protoImportPaths,
}
genResult, err := extractorImpl.Generate(ctx, path, info, genOpts)
if err != nil {
return errWrap("generation failed", err)
}
cli.Statusf(os.Stderr, "OpenAPI spec generated: %s (%s)", genResult.SpecFilePath, genResult.Format)
// Step 5: Validate the generated spec
if !skipValidate {
v := validator.NewValidator()
valResult, valErr := v.Validate(ctx, genResult.SpecFilePath)
if valErr != nil {
return errWrap("validation error", valErr)
}
if !valResult.Valid {
cli.Errorf(os.Stderr, "OpenAPI spec validation failed")
for _, validationErr := range valResult.Errors {
cli.Errorf(os.Stderr, " - %s", validationErr)
}
return errors.New("generated OpenAPI spec is invalid")
}
cli.Successf(os.Stderr, "OpenAPI spec validated")
} else {
cli.Skipf(os.Stderr, "Validation skipped")
}
// Step 6: Enrich with AI (optional)
cfg := config.Get()
if !skipEnrich && cfg.Enrich.Enabled && cfg.Enrich.Provider != "" && cfg.Enrich.Model != "" {
if enrichErr := enrichGeneratedSpec(ctx, genResult.SpecFilePath, cfg, language); enrichErr != nil {
// Log warning but don't fail - enrichment is optional
slog.WarnContext(ctx, "Enrichment failed (non-fatal)", "error", enrichErr)
}
} else {
cli.Skipf(os.Stderr, "Enrichment skipped")
}
// Step 7: Ensure spec is in the output directory
// Some extractors (Spring) generate to target/build and need copying
// Others (Gin, go-zero, gRPC) may already have written to outputDir
genDir := filepath.Dir(genResult.SpecFilePath)
targetPath := filepath.Join(outputDir, filepath.Base(genResult.SpecFilePath))
// Clean paths for comparison
absGenDir, err := filepath.Abs(genDir)
if err != nil {
absGenDir = genDir // Fallback to relative path
}
absOutputDir, err := filepath.Abs(outputDir)
if err != nil {
absOutputDir = outputDir // Fallback to relative path
}
if absGenDir != absOutputDir {
if err := copySpecToOutput(genResult.SpecFilePath, outputDir, overwriteOutput); err != nil {
return errWrap("failed to copy spec to output directory", err)
}
cli.Successf(os.Stderr, "Spec saved: %s", targetPath)
// Update genResult.SpecFilePath to point to the copied file for publishing
genResult.SpecFilePath = targetPath
} else {
cli.Successf(os.Stderr, "Spec saved: %s", genResult.SpecFilePath)
}
// Step 8: Publish the spec to remote platforms (optional)
if !skipPublish && publishTarget != "" {
// Create publisher using factory
pub, err := publisher.NewPublisher(publishTarget)
if err != nil {
return errWrap("failed to create publisher", err)
}
// Build publish options
pubOpts := &publisher.PublishOptions{
OutputPath: genResult.SpecFilePath,
Format: outputFormat,
Overwrite: publishOverwrite,
}
// Add ReadMe-specific options if using ReadMe publisher
if pub.Name() == "readme" {
cfg := config.Get()
pubOpts.ReadMe = &publisher.ReadMeOptions{
APIKey: resolveReadMeAPIKey(cfg.ReadMe),
Branch: cfg.ReadMe.Branch,
Slug: cfg.ReadMe.Slug,
UseSpecVersion: cfg.ReadMe.UseSpecVersion,
}
}
// Load spec for publishing
loader := openapi3.NewLoader()
spec, err := loader.LoadFromFile(genResult.SpecFilePath)
if err != nil {
return errWrap("failed to load spec for publishing", err)
}
// Publish
pubResult, err := pub.Publish(ctx, spec, pubOpts)
if err != nil {
return errWrap("failed to publish spec", err)
}
cli.Successf(os.Stderr, "Spec published to %s", pub.Name())
if pubResult.Message != "" {
cli.Statusf(os.Stderr, "%s", pubResult.Message)
}
}
// Step 8: Output final result
cli.Successf(os.Stderr, "Generation complete")
return nil
}
// newGenerateCmd creates a new generate command instance for testing.
func newGenerateCmd() *cobra.Command {
c := &cobra.Command{
Use: "generate [path]",
Short: "Generate OpenAPI spec from source code",
Long: `Generate OpenAPI specification by running the complete pipeline:
detect -> patch -> generate -> validate -> restore (optional)
This is the main command that orchestrates the entire workflow.
By default, the original pom.xml/build.gradle is restored after extraction
to preserve your project's formatting. Use --keep-patched to keep the changes.`,
Args: cobra.MaximumNArgs(1),
RunE: runGenerate,
}
c.Flags().Bool("keep-patched", false,
"keep the patched pom.xml/build.gradle (default: restore original after extraction)")
c.Flags().Bool("skip-validate", false,
"skip validation of the generated OpenAPI spec")
c.Flags().Duration("timeout", 5*time.Minute,
"timeout for Maven/Gradle commands")
c.Flags().Bool("skip-enrich", false,
"skip AI enrichment of the generated OpenAPI spec")
c.Flags().String("language", "en",
"language for AI-generated descriptions (e.g., en, zh)")
c.Flags().StringP("output", "o", "",
"output format (yaml or json; defaults to yaml if not specified in config)")
c.Flags().StringP("output-dir", "d", "",
"output directory for generated spec (default: project root)")
c.Flags().Bool("skip-publish", false,
"skip publishing to remote platforms")
c.Flags().String("publish-target", "",
"publish target (readme). If empty, spec is only saved locally")
c.Flags().Bool("publish-overwrite", false,
"overwrite existing spec on remote platform")
c.Flags().Bool("overwrite-output", false,
"overwrite existing local spec file if it already exists")
c.Flags().StringSlice("proto-import-path", nil,
"additional import paths for protoc (-I flags), can be specified multiple times")
registerCompletion(c, "output", []string{"yaml", "json"})
registerCompletion(c, "language", []string{"en", "zh"})
registerCompletion(c, "publish-target", []string{"readme"})
return c
}
// generate command flag variables for global rootCmd only
var (
generateKeepPatched bool
generateSkipValidate bool
generateTimeout time.Duration
generateSkipEnrich bool
generateLanguage string
generateOutputDir string
generateOutputFormat string
generateSkipPublish bool
generatePublishTarget string
generatePublishOverwrite bool
generateOverwriteOutput bool
generateProtoImportPaths []string
)
func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().BoolVar(&generateKeepPatched, "keep-patched", false,
"keep the patched pom.xml/build.gradle (default: restore original after extraction)")
generateCmd.Flags().BoolVar(&generateSkipValidate, "skip-validate", false,
"skip validation of the generated OpenAPI spec")
generateCmd.Flags().DurationVar(&generateTimeout, "timeout", 5*time.Minute,
"timeout for Maven/Gradle commands")
generateCmd.Flags().BoolVar(&generateSkipEnrich, "skip-enrich", false,
"skip AI enrichment of the generated OpenAPI spec")
generateCmd.Flags().StringVar(&generateLanguage, "language", "en",
"language for AI-generated descriptions (e.g., en, zh)")
generateCmd.Flags().StringVarP(&generateOutputFormat, "output", "o", "",
"output format (yaml or json, default: yaml)")
generateCmd.Flags().StringVarP(&generateOutputDir, "output-dir", "d", "",
"output directory for generated spec (default: project root)")
generateCmd.Flags().BoolVar(&generateSkipPublish, "skip-publish", false,
"skip publishing to remote platforms")
generateCmd.Flags().StringVar(&generatePublishTarget, "publish-target", "",
"publish target (readme). If empty, spec is only saved locally")
generateCmd.Flags().BoolVar(&generatePublishOverwrite, "publish-overwrite", false,
"overwrite existing spec on remote platform")
generateCmd.Flags().BoolVar(&generateOverwriteOutput, "overwrite-output", false,
"overwrite existing local spec file if it already exists")
generateCmd.Flags().StringSliceVar(&generateProtoImportPaths, "proto-import-path", nil,
"additional import paths for protoc (-I flags), can be specified multiple times")
registerCompletion(generateCmd, "output", []string{"yaml", "json"})
registerCompletion(generateCmd, "language", []string{"en", "zh"})
registerCompletion(generateCmd, "publish-target", []string{"readme"})
}
// enrichGeneratedSpec enriches the generated spec with AI-generated descriptions
func enrichGeneratedSpec(ctx context.Context, specFilePath string, cfg *config.Config, language string) error {
cli.Statusf(os.Stderr, "Enriching OpenAPI spec with AI descriptions...")
// Determine language
lang := language
if lang == "" {
lang = cfg.Enrich.Language
}
if lang == "" {
lang = "en"
}
// Create provider
p, err := createProviderFromConfig(cfg.Enrich)
if err != nil {
return fmt.Errorf("failed to create provider: %w", err)
}
// Load spec
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
spec, err := loader.LoadFromFile(specFilePath)
if err != nil {
return fmt.Errorf("failed to load spec: %w", err)
}
// Parse timeout
timeout := 30 * time.Second
if cfg.Enrich.Timeout != "" {
if parsed, parseErr := time.ParseDuration(cfg.Enrich.Timeout); parseErr == nil {
timeout = parsed
}
}
// Create enricher config
enricherCfg := enricher.Config{
Provider: cfg.Enrich.Provider,
Model: cfg.Enrich.Model,
Language: lang,
Timeout: timeout,
CustomBaseURL: cfg.Enrich.BaseURL,
}
enricherCfg = enricherCfg.MergeWithDefaults()
// Create enricher
e, err := enricher.NewEnricher(enricherCfg, p)
if err != nil {
return fmt.Errorf("failed to create enricher: %w", err)
}
// Enrich
result, err := e.Enrich(ctx, spec, &enricher.EnrichOptions{Language: lang})
if err != nil {
// Check if partial enrichment
if partialErr, ok := errors.AsType[*processor.PartialEnrichmentError](err); ok {
slog.WarnContext(ctx, "Partial enrichment completed",
"failed_batches", partialErr.FailedBatches,
"total_batches", partialErr.TotalBatches,
)
} else {
return fmt.Errorf("enrichment failed: %w", err)
}
}
// Save enriched spec to file
var data []byte
if strings.ToLower(filepath.Ext(specFilePath)) == ".json" {
data, err = result.MarshalJSON()
} else {
var yamlData any
yamlData, err = result.MarshalYAML()
if err == nil {
data, err = yaml.Marshal(yamlData)
}
}
if err != nil {
return fmt.Errorf("failed to marshal enriched spec: %w", err)
}
if writeErr := os.WriteFile(specFilePath, data, 0o600); writeErr != nil {
return fmt.Errorf("failed to write enriched spec: %w", writeErr)
}
cli.Successf(os.Stderr, "OpenAPI spec enriched: %s", specFilePath)
return nil
}
// createProviderFromConfig creates a provider from config settings
func createProviderFromConfig(cfg config.EnrichConfig) (provider.Provider, error) { //nolint:gocritic // copying config is acceptable
// Get API key based on provider type
var apiKey string
switch cfg.Provider {
case "openai":
apiKey = os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
return nil, errors.New("OPENAI_API_KEY environment variable not set")
}
case "anthropic":
apiKey = os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
return nil, errors.New("ANTHROPIC_API_KEY environment variable not set")
}
case "custom":
apiKey = getAPIKeyFromConfig(cfg)
if apiKey == "" {
return nil, errors.New("API key not found for custom provider")
}
}
return provider.NewProvider(provider.Config{
Provider: cfg.Provider,
Model: cfg.Model,
APIKey: apiKey,
BaseURL: cfg.BaseURL,
})
}
// getAPIKeyFromConfig gets API key from config or environment
func getAPIKeyFromConfig(cfg config.EnrichConfig) string { //nolint:gocritic // copying config is acceptable
// First check explicit config
if cfg.APIKey != "" {
return cfg.APIKey
}
// Then check environment variable
envName := cfg.APIKeyEnv
if envName == "" {
envName = "LLM_API_KEY"
}
return os.Getenv(envName)
}
// errWrap wraps an error with a message.
func errWrap(msg string, err error) error {
return errors.New(msg + ": " + err.Error())
}
// normalizeOutputFormat normalizes and validates the output format value.
// Accepts: "yaml", "yml", "YAML", "json", "JSON" -> returns "yaml" or "json".
// Returns an error for unsupported formats to avoid silent fallback behavior.
func normalizeOutputFormat(format string) (string, error) {
normalized := strings.ToLower(strings.TrimSpace(format))
switch normalized {
case "yaml", "yml":
return outputFormatYAML, nil
case "json":
return outputFormatJSON, nil
default:
return "", fmt.Errorf("unsupported output format %q; allowed values are %q and %q", format, outputFormatYAML, outputFormatJSON)
}
}
// copySpecToOutput copies the generated spec to the specified output directory.
// If overwrite is false and the destination file already exists, an error is returned.
func copySpecToOutput(srcPath, outputDir string, overwrite bool) error {
// Create output directory if it doesn't exist
if err := os.MkdirAll(outputDir, 0o755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
// Open source file
srcFile, err := os.Open(srcPath)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer srcFile.Close()
// Determine destination filename
filename := filepath.Base(srcPath)
dstPath := filepath.Join(outputDir, filename)
// Check if destination file already exists
if _, statErr := os.Stat(dstPath); statErr == nil {
if !overwrite {
return fmt.Errorf("destination file already exists: %s (use --overwrite-output to overwrite)", dstPath)
}
// Overwrite is allowed, continue
} else if !os.IsNotExist(statErr) {
return fmt.Errorf("failed to check destination file: %w", statErr)
}
// Create destination file (truncates if exists) with restrictive permissions
dstFile, createErr := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if createErr != nil {
return fmt.Errorf("failed to create destination file: %w", createErr)
}
defer dstFile.Close()
// Copy content
if _, copyErr := io.Copy(dstFile, srcFile); copyErr != nil {
return fmt.Errorf("failed to copy file: %w", copyErr)
}
return nil
}
// resolveReadMeAPIKey resolves ReadMe API key from config or environment.
// Priority: 1) cfg.APIKey, 2) cfg.APIKeyEnv (or README_API_KEY as default)
func resolveReadMeAPIKey(cfg config.ReadMeConfig) string {
// First check explicit config
if cfg.APIKey != "" {
return cfg.APIKey
}
// Then check environment variable
envName := cfg.APIKeyEnv
if envName == "" {
envName = "README_API_KEY"
}
return os.Getenv(envName)
}