Exemplo n.º 1
0
// Reader from os.Open
func loadConfigContents(reader io.Reader, workingDir string, logger common.Logger) (Config, error) {
	log := common.MaskLogger(logger)
	log.Printf("Loading config with working dir %v.\n", workingDir)

	buf := new(bytes.Buffer)
	_, err := buf.ReadFrom(reader)
	if err != nil {
		return Config{}, errors.Wrap(err, "could not read config")
	}

	data := buf.Bytes()
	var config Config
	err = json.Unmarshal(data, &config)
	if err != nil {
		if syntax, ok := err.(*json.SyntaxError); ok && syntax.Offset != 0 {
			start := strings.LastIndex(string(data[:syntax.Offset]), "\n") + 1
			line, pos := strings.Count(string(data[:start]), "\n")+1, int(syntax.Offset)-start-1
			return Config{}, errors.Wrapf(err, "could not parse config file (line %v, char %v)", line, pos)
		}
		return Config{}, errors.Wrap(err, "could not parse config file")
	}

	config.workingDir = workingDir

	err = config.loadImports()
	if err != nil {
		return Config{}, errors.WithStack(err)
	}

	config.Logger = log

	return config, nil
}
Exemplo n.º 2
0
func NewConfig(newServices []services.ServiceConfig, newGroups []services.ServiceGroupConfig, logger common.Logger) Config {

	log := common.MaskLogger(logger)
	log.Printf("Creating new config with %d services and %d groups.\n", len(newServices), len(newGroups))

	// Find Env settings common to all services
	var allEnvSlices [][]string
	for _, s := range newServices {
		allEnvSlices = append(allEnvSlices, s.Env)
	}
	env := stringSliceIntersect(allEnvSlices)

	// Remove common settings from services
	var svcs []services.ServiceConfig
	for _, s := range newServices {
		s.Env = stringSliceRemoveCommon(env, s.Env)
		svcs = append(svcs, s)
	}

	cfg := Config{
		Env:      env,
		Services: svcs,
		Groups:   []GroupDef{},
		Logger:   log,
	}

	cfg.AddGroups(newGroups)

	log.Printf("Config created: %v", cfg)

	return cfg
}
Exemplo n.º 3
0
func EmptyConfig(workingDir string, logger common.Logger) Config {

	log := common.MaskLogger(logger)
	log.Printf("Creating empty config\n")

	cfg := Config{
		workingDir: workingDir,
		Logger:     log,
	}

	cfg.ServiceMap = make(map[string]*services.ServiceConfig)
	cfg.GroupMap = make(map[string]*services.ServiceGroupConfig)

	return cfg
}