You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package generator
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/spf13/viper"
  7. )
  8. // LanguageDefinition in the configuration file.
  9. type LanguageDefinition struct {
  10. Layout SectionOpts `mapstructure:"layout"`
  11. }
  12. // ConfigureOpts for generation
  13. func (d *LanguageDefinition) ConfigureOpts(opts *GenOpts) error {
  14. opts.Sections = d.Layout
  15. if opts.LanguageOpts == nil {
  16. opts.LanguageOpts = GoLangOpts()
  17. }
  18. return nil
  19. }
  20. // LanguageConfig structure that is obtained from parsing a config file
  21. type LanguageConfig map[string]LanguageDefinition
  22. // ReadConfig at the specified path, when no path is specified it will look into
  23. // the current directory and load a .swagger.{yml,json,hcl,toml,properties} file
  24. // Returns a viper config or an error
  25. func ReadConfig(fpath string) (*viper.Viper, error) {
  26. v := viper.New()
  27. if fpath != "" {
  28. if !fileExists(fpath, "") {
  29. return nil, fmt.Errorf("can't find file for %q", fpath)
  30. }
  31. file, err := os.Open(fpath)
  32. if err != nil {
  33. return nil, err
  34. }
  35. defer func() { _ = file.Close() }()
  36. ext := filepath.Ext(fpath)
  37. if len(ext) > 0 {
  38. ext = ext[1:]
  39. }
  40. v.SetConfigType(ext)
  41. if err := v.ReadConfig(file); err != nil {
  42. return nil, err
  43. }
  44. return v, nil
  45. }
  46. v.SetConfigName(".swagger")
  47. v.AddConfigPath(".")
  48. if err := v.ReadInConfig(); err != nil {
  49. if _, ok := err.(viper.UnsupportedConfigError); !ok && v.ConfigFileUsed() != "" {
  50. return nil, err
  51. }
  52. }
  53. return v, nil
  54. }