github.com/circl-dev/go-swagger@v0.31.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/circl-dev/go-swagger/generator"
    22  )
    23  
    24  type serverOptions struct {
    25  	ServerPackage         string `long:"server-package" short:"s" description:"the package to save the server specific code" default:"restapi"`
    26  	MainTarget            string `long:"main-package" short:"" description:"the location of the generated main. Defaults to cmd/{name}-server" default:""`
    27  	ImplementationPackage string `long:"implementation-package" short:"" description:"the location of the backend implementation of the server, which will be autowired with api" default:""`
    28  }
    29  
    30  func (cs serverOptions) apply(opts *generator.GenOpts) {
    31  	opts.ServerPackage = cs.ServerPackage
    32  }
    33  
    34  // Server the command to generate an entire server application
    35  type Server struct {
    36  	WithShared
    37  	WithModels
    38  	WithOperations
    39  
    40  	serverOptions
    41  	schemeOptions
    42  	mediaOptions
    43  
    44  	SkipModels             bool   `long:"skip-models" description:"no models will be generated when this flag is specified"`
    45  	SkipOperations         bool   `long:"skip-operations" description:"no operations will be generated when this flag is specified"`
    46  	SkipSupport            bool   `long:"skip-support" description:"no supporting files will be generated when this flag is specified"`
    47  	ExcludeMain            bool   `long:"exclude-main" description:"exclude main function, so just generate the library"`
    48  	ExcludeSpec            bool   `long:"exclude-spec" description:"don't embed the swagger specification"`
    49  	FlagStrategy           string `long:"flag-strategy" description:"the strategy to provide flags for the server" default:"go-flags" choice:"go-flags" choice:"pflag" choice:"flag"` // nolint: staticcheck
    50  	CompatibilityMode      string `long:"compatibility-mode" description:"the compatibility mode for the tls server" default:"modern" choice:"modern" choice:"intermediate"`          // nolint: staticcheck
    51  	RegenerateConfigureAPI bool   `long:"regenerate-configureapi" description:"Force regeneration of configureapi.go"`
    52  
    53  	Name string `long:"name" short:"A" description:"the name of the application, defaults to a mangled value of info.title"`
    54  	// TODO(fredbi): CmdName string `long:"cmd-name" short:"A" description:"the name of the server command, when main is generated (defaults to {name}-server)"`
    55  
    56  	// deprecated flags
    57  	WithContext bool `long:"with-context" description:"handlers get a context as first arg (deprecated)"`
    58  }
    59  
    60  func (s Server) apply(opts *generator.GenOpts) {
    61  	if s.WithContext {
    62  		log.Printf("warning: deprecated option --with-context is ignored")
    63  	}
    64  
    65  	s.Shared.apply(opts)
    66  	s.Models.apply(opts)
    67  	s.Operations.apply(opts)
    68  	s.serverOptions.apply(opts)
    69  	s.schemeOptions.apply(opts)
    70  	s.mediaOptions.apply(opts)
    71  
    72  	opts.IncludeModel = !s.SkipModels
    73  	opts.IncludeValidator = !s.SkipModels
    74  	opts.IncludeHandler = !s.SkipOperations
    75  	opts.IncludeParameters = !s.SkipOperations
    76  	opts.IncludeResponses = !s.SkipOperations
    77  	opts.IncludeURLBuilder = !s.SkipOperations
    78  	opts.IncludeSupport = !s.SkipSupport
    79  	opts.IncludeMain = !s.ExcludeMain
    80  	opts.FlagStrategy = s.FlagStrategy
    81  	opts.CompatibilityMode = s.CompatibilityMode
    82  	opts.RegenerateConfigureAPI = s.RegenerateConfigureAPI
    83  
    84  	opts.Name = s.Name
    85  	opts.MainPackage = s.MainTarget
    86  
    87  	opts.ImplementationPackage = s.ImplementationPackage
    88  }
    89  
    90  func (s *Server) generate(opts *generator.GenOpts) error {
    91  	return generator.GenerateServer(s.Name, s.Models.Models, s.Operations.Operations, opts)
    92  }
    93  
    94  func (s Server) log(rp string) {
    95  	var flagsPackage string
    96  	switch {
    97  	case strings.HasPrefix(s.FlagStrategy, "pflag"):
    98  		flagsPackage = "github.com/spf13/pflag"
    99  	case strings.HasPrefix(s.FlagStrategy, "flag"):
   100  		flagsPackage = "flag"
   101  	default:
   102  		flagsPackage = "github.com/jessevdk/go-flags"
   103  	}
   104  
   105  	log.Printf(`Generation completed!
   106  
   107  For this generation to compile you need to have some packages in your GOPATH:
   108  
   109  	* github.com/circl-dev/runtime
   110  	* `+flagsPackage+`
   111  
   112  You can get these now with: go get -u -f %s/...
   113  `, rp)
   114  }
   115  
   116  // Execute runs this command
   117  func (s *Server) Execute(args []string) error {
   118  	return createSwagger(s)
   119  }