in internal/cmds/cmds.go [615:657]
func runCmd(ctx *log.Context, dir string, scriptFilePath string, cfg *handlersettings.HandlerSettings, metadata types.RCMetadata) (err error, exitCode int) {
ctx.Log("event", "executing command", "output", dir)
var scenario string
// If script is specified - use it directly for command
if cfg.Script() != "" {
scenario = "embedded-script"
// Save the script to a file
scriptFilePath = filepath.Join(dir, "script.sh")
err := files.SaveScriptFile(scriptFilePath, cfg.Script())
if err != nil {
ctx.Log("event", "failed to save script to file", "error", err, "file", scriptFilePath)
return errors.Wrap(err, "failed to save script to file"), constants.ExitCode_SaveScriptFailed
}
} else if cfg.ScriptURI() != "" {
// If scriptUri is specified then cmd should start it
scenario = "public-scriptUri"
}
ctx.Log("event", "prepare command", "scriptFile", scriptFilePath)
// We need to kill previous extension process if exists before starting a new one.
pid.KillPreviousExtension(ctx, metadata.PidFilePath)
// Store the active process id and start time in case its a long running process that needs to be killed later
// If process exited successfully the pid file is deleted
pid.SaveCurrentPidAndStartTime(metadata.PidFilePath)
defer pid.DeleteCurrentPidAndStartTime(metadata.PidFilePath)
begin := time.Now()
err, exitCode = exec.ExecCmdInDir(ctx, scriptFilePath, dir, cfg)
elapsed := time.Since(begin)
isSuccess := err == nil
telemetryResult("scenario", scenario, isSuccess, elapsed)
if err != nil {
ctx.Log("event", "failed to execute command", "error", err, "output", dir)
return errors.Wrap(err, "failed to execute command"), exitCode
}
ctx.Log("event", "executed command", "output", dir)
return nil, constants.ExitCode_Okay
}