github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/initcmd/spec.go (about) 1 package initcmd 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "os" 8 "path/filepath" 9 10 "gopkg.in/yaml.v3" 11 12 "github.com/go-openapi/spec" 13 "github.com/go-openapi/swag" 14 ) 15 16 // Spec a command struct for initializing a new swagger application. 17 type Spec struct { 18 Format string `long:"format" description:"the format for the spec document" default:"yaml" choice:"yaml" choice:"json"` 19 Title string `long:"title" description:"the title of the API"` 20 Description string `long:"description" description:"the description of the API"` 21 Version string `long:"version" description:"the version of the API" default:"0.1.0"` 22 Terms string `long:"terms" description:"the terms of services"` 23 Consumes []string `long:"consumes" description:"add a content type to the global consumes definitions, can repeat" default:"application/json"` 24 Produces []string `long:"produces" description:"add a content type to the global produces definitions, can repeat" default:"application/json"` 25 Schemes []string `long:"scheme" description:"add a scheme to the global schemes definition, can repeat" default:"http"` 26 Contact struct { 27 Name string `long:"contact.name" description:"name of the primary contact for the API"` 28 URL string `long:"contact.url" description:"url of the primary contact for the API"` 29 Email string `long:"contact.email" description:"email of the primary contact for the API"` 30 } 31 License struct { 32 Name string `long:"license.name" description:"name of the license for the API"` 33 URL string `long:"license.url" description:"url of the license for the API"` 34 } 35 } 36 37 // Execute this command 38 func (s *Spec) Execute(args []string) error { 39 targetPath := "." 40 if len(args) > 0 { 41 targetPath = args[0] 42 } 43 realPath, err := filepath.Abs(targetPath) 44 if err != nil { 45 return err 46 } 47 var file *os.File 48 switch s.Format { 49 case "json": 50 file, err = os.Create(filepath.Join(realPath, "swagger.json")) 51 if err != nil { 52 return err 53 } 54 case "yaml", "yml": 55 file, err = os.Create(filepath.Join(realPath, "swagger.yml")) 56 if err != nil { 57 return err 58 } 59 default: 60 return fmt.Errorf("invalid format: %s", s.Format) 61 } 62 defer file.Close() 63 log.Println("creating specification document in", filepath.Join(targetPath, file.Name())) 64 65 var doc spec.Swagger 66 info := new(spec.Info) 67 doc.Info = info 68 69 doc.Swagger = "2.0" 70 doc.Paths = new(spec.Paths) 71 doc.Definitions = make(spec.Definitions) 72 73 info.Title = s.Title 74 if info.Title == "" { 75 info.Title = swag.ToHumanNameTitle(filepath.Base(realPath)) 76 } 77 info.Description = s.Description 78 info.Version = s.Version 79 info.TermsOfService = s.Terms 80 if s.Contact.Name != "" || s.Contact.Email != "" || s.Contact.URL != "" { 81 var contact spec.ContactInfo 82 contact.Name = s.Contact.Name 83 contact.Email = s.Contact.Email 84 contact.URL = s.Contact.URL 85 info.Contact = &contact 86 } 87 if s.License.Name != "" || s.License.URL != "" { 88 var license spec.License 89 license.Name = s.License.Name 90 license.URL = s.License.URL 91 info.License = &license 92 } 93 94 doc.Consumes = append(doc.Consumes, s.Consumes...) 95 doc.Produces = append(doc.Produces, s.Produces...) 96 doc.Schemes = append(doc.Schemes, s.Schemes...) 97 98 if s.Format == "json" { 99 enc := json.NewEncoder(file) 100 return enc.Encode(doc) 101 } 102 103 b, err := yaml.Marshal(swag.ToDynamicJSON(doc)) 104 if err != nil { 105 return err 106 } 107 if _, err := file.Write(b); err != nil { 108 return err 109 } 110 return nil 111 }