| 10 | ) |
| 11 | |
| 12 | func Load(data []byte, configFormat string, defaultAppName string) (*Config, error) { |
| 13 | conf := &Config{} |
| 14 | configFormat = strings.ToLower(configFormat) |
| 15 | switch configFormat { |
| 16 | case flags.GlobalFlagsConfigFileFormatYAML: |
| 17 | if err := conf.FromYAML(data); err != nil { |
| 18 | return nil, fmt.Errorf("Failed to read configuration in YAML: %s\n", err.Error()) |
| 19 | } |
| 20 | case flags.GlobalFlagsConfigFileFormatJSON: |
| 21 | if err := conf.FromJSON(data); err != nil { |
| 22 | return nil, fmt.Errorf("Failed to read configuration in JSON: %s\n", err.Error()) |
| 23 | } |
| 24 | default: |
| 25 | return nil, fmt.Errorf("Unsupported configuration format [%s]", configFormat) |
| 26 | } |
| 27 | |
| 28 | // env |
| 29 | if conf.Env == nil { |
| 30 | conf.Env = make(map[string]string) |
| 31 | } |
| 32 | |
| 33 | // merge with defaults: defaultAppName is used only if loaded configuration does not have app name |
| 34 | appName := conv.S(conf.Name) |
| 35 | if appName == "" { |
| 36 | appName = defaultAppName |
| 37 | } |
| 38 | conf.Defaults(DefaultConfig(appName)) |
| 39 | |
| 40 | // validation |
| 41 | if err := conf.Validate(); err != nil { |
| 42 | return nil, core.NewErrorExtraInfo(err, "https://github.com/coldbrewcloud/coldbrew-cli/wiki/Configuration-File") |
| 43 | } |
| 44 | |
| 45 | return conf, nil |
| 46 | } |
| 47 | |
| 48 | func (c *Config) Defaults(source *Config) { |
| 49 | if source == nil { |