diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml new file mode 100644 index 0000000..58098c4 --- /dev/null +++ b/.github/workflows/push.yaml @@ -0,0 +1,14 @@ +name: "Release a tag" +on: push + +jobs: + test-unit: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + - name: Test Unit + run: go test -v ./... diff --git a/README.md b/README.md index be43782..e88a8df 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,13 @@ These capabilities make Protty a useful tool for a variety of purposes, such as The following command will start a proxy on port 8080, and after starting, all traffic from port 8080 will be redirected to a remote host located at https://example.com ```shell -docker run -p8080:80 -e REMOTE_URI=https://example.com:443 mgerasimchuk/protty:v0.4.0 +docker run -p8080:80 -e REMOTE_URI=https://example.com:443 mgerasimchuk/protty:v0.4.1 ``` ## Running options and runtime configuration ``` -» ~ docker run -p8080:80 -it mgerasimchuk/protty:v0.4.0 /bin/sh -c 'protty start --help' +» ~ docker run -p8080:80 -it mgerasimchuk/protty:v0.4.1 /bin/sh -c 'protty start --help' Start the proxy Usage: diff --git a/go.mod b/go.mod index ef23292..da733da 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module protty -go 1.18 +go 1.20 require ( github.com/graze/go-throttled v0.3.1 @@ -8,13 +8,16 @@ require ( github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 - github.com/thoas/go-funk v0.9.3 + github.com/stretchr/testify v1.7.0 golang.org/x/time v0.3.0 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/itchyny/timefmt-go v0.1.5 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/sys v0.2.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 00e9ab1..b087ed3 100644 --- a/go.sum +++ b/go.sum @@ -23,19 +23,16 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw= -github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/util/component_name_test.go b/pkg/util/component_name_test.go index 45af402..97a0e06 100644 --- a/pkg/util/component_name_test.go +++ b/pkg/util/component_name_test.go @@ -11,7 +11,7 @@ func TestGetCurrentFuncName(t *testing.T) { actual string want string }{ - {GetCurrentFuncName(), "szutil.TestGetCurrentFuncName"}, + {GetCurrentFuncName(), "util.TestGetCurrentFuncName"}, } for _, tt := range tests { tt := tt diff --git a/pkg/util/sed.go b/pkg/util/sed.go index 08afc03..8cecf1d 100644 --- a/pkg/util/sed.go +++ b/pkg/util/sed.go @@ -20,5 +20,9 @@ func SED(sedExpr string, input []byte) ([]byte, []byte, error) { if err != nil { return input, input, fmt.Errorf("%s: %w", GetFuncName(engine.RunString), err) } + // remove last \n cos sed adds this by default + if !strings.HasSuffix(string(input), "\n") && strings.HasSuffix(output, "\n") { + output = output[:len(output)-1] + } return []byte(output), input, nil } diff --git a/pkg/util/sed_test.go b/pkg/util/sed_test.go new file mode 100644 index 0000000..46174ba --- /dev/null +++ b/pkg/util/sed_test.go @@ -0,0 +1,41 @@ +package util + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestSED_Success(t *testing.T) { + type args struct { + msg string + input string + expr string + } + tests := []struct { + args args + want string + }{ + { + args{"New line can be replaced", "first line\nsecond line", `:a;N;$!ba;s/\n/,/g`}, + "first line,second line", + }, + { + args{"New line doesn't add at the end", "Hello sed", `s/sed/world/`}, + "Hello world", + }, + { + args{"New line doesn't remove from the end", "Hello sed\n", `s/sed/world/`}, + "Hello world\n", + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.args.msg, func(t *testing.T) { + t.Parallel() + actual, _, err := SED(tt.args.expr, []byte(tt.args.input)) + assert.NoError(t, err) + assert.Equal(t, tt.want, string(actual)) + }) + } +}