github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/generate/operation.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  	"errors"
    19  	"log"
    20  
    21  	"github.com/thetreep/go-swagger/generator"
    22  )
    23  
    24  type operationOptions struct {
    25  	Operations []string `long:"operation" short:"O" description:"specify an operation to include, repeat for multiple (defaults to all)"`
    26  	Tags       []string `long:"tags" description:"the tags to include, if not specified defaults to all" group:"operations"`
    27  	APIPackage string   `long:"api-package" short:"a" description:"the package to save the operations" default:"operations"`
    28  	WithEnumCI bool     `long:"with-enum-ci" description:"allow case-insensitive enumerations"`
    29  
    30  	// tags handling
    31  	SkipTagPackages bool `long:"skip-tag-packages" description:"skips the generation of tag-based operation packages, resulting in a flat generation"`
    32  }
    33  
    34  func (oo operationOptions) apply(opts *generator.GenOpts) {
    35  	opts.Operations = oo.Operations
    36  	opts.Tags = oo.Tags
    37  	opts.APIPackage = oo.APIPackage
    38  	opts.AllowEnumCI = oo.WithEnumCI
    39  	opts.SkipTagPackages = oo.SkipTagPackages
    40  }
    41  
    42  // WithOperations adds the operations options group
    43  type WithOperations struct {
    44  	Operations operationOptions `group:"Options for operation generation"`
    45  }
    46  
    47  // Operation the generate operation files command
    48  type Operation struct {
    49  	WithShared
    50  	WithOperations
    51  
    52  	clientOptions
    53  	serverOptions
    54  	schemeOptions
    55  	mediaOptions
    56  
    57  	ModelPackage string `long:"model-package" short:"m" description:"the package to save the models" default:"models"`
    58  
    59  	NoHandler    bool `long:"skip-handler" description:"when present will not generate an operation handler"`
    60  	NoStruct     bool `long:"skip-parameters" description:"when present will not generate the parameter model struct"`
    61  	NoResponses  bool `long:"skip-responses" description:"when present will not generate the response model struct"`
    62  	NoURLBuilder bool `long:"skip-url-builder" description:"when present will not generate a URL builder"`
    63  
    64  	Name []string `long:"name" short:"n" description:"the operations to generate, repeat for multiple (defaults to all). Same as --operations"`
    65  }
    66  
    67  func (o Operation) apply(opts *generator.GenOpts) {
    68  	o.Shared.apply(opts)
    69  	o.Operations.apply(opts)
    70  	o.clientOptions.apply(opts)
    71  	o.serverOptions.apply(opts)
    72  	o.schemeOptions.apply(opts)
    73  	o.mediaOptions.apply(opts)
    74  
    75  	opts.ModelPackage = o.ModelPackage
    76  	opts.IncludeHandler = !o.NoHandler
    77  	opts.IncludeResponses = !o.NoResponses
    78  	opts.IncludeParameters = !o.NoStruct
    79  	opts.IncludeURLBuilder = !o.NoURLBuilder
    80  }
    81  
    82  func (o *Operation) generate(opts *generator.GenOpts) error {
    83  	return generator.GenerateServerOperation(append(o.Name, o.Operations.Operations...), opts)
    84  }
    85  
    86  func (o Operation) log(rp string) {
    87  
    88  	log.Println(
    89  		`Generation completed!
    90  
    91  For this generation to compile you need to have some packages in your go.mod:
    92  
    93  	* github.com/go-openapi/runtime
    94  
    95  You can get these now with: go mod tidy`,
    96  	)
    97  }
    98  
    99  // Execute generates a model file
   100  func (o *Operation) Execute(args []string) error {
   101  	if o.Shared.DumpData && len(append(o.Name, o.Operations.Operations...)) > 1 {
   102  		return errors.New("only 1 operation at a time is supported for dumping data")
   103  	}
   104  
   105  	return createSwagger(o)
   106  }