Skip to content
Snippets Groups Projects
Commit 74d8dfce5c37 authored by steeve.chailloux's avatar steeve.chailloux
Browse files

runCommand must always return a file to avoid nil pointer exception

parent 55feb6cff2cd
No related branches found
No related tags found
1 merge request!2runCommand must always return a file to avoid nil pointer exception
Pipeline #41532 passed
......@@ -223,8 +223,20 @@
return cmds, nil
}
func (r *Runner) runCommand(tmpDir, name string, cmd *cmd.Cmd) (*os.File, error) {
tmpFile, err := ioutil.TempFile(tmpDir, fmt.Sprintf("compiled-%s-*.yaml", name))
if err != nil {
return nil, fmt.Errorf("cannot create compiled file: %w", err)
}
defer func() {
if err := tmpFile.Close(); err != nil {
r.config.Logger.
Err(err).
Str("temp file", tmpFile.Name()).
Msg("failed to close temp file")
}
}()
if r.config.DryRun {
r.config.Logger.Info().
Str("command", cmd.Name).
Str("args", strings.Join(cmd.Args, " ")).
Msg("would run command")
......@@ -226,9 +238,9 @@
if r.config.DryRun {
r.config.Logger.Info().
Str("command", cmd.Name).
Str("args", strings.Join(cmd.Args, " ")).
Msg("would run command")
return nil, nil
return tmpFile, nil
}
stdOut, stdErr, err := RunCMD(cmd)
if err != nil {
......@@ -243,18 +255,6 @@
fmt.Printf("\n%s\n\n", strings.Join(stdErr, "\n"))
return nil, fmt.Errorf("failed to run command: %w", err)
}
tmpFile, err := ioutil.TempFile(tmpDir, fmt.Sprintf("compiled-%s-*.yaml", name))
if err != nil {
return nil, fmt.Errorf("cannot create compiled file: %w", err)
}
defer func() {
if err := tmpFile.Close(); err != nil {
r.config.Logger.
Err(err).
Str("temp file", tmpFile.Name()).
Msg("failed to close temp file")
}
}()
if _, err := tmpFile.WriteString(strings.Join(stdOut, "\n")); err != nil {
return nil, fmt.Errorf("cannot write compiled file: %w", err)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment