github.com/youyuanwu/go-swagger@v0.19.0/cmd/swagger/commands/generate/client.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 generate 16 17 import ( 18 "log" 19 20 "github.com/go-swagger/go-swagger/generator" 21 ) 22 23 // Client the command to generate a swagger client 24 type Client struct { 25 shared 26 Name string `long:"name" short:"A" description:"the name of the application, defaults to a mangled value of info.title"` 27 Operations []string `long:"operation" short:"O" description:"specify an operation to include, repeat for multiple"` 28 Tags []string `long:"tags" description:"the tags to include, if not specified defaults to all"` 29 Principal string `long:"principal" short:"P" description:"the model to use for the security principal"` 30 Models []string `long:"model" short:"M" description:"specify a model to include, repeat for multiple"` 31 DefaultScheme string `long:"default-scheme" description:"the default scheme for this client" default:"http"` 32 DefaultProduces string `long:"default-produces" description:"the default mime type that API operations produce" default:"application/json"` 33 SkipModels bool `long:"skip-models" description:"no models will be generated when this flag is specified"` 34 SkipOperations bool `long:"skip-operations" description:"no operations will be generated when this flag is specified"` 35 DumpData bool `long:"dump-data" description:"when present dumps the json for the template generator instead of generating files"` 36 SkipValidation bool `long:"skip-validation" description:"skips validation of spec prior to generation"` 37 } 38 39 func (c *Client) getOpts() (*generator.GenOpts, error) { 40 return &generator.GenOpts{ 41 Spec: string(c.Spec), 42 43 Target: string(c.Target), 44 APIPackage: c.APIPackage, 45 ModelPackage: c.ModelPackage, 46 ServerPackage: c.ServerPackage, 47 ClientPackage: c.ClientPackage, 48 Principal: c.Principal, 49 DefaultScheme: c.DefaultScheme, 50 DefaultProduces: c.DefaultProduces, 51 IncludeModel: !c.SkipModels, 52 IncludeValidator: !c.SkipModels, 53 IncludeHandler: !c.SkipOperations, 54 IncludeParameters: !c.SkipOperations, 55 IncludeResponses: !c.SkipOperations, 56 ValidateSpec: !c.SkipValidation, 57 Tags: c.Tags, 58 IncludeSupport: true, 59 Template: c.Template, 60 TemplateDir: string(c.TemplateDir), 61 DumpData: c.DumpData, 62 ExistingModels: c.ExistingModels, 63 IsClient: true, 64 }, nil 65 } 66 67 func (c *Client) getShared() *shared { 68 return &c.shared 69 } 70 71 func (c *Client) generate(opts *generator.GenOpts) error { 72 return generator.GenerateClient(c.Name, c.Models, c.Operations, opts) 73 } 74 75 func (c *Client) log(rp string) { 76 log.Printf(`Generation completed! 77 78 For this generation to compile you need to have some packages in your GOPATH: 79 80 * github.com/go-openapi/errors 81 * github.com/go-openapi/runtime 82 * github.com/go-openapi/runtime/client 83 * github.com/go-openapi/strfmt 84 85 You can get these now with: go get -u -f %s/... 86 `, rp) 87 } 88 89 // Execute runs this command 90 func (c *Client) Execute(args []string) error { 91 return createSwagger(c) 92 }