github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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 flags "github.com/jessevdk/go-flags" 9 "github.com/thetreep/go-swagger/cmd/swagger/commands/generate" 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("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( 35 &analysis.FlattenOpts{ 36 // defaults 37 Minimal: true, 38 Verbose: true, 39 Expand: false, 40 RemoveUnused: false, 41 }, 42 ) 43 flattenOpts.BasePath = specDoc.SpecFilePath() 44 flattenOpts.Spec = analysis.New(specDoc.Spec()) 45 if err := analysis.Flatten(*flattenOpts); err != nil { 46 return err 47 } 48 49 return writeToFile(specDoc.Spec(), !c.Compact, c.Format, string(c.Output)) 50 }