github.com/mobiledgex/go-swagger@v0.19.0/cmd/swagger/commands/generate/server.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  	"strings"
    20  
    21  	"github.com/go-swagger/go-swagger/generator"
    22  )
    23  
    24  // Server the command to generate an entire server application
    25  type Server struct {
    26  	shared
    27  	Name                   string   `long:"name" short:"A" description:"the name of the application, defaults to a mangled value of info.title"`
    28  	Operations             []string `long:"operation" short:"O" description:"specify an operation to include, repeat for multiple"`
    29  	Tags                   []string `long:"tags" description:"the tags to include, if not specified defaults to all"`
    30  	Principal              string   `long:"principal" short:"P" description:"the model to use for the security principal"`
    31  	DefaultScheme          string   `long:"default-scheme" description:"the default scheme for this API" default:"http"`
    32  	Models                 []string `long:"model" short:"M" description:"specify a model to include, repeat for multiple"`
    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  	SkipSupport            bool     `long:"skip-support" description:"no supporting files will be generated when this flag is specified"`
    36  	ExcludeMain            bool     `long:"exclude-main" description:"exclude main function, so just generate the library"`
    37  	ExcludeSpec            bool     `long:"exclude-spec" description:"don't embed the swagger specification"`
    38  	WithContext            bool     `long:"with-context" description:"handlers get a context as first arg (deprecated)"`
    39  	DumpData               bool     `long:"dump-data" description:"when present dumps the json for the template generator instead of generating files"`
    40  	FlagStrategy           string   `long:"flag-strategy" description:"the strategy to provide flags for the server" default:"go-flags" choice:"go-flags" choice:"pflag"`
    41  	CompatibilityMode      string   `long:"compatibility-mode" description:"the compatibility mode for the tls server" default:"modern" choice:"modern" choice:"intermediate"`
    42  	SkipValidation         bool     `long:"skip-validation" description:"skips validation of spec prior to generation"`
    43  	RegenerateConfigureAPI bool     `long:"regenerate-configureapi" description:"Force regeneration of configureapi.go"`
    44  }
    45  
    46  func (s *Server) getOpts() (*generator.GenOpts, error) {
    47  	// warning: deprecation
    48  	if s.WithContext {
    49  		log.Printf("warning: deprecated option --with-context is ignored")
    50  	}
    51  
    52  	return &generator.GenOpts{
    53  		Spec:                   string(s.Spec),
    54  		Target:                 string(s.Target),
    55  		APIPackage:             s.APIPackage,
    56  		ModelPackage:           s.ModelPackage,
    57  		ServerPackage:          s.ServerPackage,
    58  		ClientPackage:          s.ClientPackage,
    59  		Principal:              s.Principal,
    60  		DefaultScheme:          s.DefaultScheme,
    61  		IncludeModel:           !s.SkipModels,
    62  		IncludeValidator:       !s.SkipModels,
    63  		IncludeHandler:         !s.SkipOperations,
    64  		IncludeParameters:      !s.SkipOperations,
    65  		IncludeResponses:       !s.SkipOperations,
    66  		IncludeURLBuilder:      !s.SkipOperations,
    67  		IncludeMain:            !s.ExcludeMain,
    68  		IncludeSupport:         !s.SkipSupport,
    69  		ValidateSpec:           !s.SkipValidation,
    70  		ExcludeSpec:            s.ExcludeSpec,
    71  		Template:               s.Template,
    72  		RegenerateConfigureAPI: s.RegenerateConfigureAPI,
    73  		TemplateDir:            string(s.TemplateDir),
    74  		DumpData:               s.DumpData,
    75  		Models:                 s.Models,
    76  		Operations:             s.Operations,
    77  		Tags:                   s.Tags,
    78  		Name:                   s.Name,
    79  		FlagStrategy:           s.FlagStrategy,
    80  		CompatibilityMode:      s.CompatibilityMode,
    81  		ExistingModels:         s.ExistingModels,
    82  	}, nil
    83  }
    84  
    85  func (s *Server) getShared() *shared {
    86  	return &s.shared
    87  }
    88  
    89  func (s *Server) generate(opts *generator.GenOpts) error {
    90  	return generator.GenerateServer(s.Name, s.Models, s.Operations, opts)
    91  }
    92  
    93  func (s *Server) log(rp string) {
    94  	var flagsPackage string
    95  	if strings.HasPrefix(s.FlagStrategy, "pflag") {
    96  		flagsPackage = "github.com/spf13/pflag"
    97  	} else {
    98  		flagsPackage = "github.com/jessevdk/go-flags"
    99  	}
   100  
   101  	log.Printf(`Generation completed!
   102  
   103  For this generation to compile you need to have some packages in your GOPATH:
   104  
   105  	* github.com/go-openapi/runtime
   106  	* `+flagsPackage+`
   107  
   108  You can get these now with: go get -u -f %s/...
   109  `, rp)
   110  }
   111  
   112  // Execute runs this command
   113  func (s *Server) Execute(args []string) error {
   114  	return createSwagger(s)
   115  }