github.com/youyuanwu/go-swagger@v0.19.0/cmd/swagger/swagger.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "io/ioutil" 19 "log" 20 "os" 21 22 "github.com/go-openapi/loads" 23 "github.com/go-openapi/loads/fmts" 24 "github.com/go-swagger/go-swagger/cmd/swagger/commands" 25 "github.com/jessevdk/go-flags" 26 ) 27 28 func init() { 29 loads.AddLoader(fmts.YAMLMatcher, fmts.YAMLDoc) 30 } 31 32 var ( 33 // Debug is true when the SWAGGER_DEBUG env var is not empty 34 Debug = os.Getenv("SWAGGER_DEBUG") != "" 35 ) 36 37 var opts struct { 38 // General options applicable to all commands 39 Quiet func() `long:"quiet" short:"q" description:"silence logs"` 40 LogFile func(string) `long:"output" short:"o" description:"redirect logs to file" value-name:"LOG-FILE"` 41 // Version bool `long:"version" short:"v" description:"print the version of the command"` 42 } 43 44 func main() { 45 // TODO: reactivate 'defer catch all' once product is stable 46 // Recovering from internal panics 47 // Stack may be printed in Debug mode 48 // Need import "runtime/debug". 49 //defer func() { 50 // r := recover() 51 // if r != nil { 52 // log.Printf("Fatal error:", r) 53 // if Debug { 54 // debug.PrintStack() 55 // } 56 // os.Exit(1) 57 // } 58 //}() 59 60 parser := flags.NewParser(&opts, flags.Default) 61 parser.ShortDescription = "helps you keep your API well described" 62 parser.LongDescription = ` 63 Swagger tries to support you as best as possible when building APIs. 64 65 It aims to represent the contract of your API with a language agnostic description of your application in json or yaml. 66 ` 67 _, err := parser.AddCommand("validate", "validate the swagger document", "validate the provided swagger document against a swagger spec", &commands.ValidateSpec{}) 68 if err != nil { 69 log.Fatal(err) 70 } 71 72 _, err = parser.AddCommand("init", "initialize a spec document", "initialize a swagger spec document", &commands.InitCmd{}) 73 if err != nil { 74 log.Fatal(err) 75 } 76 77 _, err = parser.AddCommand("version", "print the version", "print the version of the swagger command", &commands.PrintVersion{}) 78 if err != nil { 79 log.Fatal(err) 80 } 81 82 _, err = parser.AddCommand("serve", "serve spec and docs", "serve a spec and swagger or redoc documentation ui", &commands.ServeCmd{}) 83 if err != nil { 84 log.Fatal(err) 85 } 86 87 _, err = parser.AddCommand("expand", "expand $ref fields in a swagger spec", "expands the $refs in a swagger document to inline schemas", &commands.ExpandSpec{}) 88 if err != nil { 89 log.Fatal(err) 90 } 91 92 _, err = parser.AddCommand("flatten", "flattens a swagger document", "expand the remote references in a spec and move inline schemas to definitions, after flattening there are no complex inlined anymore", &commands.FlattenSpec{}) 93 if err != nil { 94 log.Fatal(err) 95 } 96 97 _, err = parser.AddCommand("mixin", "merge swagger documents", "merge additional specs into first/primary spec by copying their paths and definitions", &commands.MixinSpec{}) 98 if err != nil { 99 log.Fatal(err) 100 } 101 102 genpar, err := parser.AddCommand("generate", "generate go code", "generate go code for the swagger spec file", &commands.Generate{}) 103 if err != nil { 104 log.Fatalln(err) 105 } 106 for _, cmd := range genpar.Commands() { 107 switch cmd.Name { 108 case "spec": 109 cmd.ShortDescription = "generate a swagger spec document from a go application" 110 cmd.LongDescription = cmd.ShortDescription 111 case "client": 112 cmd.ShortDescription = "generate all the files for a client library" 113 cmd.LongDescription = cmd.ShortDescription 114 case "server": 115 cmd.ShortDescription = "generate all the files for a server application" 116 cmd.LongDescription = cmd.ShortDescription 117 case "model": 118 cmd.ShortDescription = "generate one or more models from the swagger spec" 119 cmd.LongDescription = cmd.ShortDescription 120 case "support": 121 cmd.ShortDescription = "generate supporting files like the main function and the api builder" 122 cmd.LongDescription = cmd.ShortDescription 123 case "operation": 124 cmd.ShortDescription = "generate one or more server operations from the swagger spec" 125 cmd.LongDescription = cmd.ShortDescription 126 } 127 } 128 129 opts.Quiet = func() { 130 log.SetOutput(ioutil.Discard) 131 } 132 opts.LogFile = func(logfile string) { 133 f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) 134 if err != nil { 135 log.Fatalf("cannot write to file %s: %v", logfile, err) 136 } 137 log.SetOutput(f) 138 } 139 140 if _, err := parser.Parse(); err != nil { 141 os.Exit(1) 142 } 143 }