github.com/mobiledgex/go-swagger@v0.19.0/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/go-swagger/go-swagger/generator"
    22  )
    23  
    24  // Operation the generate operation files command
    25  type Operation struct {
    26  	shared
    27  	Name           []string `long:"name" short:"n" required:"true" description:"the operations to generate, repeat for multiple"`
    28  	Tags           []string `long:"tags" description:"the tags to include, if not specified defaults to all"`
    29  	Principal      string   `short:"P" long:"principal" description:"the model to use for the security principal"`
    30  	DefaultScheme  string   `long:"default-scheme" description:"the default scheme for this API" default:"http"`
    31  	NoHandler      bool     `long:"skip-handler" description:"when present will not generate an operation handler"`
    32  	NoStruct       bool     `long:"skip-parameters" description:"when present will not generate the parameter model struct"`
    33  	NoResponses    bool     `long:"skip-responses" description:"when present will not generate the response model struct"`
    34  	NoURLBuilder   bool     `long:"skip-url-builder" description:"when present will not generate a URL builder"`
    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 (o *Operation) getOpts() (*generator.GenOpts, error) {
    40  	return &generator.GenOpts{
    41  		Spec:              string(o.Spec),
    42  		Target:            string(o.Target),
    43  		APIPackage:        o.APIPackage,
    44  		ModelPackage:      o.ModelPackage,
    45  		ServerPackage:     o.ServerPackage,
    46  		ClientPackage:     o.ClientPackage,
    47  		Principal:         o.Principal,
    48  		DumpData:          o.DumpData,
    49  		DefaultScheme:     o.DefaultScheme,
    50  		TemplateDir:       string(o.TemplateDir),
    51  		IncludeHandler:    !o.NoHandler,
    52  		IncludeResponses:  !o.NoResponses,
    53  		IncludeParameters: !o.NoStruct,
    54  		IncludeURLBuilder: !o.NoURLBuilder,
    55  		Tags:              o.Tags,
    56  		ValidateSpec:      !o.SkipValidation,
    57  	}, nil
    58  }
    59  
    60  func (o *Operation) getShared() *shared {
    61  	return &o.shared
    62  }
    63  
    64  func (o *Operation) generate(opts *generator.GenOpts) error {
    65  	return generator.GenerateServerOperation(o.Name, opts)
    66  }
    67  
    68  func (o *Operation) log(rp string) {
    69  
    70  	log.Printf(`Generation completed!
    71  
    72  For this generation to compile you need to have some packages in your GOPATH:
    73  
    74  	* github.com/go-openapi/runtime
    75  
    76  You can get these now with: go get -u -f %s/...
    77  `, rp)
    78  }
    79  
    80  // Execute generates a model file
    81  func (o *Operation) Execute(args []string) error {
    82  	if o.DumpData && len(o.Name) > 1 {
    83  		return errors.New("only 1 operation at a time is supported for dumping data")
    84  	}
    85  
    86  	return createSwagger(o)
    87  }