github.com/mobiledgex/go-swagger@v0.19.0/cmd/swagger/commands/flatten.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/go-openapi/analysis"
     7  	"github.com/go-openapi/loads"
     8  	"github.com/go-swagger/go-swagger/cmd/swagger/commands/generate"
     9  	flags "github.com/jessevdk/go-flags"
    10  )
    11  
    12  // FlattenSpec is a command that flattens a swagger document
    13  // which will expand the remote references in a spec and move inline schemas to definitions
    14  // after flattening there are no complex inlined anymore
    15  type FlattenSpec struct {
    16  	Compact bool           `long:"compact" description:"applies to JSON formatted specs. When present, doesn't prettify the json"`
    17  	Output  flags.Filename `long:"output" short:"o" description:"the file to write to"`
    18  	Format  string         `long:"format" description:"the format for the spec document" default:"json" choice:"yaml" choice:"json"`
    19  	generate.FlattenCmdOptions
    20  }
    21  
    22  // Execute flattens the spec
    23  func (c *FlattenSpec) Execute(args []string) error {
    24  	if len(args) != 1 {
    25  		return errors.New("The flatten command requires the single swagger document url to be specified")
    26  	}
    27  
    28  	swaggerDoc := args[0]
    29  	specDoc, err := loads.Spec(swaggerDoc)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	flattenOpts := c.FlattenCmdOptions.SetFlattenOptions(&analysis.FlattenOpts{
    35  		// defaults
    36  		Minimal:      true,
    37  		Verbose:      true,
    38  		Expand:       false,
    39  		RemoveUnused: false,
    40  	})
    41  	flattenOpts.BasePath = specDoc.SpecFilePath()
    42  	flattenOpts.Spec = analysis.New(specDoc.Spec())
    43  	if err := analysis.Flatten(*flattenOpts); err != nil {
    44  		return err
    45  	}
    46  
    47  	return writeToFile(specDoc.Spec(), !c.Compact, c.Format, string(c.Output))
    48  }