mirror of https://github.com/docker/cli.git
service/logs: use strings.Cut
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
3fa18636ec
commit
42de5cc7f0
|
@ -20,16 +20,17 @@ func ParseLogDetails(details string) (map[string]string, error) {
|
||||||
pairs := strings.Split(details, ",")
|
pairs := strings.Split(details, ",")
|
||||||
detailsMap := make(map[string]string, len(pairs))
|
detailsMap := make(map[string]string, len(pairs))
|
||||||
for _, pair := range pairs {
|
for _, pair := range pairs {
|
||||||
p := strings.SplitN(pair, "=", 2)
|
k, v, ok := strings.Cut(pair, "=")
|
||||||
// if there is no equals sign, we will only get 1 part back
|
if !ok || k == "" {
|
||||||
if len(p) != 2 {
|
// missing equal sign, or no key.
|
||||||
return nil, errors.New("invalid details format")
|
return nil, errors.New("invalid details format")
|
||||||
}
|
}
|
||||||
k, err := url.QueryUnescape(p[0])
|
var err error
|
||||||
|
k, err = url.QueryUnescape(k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
v, err := url.QueryUnescape(p[1])
|
v, err = url.QueryUnescape(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package logs
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
)
|
)
|
||||||
|
@ -12,24 +11,51 @@ func TestParseLogDetails(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
line string
|
line string
|
||||||
expected map[string]string
|
expected map[string]string
|
||||||
err error
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
{"key=value", map[string]string{"key": "value"}, nil},
|
{
|
||||||
{"key1=value1,key2=value2", map[string]string{"key1": "value1", "key2": "value2"}, nil},
|
line: "key=value",
|
||||||
{"key+with+spaces=value%3Dequals,asdf%2C=", map[string]string{"key with spaces": "value=equals", "asdf,": ""}, nil},
|
expected: map[string]string{"key": "value"},
|
||||||
{"key=,=nothing", map[string]string{"key": "", "": "nothing"}, nil},
|
},
|
||||||
{"=", map[string]string{"": ""}, nil},
|
{
|
||||||
{"errors", nil, errors.New("invalid details format")},
|
line: "key1=value1,key2=value2",
|
||||||
|
expected: map[string]string{"key1": "value1", "key2": "value2"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
line: "key+with+spaces=value%3Dequals,asdf%2C=",
|
||||||
|
expected: map[string]string{"key with spaces": "value=equals", "asdf,": ""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
line: "key=,key2=",
|
||||||
|
expected: map[string]string{"key": "", "key2": ""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
line: "key=,=nothing",
|
||||||
|
expectedErr: "invalid details format",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
line: "=nothing",
|
||||||
|
expectedErr: "invalid details format",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
line: "=",
|
||||||
|
expectedErr: "invalid details format",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
line: "errors",
|
||||||
|
expectedErr: "invalid details format",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, testcase := range testCases {
|
for _, tc := range testCases {
|
||||||
testcase := testcase
|
tc := tc
|
||||||
t.Run(testcase.line, func(t *testing.T) {
|
t.Run(tc.line, func(t *testing.T) {
|
||||||
actual, err := ParseLogDetails(testcase.line)
|
actual, err := ParseLogDetails(tc.line)
|
||||||
if testcase.err != nil {
|
if tc.expectedErr != "" {
|
||||||
assert.Error(t, err, testcase.err.Error())
|
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
|
||||||
return
|
} else {
|
||||||
|
assert.Check(t, err)
|
||||||
}
|
}
|
||||||
assert.Check(t, is.DeepEqual(testcase.expected, actual))
|
assert.Check(t, is.DeepEqual(tc.expected, actual))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue