Skip to content

fix: Printing of errors and returning correct exit code #28

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

Merged
merged 7 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ jobs:
run: dockerized/bin/dockerized protoc --version
shell: bash

- name: "Test: dockerized returns inner exit code"
run: |
# Run a command that returns 100, check that dockerized returns 100
EXITCODE=$(dockerized/bin/dockerized bash -c 'exit 100' &>/dev/null || echo $?)
echo "Exit code: $EXITCODE"
[ $EXITCODE -eq 100 ]
shell: bash

# region windows
- if: runner.os == 'windows'
name: "dockerized --compile (cmd)"
Expand Down
18 changes: 7 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ var Version string
var contains = util.Contains

func main() {
RunCli(os.Args[1:])
err, exitCode := RunCli(os.Args[1:])
if err != nil {
fmt.Printf("%s\n", err)
}
os.Exit(exitCode)
}

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

err = dockerized.DockerComposeRun(project, runOptions, volumes)
if err != nil {
return err, 1
}
return nil, 0
return dockerized.DockerComposeRun(project, runOptions, volumes)
}

func parseArguments(args []string) ([]string, string, string, []string) {
Expand Down
18 changes: 9 additions & 9 deletions pkg/dockerized.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func LoadEnvFiles(hostCwd string, optionVerbose bool) error {
return nil
}

func dockerComposeRunAdHocService(service types.ServiceConfig, runOptions api.RunOptions) error {
func dockerComposeRunAdHocService(service types.ServiceConfig, runOptions api.RunOptions) (error, int) {
if service.Environment == nil {
service.Environment = map[string]*string{}
}
Expand All @@ -357,7 +357,7 @@ func dockerComposeRunAdHocService(service types.ServiceConfig, runOptions api.Ru
}, runOptions, []types.ServiceVolumeConfig{})
}

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

func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) error {
func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) (error, int) {
err := os.Chdir(project.WorkingDir)
if err != nil {
return err
return err, 1
}
ctx, _ := newSigContext()

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

backend, err := getBackend()
if err != nil {
return err
return err, 1
}

err = dockerComposeUpNetworkOnly(backend, ctx, project)
if err != nil {
return err
return err, 1
}

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

exitCode, err := backend.RunOneOffContainer(ctx, project, runOptions)
if err != nil {
return err
return err, exitCode
}
if exitCode != 0 {
return fmt.Errorf("docker-compose exited with code %d", exitCode)
return fmt.Errorf("%s exited with code %d", serviceName, exitCode), exitCode
}
return nil
return nil, 0
}

func unique(s []string) []string {
Expand Down