github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/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  type clientOptions struct {
    24  	ClientPackage string `long:"client-package" short:"c" description:"the package to save the client specific code" default:"client"`
    25  }
    26  
    27  func (co clientOptions) apply(opts *generator.GenOpts) {
    28  	opts.ClientPackage = co.ClientPackage
    29  }
    30  
    31  // Client the command to generate a swagger client
    32  type Client struct {
    33  	WithShared
    34  	WithModels
    35  	WithOperations
    36  
    37  	clientOptions
    38  	schemeOptions
    39  	mediaOptions
    40  
    41  	SkipModels     bool `long:"skip-models" description:"no models will be generated when this flag is specified"`
    42  	SkipOperations bool `long:"skip-operations" description:"no operations will be generated when this flag is specified"`
    43  
    44  	Name string `long:"name" short:"A" description:"the name of the application, defaults to a mangled value of info.title"`
    45  }
    46  
    47  func (c Client) apply(opts *generator.GenOpts) {
    48  	c.Shared.apply(opts)
    49  	c.Models.apply(opts)
    50  	c.Operations.apply(opts)
    51  	c.clientOptions.apply(opts)
    52  	c.schemeOptions.apply(opts)
    53  	c.mediaOptions.apply(opts)
    54  
    55  	opts.IncludeModel = !c.SkipModels
    56  	opts.IncludeValidator = !c.SkipModels
    57  	opts.IncludeHandler = !c.SkipOperations
    58  	opts.IncludeParameters = !c.SkipOperations
    59  	opts.IncludeResponses = !c.SkipOperations
    60  	opts.Name = c.Name
    61  
    62  	opts.IsClient = true
    63  	opts.IncludeSupport = true
    64  }
    65  
    66  func (c *Client) generate(opts *generator.GenOpts) error {
    67  	return generator.GenerateClient(c.Name, c.Models.Models, c.Operations.Operations, opts)
    68  }
    69  
    70  func (c *Client) log(rp string) {
    71  	log.Println(`Generation completed!
    72  
    73  For this generation to compile you need to have some packages in your go.mod:
    74  
    75  	* github.com/go-openapi/errors
    76  	* github.com/go-openapi/runtime
    77  	* github.com/go-openapi/runtime/client
    78  	* github.com/go-openapi/strfmt
    79  
    80  You can get these now with: go mod tidy`)
    81  }
    82  
    83  // Execute runs this command
    84  func (c *Client) Execute(args []string) error {
    85  	return createSwagger(c)
    86  }