github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/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 "github.com/go-swagger/go-swagger/cmd/swagger/commands" 23 flags "github.com/jessevdk/go-flags" 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("validate", "validate the swagger document", "validate the provided swagger document against a swagger spec", &commands.ValidateSpec{}) 57 if err != nil { 58 log.Fatal(err) 59 } 60 61 _, err = parser.AddCommand("init", "initialize a spec document", "initialize a swagger spec document", &commands.InitCmd{}) 62 if err != nil { 63 log.Fatal(err) 64 } 65 66 _, err = parser.AddCommand("version", "print the version", "print the version of the swagger command", &commands.PrintVersion{}) 67 if err != nil { 68 log.Fatal(err) 69 } 70 71 _, err = parser.AddCommand("serve", "serve spec and docs", "serve a spec and swagger or redoc documentation ui", &commands.ServeCmd{}) 72 if err != nil { 73 log.Fatal(err) 74 } 75 76 _, err = parser.AddCommand("expand", "expand $ref fields in a swagger spec", "expands the $refs in a swagger document to inline schemas", &commands.ExpandSpec{}) 77 if err != nil { 78 log.Fatal(err) 79 } 80 81 _, 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{}) 82 if err != nil { 83 log.Fatal(err) 84 } 85 86 _, err = parser.AddCommand("mixin", "merge swagger documents", "merge additional specs into first/primary spec by copying their paths and definitions", &commands.MixinSpec{}) 87 if err != nil { 88 log.Fatal(err) 89 } 90 91 _, err = parser.AddCommand("diff", "diff swagger documents", "diff specs showing which changes will break existing clients", &commands.DiffCommand{}) 92 if err != nil { 93 log.Fatal(err) 94 } 95 96 genpar, err := parser.AddCommand("generate", "generate go code", "generate go code for the swagger spec file", &commands.Generate{}) 97 if err != nil { 98 log.Fatalln(err) 99 } 100 for _, cmd := range genpar.Commands() { 101 switch cmd.Name { 102 case "spec": 103 cmd.ShortDescription = "generate a swagger spec document from a go application" 104 cmd.LongDescription = cmd.ShortDescription 105 case "client": 106 cmd.ShortDescription = "generate all the files for a client library" 107 cmd.LongDescription = cmd.ShortDescription 108 case "server": 109 cmd.ShortDescription = "generate all the files for a server application" 110 cmd.LongDescription = cmd.ShortDescription 111 case "model": 112 cmd.ShortDescription = "generate one or more models from the swagger spec" 113 cmd.LongDescription = cmd.ShortDescription 114 case "support": 115 cmd.ShortDescription = "generate supporting files like the main function and the api builder" 116 cmd.LongDescription = cmd.ShortDescription 117 case "operation": 118 cmd.ShortDescription = "generate one or more server operations from the swagger spec" 119 cmd.LongDescription = cmd.ShortDescription 120 case "markdown": 121 cmd.ShortDescription = "generate a markdown representation from the swagger spec" 122 cmd.LongDescription = cmd.ShortDescription 123 case "cli": 124 cmd.ShortDescription = "generate a command line client tool from the swagger spec" 125 cmd.LongDescription = cmd.ShortDescription 126 } 127 } 128 129 opts.Quiet = func() { 130 log.SetOutput(io.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 }