Skip to content

Commit

Permalink
use hashicorp envparse over toMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Sep 9, 2024
1 parent 48da52a commit 4c1eca2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 70 deletions.
36 changes: 14 additions & 22 deletions executor/linux/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
48 changes: 0 additions & 48 deletions executor/linux/outputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"flag"
"net/http/httptest"
"os"
"reflect"
"testing"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 4c1eca2

Please sign in to comment.