github.com/dougm/docker@v1.5.0/daemon/execdriver/lxc/info.go (about)

     1  package lxc
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  var (
    11  	ErrCannotParse = errors.New("cannot parse raw input")
    12  )
    13  
    14  type lxcInfo struct {
    15  	Running bool
    16  	Pid     int
    17  }
    18  
    19  func parseLxcInfo(raw string) (*lxcInfo, error) {
    20  	if raw == "" {
    21  		return nil, ErrCannotParse
    22  	}
    23  	var (
    24  		err  error
    25  		s    = bufio.NewScanner(strings.NewReader(raw))
    26  		info = &lxcInfo{}
    27  	)
    28  	for s.Scan() {
    29  		text := s.Text()
    30  
    31  		if s.Err() != nil {
    32  			return nil, s.Err()
    33  		}
    34  
    35  		parts := strings.Split(text, ":")
    36  		if len(parts) < 2 {
    37  			continue
    38  		}
    39  		switch strings.ToLower(strings.TrimSpace(parts[0])) {
    40  		case "state":
    41  			info.Running = strings.TrimSpace(parts[1]) == "RUNNING"
    42  		case "pid":
    43  			info.Pid, err = strconv.Atoi(strings.TrimSpace(parts[1]))
    44  			if err != nil {
    45  				return nil, err
    46  			}
    47  		}
    48  	}
    49  	return info, nil
    50  }