github.com/ld86/docker@v1.7.1-rc3/daemon/execdriver/lxc/info.go (about)

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