parse_logs.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package client
  2. // parse_logs.go contains utility helpers for getting information out of docker
  3. // log lines. really, it only contains ParseDetails right now. maybe in the
  4. // future there will be some desire to parse log messages back into a struct?
  5. // that would go here if we did
  6. import (
  7. "net/url"
  8. "strings"
  9. "github.com/pkg/errors"
  10. )
  11. // ParseLogDetails takes a details string of key value pairs in the form
  12. // "k=v,l=w", where the keys and values are url query escaped, and each pair
  13. // is separated by a comma, returns a map. returns an error if the details
  14. // string is not in a valid format
  15. // the exact form of details encoding is implemented in
  16. // api/server/httputils/write_log_stream.go
  17. func ParseLogDetails(details string) (map[string]string, error) {
  18. pairs := strings.Split(details, ",")
  19. detailsMap := make(map[string]string, len(pairs))
  20. for _, pair := range pairs {
  21. p := strings.SplitN(pair, "=", 2)
  22. // if there is no equals sign, we will only get 1 part back
  23. if len(p) != 2 {
  24. return nil, errors.New("invalid details format")
  25. }
  26. k, err := url.QueryUnescape(p[0])
  27. if err != nil {
  28. return nil, err
  29. }
  30. v, err := url.QueryUnescape(p[1])
  31. if err != nil {
  32. return nil, err
  33. }
  34. detailsMap[k] = v
  35. }
  36. return detailsMap, nil
  37. }