github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/service/logs/parse_logs.go (about)

     1  /*Package logs contains tools for parsing docker log lines.
     2   */
     3  package logs
     4  
     5  import (
     6  	"net/url"
     7  	"strings"
     8  
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // ParseLogDetails parses a string of key value pairs in the form
    13  // "k=v,l=w", where the keys and values are url query escaped, and each pair
    14  // is separated by a comma. Returns a map of the key value pairs on success,
    15  // and an error if the details string is not in a valid format.
    16  //
    17  // The details string encoding is implemented in
    18  // github.com/moby/moby/api/server/httputils/write_log_stream.go
    19  func ParseLogDetails(details string) (map[string]string, error) {
    20  	pairs := strings.Split(details, ",")
    21  	detailsMap := make(map[string]string, len(pairs))
    22  	for _, pair := range pairs {
    23  		p := strings.SplitN(pair, "=", 2)
    24  		// if there is no equals sign, we will only get 1 part back
    25  		if len(p) != 2 {
    26  			return nil, errors.New("invalid details format")
    27  		}
    28  		k, err := url.QueryUnescape(p[0])
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		v, err := url.QueryUnescape(p[1])
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		detailsMap[k] = v
    37  	}
    38  	return detailsMap, nil
    39  }