Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct docker.Run error handling #54

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
package docker

import (
"fmt"
"regexp"
"time"

Expand Down Expand Up @@ -72,15 +73,20 @@ func Run(image string, runArgs []string, containerArgs []string) (id string, err
cmd.Args = append(cmd.Args, containerArgs...)
cmd.Debug = true
output, err := cmd.CombinedOutputLines()
// if docker created a container the id will be the first line and match
if len(output) > 0 && containerIDRegex.MatchString(output[0]) {
id = output[0]
}
// log error lines if there were any
if err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking err should occur before checking the output! fixed here

// log error output if there was any
for _, line := range output {
log.Error(line)
}
return "", err
}
// if docker created a container the id will be the first line and match
// validate the output and get the id
if len(output) < 1 {
return "", fmt.Errorf("failed to get container id, received no output from docker run")
}
if !containerIDRegex.MatchString(output[0]) {
return "", fmt.Errorf("failed to get container id, output did not match: %v", output)
}
return id, nil
return output[0], nil
}