1package generate
2
3import (
4	"log"
5	"os"
6	"path/filepath"
7
8	"github.com/go-swagger/go-swagger/generator"
9	flags "github.com/jessevdk/go-flags"
10	"github.com/spf13/viper"
11)
12
13type shared struct {
14	Spec          flags.Filename `long:"spec" short:"f" description:"the spec file to use (default swagger.{json,yml,yaml})"`
15	APIPackage    string         `long:"api-package" short:"a" description:"the package to save the operations" default:"operations"`
16	ModelPackage  string         `long:"model-package" short:"m" description:"the package to save the models" default:"models"`
17	ServerPackage string         `long:"server-package" short:"s" description:"the package to save the server specific code" default:"restapi"`
18	ClientPackage string         `long:"client-package" short:"c" description:"the package to save the client specific code" default:"client"`
19	Target        flags.Filename `long:"target" short:"t" default:"./" description:"the base directory for generating the files"`
20	TemplateDir   flags.Filename `long:"template-dir" short:"T" description:"alternative template override directory"`
21	ConfigFile    flags.Filename `long:"config-file" short:"C" description:"configuration file to use for overriding template options"`
22}
23
24func readConfig(filename string) (*viper.Viper, error) {
25	if filename == "" {
26		return nil, nil
27	}
28
29	abspath, err := filepath.Abs(filename)
30	if err != nil {
31		log.Fatalln(err)
32	}
33	log.Println("trying to read config from", abspath)
34	return generator.ReadConfig(abspath)
35}
36
37func configureOptsFromConfig(cfg *viper.Viper, opts *generator.GenOpts) error {
38	if cfg == nil {
39		return nil
40	}
41
42	var def generator.LanguageDefinition
43	if err := cfg.Unmarshal(&def); err != nil {
44		return err
45	}
46	def.ConfigureOpts(opts)
47	return nil
48}
49
50func setDebug(cfg *viper.Viper) {
51	if os.Getenv("DEBUG") != "" || os.Getenv("SWAGGER_DEBUG") != "" {
52		if cfg != nil {
53			cfg.Debug()
54		} else {
55			log.Println("NO config read")
56		}
57	}
58}
59