func execCmdInDirWithAction()

in pkg/commandhandler/cmd.go [43:86]


func execCmdInDirWithAction(cmd, workingDir, logDir string, waitForCompletion bool, el logging.ILogger, params *map[string]string) (int, error) {
	var exitCode int
	var execErr error
	err := os.MkdirAll(workingDir, constants.FilePermissions_UserOnly_ReadWriteExecute)
	if err != nil {
		return -1, errors.Wrapf(err, "error while creating/accessing directory %s", workingDir)
	}

	if waitForCompletion {
		outFileName, errFileName := logPaths(logDir)
		outF, err := os.OpenFile(outFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, constants.FilePermissions_UserOnly_ReadWrite)
		if err != nil {
			return -1, errors.Wrapf(err, "failed to open stdout file")
		}

		errF, err := os.OpenFile(errFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, constants.FilePermissions_UserOnly_ReadWrite)
		if err != nil {
			return -1, errors.Wrapf(err, "failed to open stderr file")
		}

		exitCode, execErr = execWaitFunctionWithParams(cmd, workingDir, outF, errF, params)

		// add the output of the command to the log file
		el.Info("command: %s", cmd)
		stdOutFile, err2 := os.OpenFile(outFileName, os.O_RDONLY, constants.FilePermissions_UserOnly_ReadWrite)
		if err2 == nil {
			el.InfoFromStream("stdout:", stdOutFile)
			stdOutFile.Close()
		}

		stdErrFile, err3 := os.OpenFile(errFileName, os.O_RDONLY, constants.FilePermissions_UserOnly_ReadWrite)
		if err3 == nil {
			el.InfoFromStream("stderr:", stdErrFile)
			stdErrFile.Close()
		}

	} else {

		exitCode, execErr = execDontWaitFunctionWithParams(cmd, workingDir, params)

	}

	return exitCode, execErr
}