Example #1
0
func PrepareCommandForRecipe(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (*exec.Cmd, error) {
	pattern := properties[recipe]
	if pattern == constants.EMPTY_STRING {
		return nil, i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe)
	}

	var err error
	commandLine := properties.ExpandPropsInString(pattern)
	if removeUnsetProperties {
		commandLine, err = props.DeleteUnexpandedPropsFromString(commandLine)
		if err != nil {
			return nil, i18n.WrapError(err)
		}
	}

	command, err := utils.PrepareCommand(commandLine, logger)
	if err != nil {
		return nil, i18n.WrapError(err)
	}

	if echoCommandLine {
		fmt.Println(commandLine)
	}

	return command, nil
}
func TestDeleteUnexpandedPropsFromString2(t *testing.T) {
	aMap := make(props.PropertiesMap)
	aMap["key2"] = "42"

	str := "{key1} == {key2} == {key3} == true"

	str = aMap.ExpandPropsInString(str)
	str, err := props.DeleteUnexpandedPropsFromString(str)
	NoError(t, err)
	require.Equal(t, " == 42 ==  == true", str)
}
func TestDeleteUnexpandedPropsFromString(t *testing.T) {
	aMap := make(map[string]string)
	aMap["key1"] = "42"
	aMap["key2"] = "{key1}"

	str := "{key1} == {key2} == {key3} == true"

	str = props.ExpandPropsInString(aMap, str)
	str, err := props.DeleteUnexpandedPropsFromString(str)
	NoError(t, err)
	require.Equal(t, "42 == 42 ==  == true", str)
}
Example #4
0
func ExecRecipe(properties map[string]string, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) ([]byte, error) {
	pattern := properties[recipe]
	if pattern == constants.EMPTY_STRING {
		return nil, utils.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe)
	}

	var err error
	commandLine := props.ExpandPropsInString(properties, pattern)
	if removeUnsetProperties {
		commandLine, err = props.DeleteUnexpandedPropsFromString(commandLine)
		if err != nil {
			return nil, utils.WrapError(err)
		}
	}

	command, err := utils.PrepareCommand(commandLine, logger)
	if err != nil {
		return nil, utils.WrapError(err)
	}

	if echoCommandLine {
		fmt.Println(commandLine)
	}

	if echoOutput {
		command.Stdout = os.Stdout
	}

	command.Stderr = os.Stderr

	if echoOutput {
		err := command.Run()
		return nil, utils.WrapError(err)
	}

	bytes, err := command.Output()
	return bytes, utils.WrapError(err)
}