feat(api): add spec description flag
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
6fdd769212
commit
1c9e4891e7
2 changed files with 42 additions and 5 deletions
|
|
@ -13,10 +13,11 @@ import (
|
|||
|
||||
func addSpecCommand(parent *cli.Command) {
|
||||
var (
|
||||
output string
|
||||
format string
|
||||
title string
|
||||
version string
|
||||
output string
|
||||
format string
|
||||
title string
|
||||
description string
|
||||
version string
|
||||
)
|
||||
|
||||
cmd := cli.NewCommand("spec", "Generate OpenAPI specification", "", func(cmd *cli.Command, args []string) error {
|
||||
|
|
@ -24,7 +25,7 @@ func addSpecCommand(parent *cli.Command) {
|
|||
// Additional groups can be added here as the platform grows.
|
||||
builder := &goapi.SpecBuilder{
|
||||
Title: title,
|
||||
Description: "Lethean Core API",
|
||||
Description: description,
|
||||
Version: version,
|
||||
}
|
||||
|
||||
|
|
@ -48,6 +49,7 @@ func addSpecCommand(parent *cli.Command) {
|
|||
cli.StringFlag(cmd, &output, "output", "o", "", "Write spec to file instead of stdout")
|
||||
cli.StringFlag(cmd, &format, "format", "f", "json", "Output format: json or yaml")
|
||||
cli.StringFlag(cmd, &title, "title", "t", "Lethean Core API", "API title in spec")
|
||||
cli.StringFlag(cmd, &description, "description", "d", "Lethean Core API", "API description in spec")
|
||||
cli.StringFlag(cmd, &version, "version", "V", "1.0.0", "API version in spec")
|
||||
|
||||
parent.AddCommand(cmd)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ package api
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"forge.lthn.ai/core/cli/pkg/cli"
|
||||
|
|
@ -51,11 +53,44 @@ func TestAPISpecCmd_Good_JSON(t *testing.T) {
|
|||
if specCmd.Flag("title") == nil {
|
||||
t.Fatal("expected --title flag on spec command")
|
||||
}
|
||||
if specCmd.Flag("description") == nil {
|
||||
t.Fatal("expected --description flag on spec command")
|
||||
}
|
||||
if specCmd.Flag("version") == nil {
|
||||
t.Fatal("expected --version flag on spec command")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPISpecCmd_Good_CustomDescription(t *testing.T) {
|
||||
root := &cli.Command{Use: "root"}
|
||||
AddAPICommands(root)
|
||||
|
||||
outputFile := t.TempDir() + "/spec.json"
|
||||
root.SetArgs([]string{"api", "spec", "--description", "Custom API description", "--output", outputFile})
|
||||
root.SetErr(new(bytes.Buffer))
|
||||
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
var spec map[string]any
|
||||
data, err := os.ReadFile(outputFile)
|
||||
if err != nil {
|
||||
t.Fatalf("expected spec file to be written: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(data, &spec); err != nil {
|
||||
t.Fatalf("expected valid JSON spec, got error: %v", err)
|
||||
}
|
||||
|
||||
info, ok := spec["info"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("expected info object in generated spec")
|
||||
}
|
||||
if info["description"] != "Custom API description" {
|
||||
t.Fatalf("expected custom description, got %v", info["description"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPISDKCmd_Bad_NoLang(t *testing.T) {
|
||||
root := &cli.Command{Use: "root"}
|
||||
AddAPICommands(root)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue