Exemple #1
0
func (compositeSource *CompositeSource) Get() (map[string]interface{}, error) {

	resultEnv := make(map[string]interface{})

	_, err := From(compositeSource.Sources).
		Select(func(rawSource T) (T, error) {
			loader, err := GetSource(rawSource.(map[string]interface{}))

			if err != nil {
				return nil, fmt.Errorf("Failed to parse source: %s", err)
			}

			return loader, nil
		}).
		// Resolve in parallel because some sources might use IO and will take some time
		AsParallel().AsOrdered().
		Select(func(loader T) (T, error) {
			result, err := loader.(Source).Get()

			if err != nil {
				return nil, fmt.Errorf("Failed to resolve source: %s", err)
			}

			return result, nil
		}).
		AsSequential().
		CountBy(func(partialConfig T) (bool, error) {
			for key, value := range flatmap.Flatten(partialConfig.(map[string]interface{})) {
				resultEnv[key] = value
			}
			return true, nil
		})

	return resultEnv, err
}
Exemple #2
0
func main() {
	defer func() {
		if r := recover(); r != nil {
			os.Stderr.WriteString(fmt.Sprintln(r))
			os.Exit(1)
		}
	}()

	if len(os.Args) < 2 {
		panic("the required argument `command` was not provided\n")
	}

	command := strings.Join(os.Args[1:], " ")

	sources := getSources()

	configs := getConfigs(sources)

	sourceKeys := make([]int, 0, len(configs))
	for key := range configs {
		sourceKeys = append(sourceKeys, key)
	}
	sort.Ints(sourceKeys)

	for _, sourceKey := range sourceKeys {
		result := configs[sourceKey]

		if result.err != nil {
			panic(result.err)
		}

		partialConfig := flatmap.Flatten(result.config)

		for key, value := range partialConfig {
			os.Setenv(key, fmt.Sprintf("%v", value))
		}
	}

	envVars := GetEnvironmentVariables()
	for key, value := range envVars {
		if strings.HasPrefix(value, configoPrefix) {
			tmpl, err := template.New(key).Parse(strings.TrimPrefix(value, configoPrefix))

			if err != nil {
				panic(err)
			}

			var buffer bytes.Buffer
			tmpl.Execute(&buffer, envVars)

			os.Setenv(key, buffer.String())
		}
	}

	execute(command)
}