// Load retrieves a specified configuration file and return a Config struct func Load(file, directories string) *Config { // Load the configuration file conf, err := loadFile(file) if err != nil { logger.Fatal(err) } // Apply default configs to the configuration file if err := mergo.Merge(conf, defaultConfig); err != nil { logger.Fatal(err) } for i := range conf.Sensu { if err := mergo.Merge(&conf.Sensu[i], defaultSensuConfig); err != nil { logger.Fatal(err) } } if directories != "" { configDir := loadDirectories(directories) // Overwrite the file config with the configs from the directories if err := mergo.MergeWithOverwrite(conf, configDir); err != nil { logger.Fatal(err) } } conf.Sensu = initSensu(conf.Sensu) // Support the dashboard attribute if conf.Dashboard != nil { conf.Uchiwa = *conf.Dashboard // Apply the default config to the dashboard attribute if err := mergo.Merge(conf, defaultConfig); err != nil { logger.Fatal(err) } } conf.Uchiwa = initUchiwa(conf.Uchiwa) return conf }
// loadDirectories loads a Config struct from one or multiple directories of configuration func loadDirectories(path string) *Config { conf := new(Config) var configFiles []string directories := strings.Split(strings.ToLower(path), ",") for _, directory := range directories { // Find all JSON files in the specified directories files, err := filepath.Glob(filepath.Join(directory, "*.json")) if err != nil { logger.Warning(err) continue } // Add the files found to a slice of configuration files to open for _, file := range files { configFiles = append(configFiles, file) } } // Load every configuration files and merge them together bit by bit for _, file := range configFiles { // Load the config from the file c, err := loadFile(file) if err != nil { logger.Warning(err) continue } // Apply this configuration to the existing one if err := mergo.MergeWithOverwrite(conf, c); err != nil { logger.Warning(err) continue } } // Apply the default config to the Sensu APIs for i := range conf.Sensu { if err := mergo.Merge(&conf.Sensu[i], defaultSensuConfig); err != nil { logger.Fatal(err) } } return conf }