Skip to main content

cmd/openapi/main.go

Source

Overview

What: CLI entrypoint that regenerates the merged OpenAPI document.

Why: Produces a single dist/openapi.json artifact from the OpenAPI fragments under specs/ for publishing and tooling.

How: Builds an internal/openapi service with an output path, runs the merge via Document(ctx), and exits non-zero on failure.

Contents

Imports

import block 1

cmd/openapi/main.go#L3
import (
"context"
"flag"
"fmt"
"os"

"github.com/theroutercompany/api_router/internal/openapi"
)

Functions and Methods

main

What: Parses --out and triggers OpenAPI merge generation.

Why: Provides a minimal reproducible command for CI and local dev.

How: Instantiates the OpenAPI merge service, calls Document(context.Background()), and prints a success/failure message.

cmd/openapi/main.go#L12
func main() {
outPath := flag.String("out", "dist/openapi.json", "Path to write the merged OpenAPI document")
flag.Parse()

svc := openapi.NewService(openapi.WithDistPath(*outPath))

if _, err := svc.Document(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "openapi merge failed: %v\n", err)
os.Exit(1)
}

fmt.Fprintf(os.Stdout, "OpenAPI document written to %s\n", *outPath)
}

Walkthrough

The list below documents the statements inside the function body, including nested blocks and inline closures.

  • L13: outPath := flag.String("out", "dist/openapi.json", "Path to write the merged OpenAPI document")
    • What: Defines outPath.
    • Why: Keeps intermediate state available for later steps in the function.
    • How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
  • L14: flag.Parse()
    • What: Calls flag.Parse.
    • Why: Performs side effects or delegates work to a helper.
    • How: Executes the expression statement.
  • L16: svc := openapi.NewService(openapi.WithDistPath(*outPath))
    • What: Defines svc.
    • Why: Keeps intermediate state available for later steps in the function.
    • How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
  • L18: if _, err := svc.Document(context.Background()); err != nil { fmt.Fprintf(os.Stderr, "openapi merge failed: %v\n", err) os.Exit(1) }
    • What: Branches conditionally.
    • Why: Handles different execution paths based on runtime state.
    • How: Evaluates the condition and executes the matching branch.
    • Nested steps:
      • L18: _, err := svc.Document(context.Background())
        • What: Defines _, err.
        • Why: Keeps intermediate state available for later steps in the function.
        • How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
      • L19: fmt.Fprintf(os.Stderr, "openapi merge failed: %v\n", err)
        • What: Calls fmt.Fprintf.
        • Why: Performs side effects or delegates work to a helper.
        • How: Executes the expression statement.
      • L20: os.Exit(1)
        • What: Calls os.Exit.
        • Why: Performs side effects or delegates work to a helper.
        • How: Executes the expression statement.
  • L23: fmt.Fprintf(os.Stdout, "OpenAPI document written to %s\n", *outPath)
    • What: Calls fmt.Fprintf.
    • Why: Performs side effects or delegates work to a helper.
    • How: Executes the expression statement.

Guides

Neighboring source