2017-09-07 12:44:52 -04:00
|
|
|
/*Package logs contains tools for parsing docker log lines.
|
|
|
|
*/
|
|
|
|
package logs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseLogDetails 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 ParseLogDetails(details string) (map[string]string, error) {
|
|
|
|
pairs := strings.Split(details, ",")
|
|
|
|
detailsMap := make(map[string]string, len(pairs))
|
|
|
|
for _, pair := range pairs {
|
2022-12-27 10:30:02 -05:00
|
|
|
k, v, ok := strings.Cut(pair, "=")
|
|
|
|
if !ok || k == "" {
|
|
|
|
// missing equal sign, or no key.
|
2017-09-07 12:44:52 -04:00
|
|
|
return nil, errors.New("invalid details format")
|
|
|
|
}
|
2022-12-27 10:30:02 -05:00
|
|
|
var err error
|
|
|
|
k, err = url.QueryUnescape(k)
|
2017-09-07 12:44:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-27 10:30:02 -05:00
|
|
|
v, err = url.QueryUnescape(v)
|
2017-09-07 12:44:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
detailsMap[k] = v
|
|
|
|
}
|
|
|
|
return detailsMap, nil
|
|
|
|
}
|