github.com/tompao/docker@v1.9.1/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  // Define error messages
    13  var (
    14  	ErrCannotParse = errors.New("cannot parse raw input")
    15  )
    16  
    17  type lxcInfo struct {
    18  	Running bool
    19  	Pid     int
    20  }
    21  
    22  func parseLxcInfo(raw string) (*lxcInfo, error) {
    23  	if raw == "" {
    24  		return nil, ErrCannotParse
    25  	}
    26  	var (
    27  		err  error
    28  		s    = bufio.NewScanner(strings.NewReader(raw))
    29  		info = &lxcInfo{}
    30  	)
    31  	for s.Scan() {
    32  		text := s.Text()
    33  
    34  		if s.Err() != nil {
    35  			return nil, s.Err()
    36  		}
    37  
    38  		parts := strings.Split(text, ":")
    39  		if len(parts) < 2 {
    40  			continue
    41  		}
    42  		switch strings.ToLower(strings.TrimSpace(parts[0])) {
    43  		case "state":
    44  			info.Running = strings.TrimSpace(parts[1]) == "RUNNING"
    45  		case "pid":
    46  			info.Pid, err = strconv.Atoi(strings.TrimSpace(parts[1]))
    47  			if err != nil {
    48  				return nil, err
    49  			}
    50  		}
    51  	}
    52  	return info, nil
    53  }