Skip to content

Commit 47df2fe

Browse files
fix: Printing of errors and returning correct exit code (#28)
fixes #27
1 parent fc0e0d2 commit 47df2fe

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ jobs:
8181
run: dockerized/bin/dockerized protoc --version
8282
shell: bash
8383

84+
- name: "Test: dockerized returns inner exit code"
85+
run: |
86+
# Run a command that returns 100, check that dockerized returns 100
87+
EXITCODE=$(dockerized/bin/dockerized bash -c 'exit 100' &>/dev/null || echo $?)
88+
echo "Exit code: $EXITCODE"
89+
[ $EXITCODE -eq 100 ]
90+
shell: bash
91+
8492
# region windows
8593
- if: runner.os == 'windows'
8694
name: "dockerized --compile (cmd)"

main.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ var Version string
1818
var contains = util.Contains
1919

2020
func main() {
21-
RunCli(os.Args[1:])
21+
err, exitCode := RunCli(os.Args[1:])
22+
if err != nil {
23+
fmt.Printf("%s\n", err)
24+
}
25+
os.Exit(exitCode)
2226
}
2327

2428
func RunCli(args []string) (err error, exitCode int) {
@@ -183,18 +187,10 @@ func RunCli(args []string) (err error, exitCode int) {
183187
fmt.Printf(" This command, if it exists, will not support version switching.\n")
184188
fmt.Printf(" See: https://github.com/jessfraz/dockerfiles\n")
185189
}
186-
err := dockerized.DockerRun(image, runOptions, volumes)
187-
if err != nil {
188-
return err, 1
189-
}
190-
return nil, 0
190+
return dockerized.DockerRun(image, runOptions, volumes)
191191
}
192192

193-
err = dockerized.DockerComposeRun(project, runOptions, volumes)
194-
if err != nil {
195-
return err, 1
196-
}
197-
return nil, 0
193+
return dockerized.DockerComposeRun(project, runOptions, volumes)
198194
}
199195

200196
func parseArguments(args []string) ([]string, string, string, []string) {

pkg/dockerized.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func LoadEnvFiles(hostCwd string, optionVerbose bool) error {
344344
return nil
345345
}
346346

347-
func dockerComposeRunAdHocService(service types.ServiceConfig, runOptions api.RunOptions) error {
347+
func dockerComposeRunAdHocService(service types.ServiceConfig, runOptions api.RunOptions) (error, int) {
348348
if service.Environment == nil {
349349
service.Environment = map[string]*string{}
350350
}
@@ -357,7 +357,7 @@ func dockerComposeRunAdHocService(service types.ServiceConfig, runOptions api.Ru
357357
}, runOptions, []types.ServiceVolumeConfig{})
358358
}
359359

360-
func DockerRun(image string, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) error {
360+
func DockerRun(image string, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) (error, int) {
361361
// Couldn't get 'docker run' to work, so instead define a Docker Compose Service and run that.
362362
// This coincidentally allows re-using the same code for both 'docker run' and 'docker-compose run'
363363
// - ContainerCreate is simple, but the logic to attach to it is very complex, and not exposed by the Docker SDK.
@@ -504,10 +504,10 @@ func DockerComposeBuild(composeFilePaths []string, buildOptions api.BuildOptions
504504
return backend.Build(ctx, project, buildOptions)
505505
}
506506

507-
func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) error {
507+
func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) (error, int) {
508508
err := os.Chdir(project.WorkingDir)
509509
if err != nil {
510-
return err
510+
return err, 1
511511
}
512512
ctx, _ := newSigContext()
513513

@@ -525,24 +525,24 @@ func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes
525525

526526
backend, err := getBackend()
527527
if err != nil {
528-
return err
528+
return err, 1
529529
}
530530

531531
err = dockerComposeUpNetworkOnly(backend, ctx, project)
532532
if err != nil {
533-
return err
533+
return err, 1
534534
}
535535

536536
project.Services = []types.ServiceConfig{service}
537537

538538
exitCode, err := backend.RunOneOffContainer(ctx, project, runOptions)
539539
if err != nil {
540-
return err
540+
return err, exitCode
541541
}
542542
if exitCode != 0 {
543-
return fmt.Errorf("docker-compose exited with code %d", exitCode)
543+
return fmt.Errorf("%s exited with code %d", serviceName, exitCode), exitCode
544544
}
545-
return nil
545+
return nil, 0
546546
}
547547

548548
func unique(s []string) []string {

0 commit comments

Comments
 (0)