new commands.[name].type: script; update templates

This commit is contained in:
Andrew Woodlee 2024-10-15 08:49:13 -05:00
parent 51f5084dd0
commit 2aeb435167
3 changed files with 51 additions and 12 deletions

View File

@ -100,15 +100,61 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
// Is command type defined. That is, is it local or not // Is command type defined. That is, is it local or not
if command.Type != "" { if command.Type != "" {
var (
script *bytes.Buffer
buffer bytes.Buffer
scriptEnvFileBuffer bytes.Buffer
scriptFileBuffer bytes.Buffer
dirErr error
scriptEnvFilePresent bool
)
defer func() { defer func() {
// did the program panic while writing to the buffer? // did the program panic while writing to the buffer?
if err := recover(); err != nil { if err := recover(); err != nil {
cmdCtxLogger.Info().Msg(fmt.Sprintf("panic occurred writing to buffer: %x", err)) // cmdCtxLogger.Debug().Msg(fmt.Sprintf("script buffer: %v", script))
cmdCtxLogger.Info().Msg(fmt.Sprintf("panic occurred writing to buffer: %v", err))
} }
}() }()
if command.Type == "script" { if command.Type == "script" {
script := bytes.NewBufferString(cmd + "\n")
if command.ScriptEnvFile != "" {
command.ScriptEnvFile, dirErr = resolveDir(command.ScriptEnvFile)
if dirErr != nil {
return nil, dirErr
}
file, err := os.Open(command.ScriptEnvFile)
if err != nil {
return nil, err
}
defer file.Close()
_, err = io.Copy(&scriptEnvFileBuffer, file)
if err != nil {
return nil, err
}
// Bug: writing to buffer triggers panic
// why?
// use bytes.Buffer instead of pointer to memory (*bytes.Buffer)
_, err = buffer.WriteString(scriptEnvFileBuffer.String())
if err != nil {
return nil, err
}
// write newline
buffer.WriteByte(0x0A)
_, err = buffer.WriteString(cmd)
if err != nil {
return nil, err
}
script = &buffer
} else {
script = bytes.NewBufferString(cmd + "\n")
}
commandSession.Stdin = script commandSession.Stdin = script
if err := commandSession.Shell(); err != nil { if err := commandSession.Shell(); err != nil {
@ -144,15 +190,8 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
if command.Type == "scriptFile" { if command.Type == "scriptFile" {
var (
buffer bytes.Buffer
scriptEnvFileBuffer bytes.Buffer
scriptFileBuffer bytes.Buffer
dirErr error
scriptEnvFilePresent bool
)
if command.ScriptEnvFile != "" { if command.ScriptEnvFile != "" {
command.ScriptEnvFile, dirErr = resolveDir(command.ScriptEnvFile) command.ScriptEnvFile, dirErr = resolveDir(command.ScriptEnvFile)
if dirErr != nil { if dirErr != nil {
return nil, dirErr return nil, dirErr

View File

@ -15,7 +15,7 @@ The following commands ran:
{{end}} {{end}}
{{ end }} {{ end }}
{{ if .CmdOutput }}{{- range .CmdOutput }}Commad output for {{ .CmdName }}: {{ if .CmdOutput }}{{- range .CmdOutput }}Command output for {{ .CmdName }}:
{{- range .Output}} {{- range .Output}}
{{ . }} {{ . }}
{{ end }}{{ end }} {{ end }}{{ end }}

View File

@ -5,7 +5,7 @@ The following commands ran:
- {{. -}} - {{. -}}
{{end}} {{end}}
{{ if .CmdOutput }}{{- range .CmdOutput }}Commad output for {{ .CmdName }}: {{ if .CmdOutput }}{{- range .CmdOutput }}Command output for {{ .CmdName }}:
{{- range .Output}} {{- range .Output}}
{{ . }} {{ . }}
{{ end }}{{ end }} {{ end }}{{ end }}