github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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" 19 "log" 20 "os" 21 22 flags "github.com/jessevdk/go-flags" 23 "github.com/thetreep/go-swagger/cmd/swagger/commands" 24 ) 25 26 var opts struct { 27 // General options applicable to all commands 28 Quiet func() `long:"quiet" short:"q" description:"silence logs"` 29 LogFile func(string) `long:"log-output" description:"redirect logs to file" value-name:"LOG-FILE"` 30 // Version bool `long:"version" short:"v" description:"print the version of the command"` 31 } 32 33 func main() { 34 // TODO: reactivate 'defer catch all' once product is stable 35 // Recovering from internal panics 36 // Stack may be printed in Debug mode 37 // Need import "runtime/debug". 38 // defer func() { 39 // r := recover() 40 // if r != nil { 41 // log.Printf("Fatal error:", r) 42 // if Debug { 43 // debug.PrintStack() 44 // } 45 // os.Exit(1) 46 // } 47 // }() 48 49 parser := flags.NewParser(&opts, flags.Default) 50 parser.ShortDescription = "helps you keep your API well described" 51 parser.LongDescription = ` 52 Swagger tries to support you as best as possible when building APIs. 53 54 It aims to represent the contract of your API with a language agnostic description of your application in json or yaml. 55 ` 56 _, err := parser.AddCommand( 57 "validate", 58 "validate the swagger document", 59 "validate the provided swagger document against a swagger spec", 60 &commands.ValidateSpec{}, 61 ) 62 if err != nil { 63 log.Fatal(err) 64 } 65 66 _, err = parser.AddCommand("init", "initialize a spec document", "initialize a swagger spec document", &commands.InitCmd{}) 67 if err != nil { 68 log.Fatal(err) 69 } 70 71 _, err = parser.AddCommand("version", "print the version", "print the version of the swagger command", &commands.PrintVersion{}) 72 if err != nil { 73 log.Fatal(err) 74 } 75 76 _, err = parser.AddCommand("serve", "serve spec and docs", "serve a spec and swagger or redoc documentation ui", &commands.ServeCmd{}) 77 if err != nil { 78 log.Fatal(err) 79 } 80 81 _, err = parser.AddCommand( 82 "expand", 83 "expand $ref fields in a swagger spec", 84 "expands the $refs in a swagger document to inline schemas", 85 &commands.ExpandSpec{}, 86 ) 87 if err != nil { 88 log.Fatal(err) 89 } 90 91 _, err = parser.AddCommand( 92 "flatten", 93 "flattens a swagger document", 94 "expand the remote references in a spec and move inline schemas to definitions, after flattening there are no complex inlined anymore", 95 &commands.FlattenSpec{}, 96 ) 97 if err != nil { 98 log.Fatal(err) 99 } 100 101 _, err = parser.AddCommand( 102 "mixin", 103 "merge swagger documents", 104 "merge additional specs into first/primary spec by copying their paths and definitions", 105 &commands.MixinSpec{}, 106 ) 107 if err != nil { 108 log.Fatal(err) 109 } 110 111 _, err = parser.AddCommand( 112 "diff", 113 "diff swagger documents", 114 "diff specs showing which changes will break existing clients", 115 &commands.DiffCommand{}, 116 ) 117 if err != nil { 118 log.Fatal(err) 119 } 120 121 genpar, err := parser.AddCommand("generate", "generate go code", "generate go code for the swagger spec file", &commands.Generate{}) 122 if err != nil { 123 log.Fatalln(err) 124 } 125 for _, cmd := range genpar.Commands() { 126 switch cmd.Name { 127 case "spec": 128 cmd.ShortDescription = "generate a swagger spec document from a go application" 129 cmd.LongDescription = cmd.ShortDescription 130 case "client": 131 cmd.ShortDescription = "generate all the files for a client library" 132 cmd.LongDescription = cmd.ShortDescription 133 case "server": 134 cmd.ShortDescription = "generate all the files for a server application" 135 cmd.LongDescription = cmd.ShortDescription 136 case "model": 137 cmd.ShortDescription = "generate one or more models from the swagger spec" 138 cmd.LongDescription = cmd.ShortDescription 139 case "support": 140 cmd.ShortDescription = "generate supporting files like the main function and the api builder" 141 cmd.LongDescription = cmd.ShortDescription 142 case "operation": 143 cmd.ShortDescription = "generate one or more server operations from the swagger spec" 144 cmd.LongDescription = cmd.ShortDescription 145 case "markdown": 146 cmd.ShortDescription = "generate a markdown representation from the swagger spec" 147 cmd.LongDescription = cmd.ShortDescription 148 case "cli": 149 cmd.ShortDescription = "generate a command line client tool from the swagger spec" 150 cmd.LongDescription = cmd.ShortDescription 151 } 152 } 153 154 opts.Quiet = func() { 155 log.SetOutput(io.Discard) 156 } 157 opts.LogFile = func(logfile string) { 158 f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) 159 if err != nil { 160 log.Fatalf("cannot write to file %s: %v", logfile, err) 161 } 162 log.SetOutput(f) 163 } 164 165 if _, err := parser.Parse(); err != nil { 166 os.Exit(1) 167 } 168 }