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