예제 #1
0
func diff(aFilePath, bFilePath string, separator string) {
	aFile, err := ioutil.ReadFile(aFilePath)
	if err != nil {
		log.Fatalln(fmt.Sprintf("error reading a [%s]:", path.Clean(aFilePath)), err)
	}

	aYAML, err := yaml.Parse(aFilePath, aFile)
	if err != nil {
		log.Fatalln(fmt.Sprintf("error parsing a [%s]:", path.Clean(aFilePath)), err)
	}

	bFile, err := ioutil.ReadFile(bFilePath)
	if err != nil {
		log.Fatalln(fmt.Sprintf("error reading b [%s]:", path.Clean(bFilePath)), err)
	}

	bYAML, err := yaml.Parse(bFilePath, bFile)
	if err != nil {
		log.Fatalln(fmt.Sprintf("error parsing b [%s]:", path.Clean(bFilePath)), err)
	}

	diffs := compare.Compare(aYAML, bYAML)

	if len(diffs) == 0 {
		fmt.Println("no differences!")
		return
	}

	for _, diff := range diffs {
		fmt.Println("Difference in", strings.Join(diff.Path, "."))

		if diff.A != nil {
			ayaml, err := candiedyaml.Marshal(diff.A)
			if err != nil {
				panic(err)
			}

			fmt.Printf("  %s has:\n    \x1b[31m%s\x1b[0m\n", aFilePath, strings.Replace(string(ayaml), "\n", "\n    ", -1))
		}

		if diff.B != nil {
			byaml, err := candiedyaml.Marshal(diff.B)
			if err != nil {
				panic(err)
			}

			fmt.Printf("  %s has:\n    \x1b[32m%s\x1b[0m\n", bFilePath, strings.Replace(string(byaml), "\n", "\n    ", -1))
		}

		fmt.Printf(separator)
	}
}
예제 #2
0
파일: exec.go 프로젝트: gstackio/spiff
func func_exec(arguments []interface{}, binding Binding) (yaml.Node, EvaluationInfo, bool) {
	info := DefaultInfo()

	if len(arguments) < 1 {
		return nil, info, false
	}
	args := []string{}
	debug.Debug("exec: found %d arguments for call\n", len(arguments))
	for i, arg := range arguments {
		list, ok := arg.([]yaml.Node)
		if i == 0 && ok {
			debug.Debug("exec: found array as first argument\n")
			if len(arguments) == 1 && len(list) > 0 {
				// handle single list argument to gain command and argument
				for j, arg := range list {
					v, ok := getArg(j, arg.Value())
					if !ok {
						info.Issue = "command argument must be string"
						return nil, info, false
					}
					args = append(args, v)
				}
			} else {
				info.Issue = "list not allowed for command argument"
				return nil, info, false
			}
		} else {
			v, ok := getArg(i, arg)
			if !ok {
				info.Issue = "command argument must be string"
				return nil, info, false
			}
			args = append(args, v)
		}
	}
	result, err := cachedExecute(args)
	if err != nil {
		info.Issue = "execution '" + args[0] + "' failed"
		// expression set to undefined
		return nil, info, false
	}

	str := string(result)
	execYML, err := yaml.Parse("exec", result)
	if strings.HasPrefix(str, "---\n") && err == nil {
		debug.Debug("exec: found yaml result %+v\n", execYML)
		return execYML, info, true
	} else {
		if strings.HasSuffix(str, "\n") {
			str = str[:len(str)-1]
		}
		int64YML, err := strconv.ParseInt(str, 10, 64)
		if err == nil {
			debug.Debug("exec: found integer result: %s\n", int64YML)
			return node(int64YML), info, true
		}
		debug.Debug("exec: found string result: %s\n", string(result))
		return node(str), info, true
	}
}
예제 #3
0
파일: init_test.go 프로젝트: gstackio/spiff
func parseYAML(source string) yaml.Node {
	parsed, err := yaml.Parse("dynaml test", []byte(source))
	if err != nil {
		panic(err)
	}

	return parsed
}
예제 #4
0
파일: init_test.go 프로젝트: gstackio/spiff
func parseYAMLFrom(source string, name string) yaml.Node {
	parsed, err := yaml.Parse(name, []byte(source))
	if err != nil {
		panic(err)
	}

	return parsed
}
예제 #5
0
파일: spiff.go 프로젝트: rdgallagher/spiff
func merge(templateFilePath string, stubFilePaths []string) {
	templateFile, err := ioutil.ReadFile(templateFilePath)
	if err != nil {
		log.Fatalln("error reading template:", err)
	}

	templateYAML, err := yaml.Parse(templateFilePath, templateFile)
	if err != nil {
		log.Fatalln("error parsing template:", err)
	}

	stubs := []yaml.Node{}

	for _, stubFilePath := range stubFilePaths {
		stubFile, err := ioutil.ReadFile(stubFilePath)
		if err != nil {
			log.Fatalln("error reading stub:", err)
		}

		stubYAML, err := yaml.Parse(stubFilePath, stubFile)
		if err != nil {
			log.Fatalln("error parsing stub:", err)
		}

		stubs = append(stubs, stubYAML)
	}

	flowed, err := flow.Cascade(templateYAML, stubs...)
	if err != nil {
		log.Fatalln("error generating manifest:", err)
	}

	yaml, err := candiedyaml.Marshal(flowed)
	if err != nil {
		log.Fatalln("error marshalling manifest:", err)
	}

	fmt.Println(string(yaml))
}