Exemplo n.º 1
0
func (atcConfig ATCConfig) newConfig(configPath PathFlag, templateVariablesFiles []PathFlag, templateVariables template.Variables) atc.Config {
	configFile, err := ioutil.ReadFile(string(configPath))
	if err != nil {
		failWithErrorf("could not read config file", err)
	}

	var resultVars template.Variables

	for _, path := range templateVariablesFiles {
		fileVars, templateErr := template.LoadVariablesFromFile(string(path))
		if templateErr != nil {
			failWithErrorf("failed to load variables from file (%s)", templateErr, string(path))
		}

		resultVars = resultVars.Merge(fileVars)
	}

	resultVars = resultVars.Merge(templateVariables)

	configFile, err = template.Evaluate(configFile, resultVars)
	if err != nil {
		failWithErrorf("failed to evaluate variables into template", err)
	}

	var newConfig atc.Config
	err = yaml.Unmarshal(configFile, &newConfig)
	if err != nil {
		failWithErrorf("failed to parse configuration file", err)
	}

	return newConfig
}
Exemplo n.º 2
0
func (atcConfig ATCConfig) newConfig(configPath string, templateVariablesFiles []string, templateVariables []string) (atc.Config, []byte) {
	configFile, err := ioutil.ReadFile(configPath)
	if err != nil {
		failWithErrorf("could not read config file", err)
	}

	var resultVars template.Variables

	for _, path := range templateVariablesFiles {
		fileVars, err := template.LoadVariablesFromFile(path)
		if err != nil {
			failWithErrorf("failed to load variables from file (%s)", err, path)
		}

		resultVars = resultVars.Merge(fileVars)
	}

	vars, err := template.LoadVariables(templateVariables)
	if err != nil {
		failWithErrorf("could not load template variables", err)
	}

	resultVars = resultVars.Merge(vars)

	configFile, err = template.Evaluate(configFile, resultVars)
	if err != nil {
		failWithErrorf("failed to evaluate variables into template", err)
	}

	var newConfig atc.Config
	err = yaml.Unmarshal(configFile, &newConfig)
	if err != nil {
		failWithErrorf("failed to parse configuration file", err)
	}

	fmt.Println("New Config Raw: %+v", newConfig)

	return newConfig, configFile
}
Exemplo n.º 3
0
				"b": "new",
			}

			result := a.Merge(b)

			Expect(result).To(Equal(template.Variables{
				"a": "foo",
				"b": "new",
			}))

		})
	})

	Describe("loading variables from a file", func() {
		It("can load them from a file", func() {
			variables, err := template.LoadVariablesFromFile("fixtures/vars.yml")
			Expect(err).NotTo(HaveOccurred())
			Expect(variables).To(Equal(template.Variables{
				"hello": "world",
			}))

		})

		It("returns an error if the file does not exist", func() {
			_, err := template.LoadVariablesFromFile("fixtures/missing.yml")
			Expect(err).To(HaveOccurred())
		})

		It("returns an error if the file is in an invalid format", func() {
			_, err := template.LoadVariablesFromFile("fixtures/invalid_vars.yml")
			Expect(err).To(HaveOccurred())