Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: format transform #1094

Merged
merged 6 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions cmd/openapi/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package openapi

import (
"context"
"os"

charm_internal "github.com/speakeasy-api/speakeasy/internal/charm"
"github.com/speakeasy-api/speakeasy/internal/model"
"github.com/speakeasy-api/speakeasy/internal/model/flag"
"github.com/speakeasy-api/speakeasy/internal/transform"
"github.com/speakeasy-api/speakeasy/internal/utils"
"os"
)

var transformCmd = &model.CommandGroup{
Usage: "transform",
Short: "Transform an OpenAPI spec using a well-defined function",
Commands: []model.Command{removeUnusedCmd, filterOperationsCmd, cleanupCmd},
Commands: []model.Command{removeUnusedCmd, filterOperationsCmd, cleanupCmd, formatCmd},
}

type basicFlagsI struct {
Expand Down Expand Up @@ -76,6 +77,14 @@ var cleanupCmd = &model.ExecutableCommand[basicFlagsI]{
Flags: basicFlags,
}

var formatCmd = &model.ExecutableCommand[basicFlagsI]{
Usage: "format",
Short: "Format an OpenAPI document to be more human-readable",
Long: "Format an OpenAPI document to be more human-readable by sorting the keys in a specific order best suited for each level in the OpenAPI specification",
Run: runFormat,
Flags: basicFlags,
}

func runRemoveUnused(ctx context.Context, flags basicFlagsI) error {
out, yamlOut, err := setupOutput(ctx, flags.Out)
defer out.Close()
Expand Down Expand Up @@ -106,6 +115,16 @@ func runCleanup(ctx context.Context, flags basicFlagsI) error {
return transform.CleanupDocument(ctx, flags.Schema, yamlOut, out)
}

func runFormat(ctx context.Context, flags basicFlagsI) error {
out, yamlOut, err := setupOutput(ctx, flags.Out)
defer out.Close()
if err != nil {
return err
}

return transform.FormatDocument(ctx, flags.Schema, yamlOut, out)
}

func setupOutput(ctx context.Context, out string) (*os.File, bool, error) {
yamlOut := utils.HasYAMLExt(out)

Expand Down
Loading
Loading