From 4c1eca23a0c15ad6df44caef5b417ed7797d8013 Mon Sep 17 00:00:00 2001 From: ecrupper Date: Mon, 9 Sep 2024 11:08:33 -0500 Subject: [PATCH] use hashicorp envparse over toMap --- executor/linux/outputs.go | 36 ++++++++++--------------- executor/linux/outputs_test.go | 48 ---------------------------------- go.mod | 1 + go.sum | 2 ++ 4 files changed, 17 insertions(+), 70 deletions(-) diff --git a/executor/linux/outputs.go b/executor/linux/outputs.go index eb61b9b2..00c02db5 100644 --- a/executor/linux/outputs.go +++ b/executor/linux/outputs.go @@ -3,11 +3,12 @@ package linux import ( + "bytes" "context" "encoding/base64" "fmt" - "strings" + envparse "github.com/hashicorp/go-envparse" "github.com/sirupsen/logrus" "github.com/go-vela/types/pipeline" @@ -125,34 +126,25 @@ func (o *outputSvc) poll(ctx context.Context, ctn *pipeline.Container) (map[stri return nil, nil, err } + reader := bytes.NewReader(outputBytes) + + outputMap, err := envparse.Parse(reader) + if err != nil { + logger.Debugf("unable to parse output map: %v", err) + } + // grab masked outputs maskedBytes, err := o.client.Runtime.PollOutputsContainer(ctx, ctn, "/vela/outputs/masked.env") if err != nil { return nil, nil, err } - return toMap(outputBytes), toMap(maskedBytes), nil -} + reader = bytes.NewReader(maskedBytes) -// toMap is a helper function that turns raw docker exec output bytes into a map -// by splitting on carriage returns + newlines and once more on `=`. -func toMap(input []byte) map[string]string { - // carriage returns are included in the split because the exec config `TTY` value is set to true - lines := strings.Split(string(input), "\r\n") - - m := make(map[string]string) - - for _, line := range lines { - parts := strings.SplitN(line, "=", 2) - if len(parts) == 2 { - s := parts[1] - if !strings.Contains(parts[1], "\\\\n") { - s = strings.Replace(parts[1], "\\n", "\\\n", -1) - } - - m[parts[0]] = s - } + maskMap, err := envparse.Parse(reader) + if err != nil { + logger.Debugf("unable to parse masked output map: %v", err) } - return m + return outputMap, maskMap, nil } diff --git a/executor/linux/outputs_test.go b/executor/linux/outputs_test.go index dc463e24..db0a3174 100644 --- a/executor/linux/outputs_test.go +++ b/executor/linux/outputs_test.go @@ -7,7 +7,6 @@ import ( "flag" "net/http/httptest" "os" - "reflect" "testing" "github.com/gin-gonic/gin" @@ -468,50 +467,3 @@ func TestLinux_Outputs_poll(t *testing.T) { }) } } - -func TestLinux_Outputs_toMap(t *testing.T) { - // setup tests - tests := []struct { - name string - runtime string - input []byte - wantMap map[string]string - }{ - { - name: "basic", - runtime: constants.DriverDocker, - input: []byte("FOO=bar\r\nONE=1\r\nTEST=hello world\r\n"), - wantMap: map[string]string{ - "FOO": "bar", - "ONE": "1", - "TEST": "hello world", - }, - }, - { - name: "multiple equals", - runtime: constants.DriverDocker, - input: []byte("FOO=bar\r\nEQUATION=e=mc^2\r\n"), - wantMap: map[string]string{ - "FOO": "bar", - "EQUATION": "e=mc^2", - }, - }, - { - name: "bad format", - runtime: constants.DriverDocker, - input: []byte("FOO;bar//ONE^TEST,,,hello world"), - wantMap: make(map[string]string), - }, - } - - // run tests - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - got := toMap(test.input) - - if !reflect.DeepEqual(got, test.wantMap) { - t.Errorf("toMap is %v, want %v", got, test.wantMap) - } - }) - } -} diff --git a/go.mod b/go.mod index 491b2750..f27744e5 100644 --- a/go.mod +++ b/go.mod @@ -77,6 +77,7 @@ require ( github.com/goware/urlx v0.3.2 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect + github.com/hashicorp/go-envparse v0.1.0 github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-retryablehttp v0.7.7 // indirect github.com/huandu/xstrings v1.3.3 // indirect diff --git a/go.sum b/go.sum index 0d40d5eb..51e75d6f 100644 --- a/go.sum +++ b/go.sum @@ -147,6 +147,8 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= +github.com/hashicorp/go-envparse v0.1.0 h1:bE++6bhIsNCPLvgDZkYqo3nA+/PFI51pkrHdmPSDFPY= +github.com/hashicorp/go-envparse v0.1.0/go.mod h1:OHheN1GoygLlAkTlXLXvAdnXdZxy8JUweQ1rAXx1xnc= github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=