From 71604786c4a3c327f580730779e594e420a1e886 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Wed, 23 Oct 2024 17:24:28 +1300 Subject: [PATCH 1/3] Always write update output file, even on container failure --- internal/infra/run.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/internal/infra/run.go b/internal/infra/run.go index c2f863f..46bf568 100644 --- a/internal/infra/run.go +++ b/internal/infra/run.go @@ -6,6 +6,16 @@ import ( "encoding/base64" "encoding/json" "fmt" + "io" + "log" + "net/http" + "os" + "os/signal" + "regexp" + "strings" + "syscall" + "time" + "github.com/dependabot/cli/internal/model" "github.com/dependabot/cli/internal/server" "github.com/docker/docker/api/types" @@ -17,15 +27,6 @@ import ( "github.com/moby/moby/api/types/registry" "github.com/moby/moby/client" "gopkg.in/yaml.v3" - "io" - "log" - "net/http" - "os" - "os/signal" - "regexp" - "strings" - "syscall" - "time" ) type RunParams struct { @@ -131,12 +132,15 @@ func Run(params RunParams) error { if params.ApiUrl == "" { params.ApiUrl = fmt.Sprintf("http://host.docker.internal:%v", api.Port()) } - if err := runContainers(ctx, params); err != nil { - return err - } + + // run the containers, but don't return the error until AFTER the output is generated. + // this ensures that the output is always written in the scenario where there are multiple outputs, + // some that succeed and some that fail; we still want to see the output of the successful ones. + runContainersErr := runContainers(ctx, params) api.Complete() + // write the output to a file output, err := generateOutput(params, api, outFile) if err != nil { return err @@ -146,6 +150,11 @@ func Run(params RunParams) error { return diff(params, outFile, output) } + // if the containers failed, poperagate the error + if runContainersErr != nil { + return runContainersErr + } + return nil } From 75fb427a41da0b9200ed53757cda8518ff4aab75 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Wed, 23 Oct 2024 17:37:35 +1300 Subject: [PATCH 2/3] Fix typo --- internal/infra/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/infra/run.go b/internal/infra/run.go index 46bf568..37e7ccd 100644 --- a/internal/infra/run.go +++ b/internal/infra/run.go @@ -150,7 +150,7 @@ func Run(params RunParams) error { return diff(params, outFile, output) } - // if the containers failed, poperagate the error + // if the containers failed, propagate the error if runContainersErr != nil { return runContainersErr } From 79439a5c832efb0765a757bbb9158e503dbaf84c Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Thu, 24 Oct 2024 10:27:40 +1300 Subject: [PATCH 3/3] Tidy code --- internal/infra/run.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/internal/infra/run.go b/internal/infra/run.go index 37e7ccd..faa644f 100644 --- a/internal/infra/run.go +++ b/internal/infra/run.go @@ -6,16 +6,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io" - "log" - "net/http" - "os" - "os/signal" - "regexp" - "strings" - "syscall" - "time" - "github.com/dependabot/cli/internal/model" "github.com/dependabot/cli/internal/server" "github.com/docker/docker/api/types" @@ -27,6 +17,15 @@ import ( "github.com/moby/moby/api/types/registry" "github.com/moby/moby/client" "gopkg.in/yaml.v3" + "io" + "log" + "net/http" + "os" + "os/signal" + "regexp" + "strings" + "syscall" + "time" ) type RunParams struct { @@ -150,12 +149,7 @@ func Run(params RunParams) error { return diff(params, outFile, output) } - // if the containers failed, propagate the error - if runContainersErr != nil { - return runContainersErr - } - - return nil + return runContainersErr } func generateOutput(params RunParams, api *server.API, outFile *os.File) ([]byte, error) {