Пример #1
0
func compileTemplateFromFile(context *pongo2.Context, file *string) (*string, error) {
	log.Debug("  Compiling template:\n    %s", *file)

	source, err := util.LoadFileAsString(*file)
	if err != nil {
		log.Error("Error loading template source:\n  %s", *file)
		return nil, err
	}
	log.Trace("-- %s --\n%s\n----", "source", *source)

	return compileTemplateFromString(context, source)
}
Пример #2
0
func compileTemplateFromString(context *pongo2.Context, source *string) (*string, error) {
	template, err := pongo2.FromString(*source)
	if err != nil {
		return nil, errors.New(fmt.Sprintf("Error building template from source\nerror:\n  %s", err.Error()))
	}

	compiled, err := template.Execute(*context)
	if err != nil {
		log.Error("Error loading template from source\n\nerror:\n  %s", err.Error())
		return nil, err
	} else {
		log.Trace("-- %s --\n%s\n----", "compiled", compiled)
		return &compiled, nil
	}
}
Пример #3
0
func createContext(envData *map[string]interface{}, provider *data.Provider, dataFile *string) (*pongo2.Context, error) {
	// Create context
	context := pongo2.Context{
		"env": map[string]interface{}{
			"provider": provider.Name,
		},
	}

	// Default values
	log.Debug("  Applying default data to context")
	context["private_ipv4"] = "$private_ipv4" // Provided by CoreOS
	context["public_ipv4"] = "$public_ipv4"   // Provided by CoreOS

	// Merge environment/provider values
	var data map[string]interface{}

	log.Debug("  Applying environment data to context")
	if err := mergo.Merge(&data, *envData); err != nil {
		return nil, err
	}

	log.Debug("  Applying provider data to context")
	if err := mergo.Merge(&data, provider.Data); err != nil {
		return nil, err
	}

	// Add/Override values from extra data
	if dataFile != nil {
		log.Debug("  Applying extra data to context")

		var extraData map[string]interface{}
		if err := util.LoadYAMLFromFile(*dataFile, &extraData); err != nil {
			log.Error("Error loading/parsing extra data from file:\n  %s", *dataFile)
			return nil, err
		}

		if err := mergo.Merge(&data, extraData); err != nil {
			return nil, err
		}
	}

	// Set result into context
	for k, v := range data {
		context[k] = v
	}

	return &context, nil
}
Пример #4
0
func HandleFatalError(err error) {
	log.Error("\n%s\n", err.Error())
	os.Exit(1)
}