cli/internal/logdetails: move to top-level "internal"

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-05-06 20:04:25 +02:00
parent d0d8d1dc72
commit b6059af164
3 changed files with 1 additions and 1 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/cli/internal/logdetails"
"github.com/docker/cli/internal/logdetails"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/swarm"
-38
View File
@@ -1,38 +0,0 @@
// Package logdetails contains tools for parsing docker log lines.
package logdetails
import (
"errors"
"net/url"
"strings"
)
// Parse parses a string of key value pairs in the form
// "k=v,l=w", where the keys and values are url query escaped, and each pair
// is separated by a comma. Returns a map of the key value pairs on success,
// and an error if the details string is not in a valid format.
//
// The details string encoding is implemented in
// github.com/moby/moby/api/server/httputils/write_log_stream.go
func Parse(details string) (map[string]string, error) {
pairs := strings.Split(details, ",")
detailsMap := make(map[string]string, len(pairs))
for _, pair := range pairs {
k, v, ok := strings.Cut(pair, "=")
if !ok || k == "" {
// missing equal sign, or no key.
return nil, errors.New("invalid details format")
}
var err error
k, err = url.QueryUnescape(k)
if err != nil {
return nil, err
}
v, err = url.QueryUnescape(v)
if err != nil {
return nil, err
}
detailsMap[k] = v
}
return detailsMap, nil
}
@@ -1,60 +0,0 @@
package logdetails
import (
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestParse(t *testing.T) {
testCases := []struct {
line string
expected map[string]string
expectedErr string
}{
{
line: "key=value",
expected: map[string]string{"key": "value"},
},
{
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 _, tc := range testCases {
t.Run(tc.line, func(t *testing.T) {
actual, err := Parse(tc.line)
if tc.expectedErr != "" {
assert.Check(t, is.Error(err, tc.expectedErr))
} else {
assert.Check(t, err)
}
assert.Check(t, is.DeepEqual(tc.expected, actual))
})
}
}