2015-04-27 18:53:21 -04:00
|
|
|
// +build linux
|
|
|
|
|
2014-03-05 20:57:27 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// Define error messages
|
2014-03-05 20:57:27 -05:00
|
|
|
var (
|
|
|
|
ErrCannotParse = errors.New("cannot parse raw input")
|
|
|
|
)
|
|
|
|
|
|
|
|
type lxcInfo struct {
|
|
|
|
Running bool
|
|
|
|
Pid int
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLxcInfo(raw string) (*lxcInfo, error) {
|
|
|
|
if raw == "" {
|
|
|
|
return nil, ErrCannotParse
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
s = bufio.NewScanner(strings.NewReader(raw))
|
|
|
|
info = &lxcInfo{}
|
|
|
|
)
|
|
|
|
for s.Scan() {
|
|
|
|
text := s.Text()
|
|
|
|
|
|
|
|
if s.Err() != nil {
|
|
|
|
return nil, s.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(text, ":")
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2014-03-11 15:08:32 -04:00
|
|
|
switch strings.ToLower(strings.TrimSpace(parts[0])) {
|
2014-03-05 20:57:27 -05:00
|
|
|
case "state":
|
|
|
|
info.Running = strings.TrimSpace(parts[1]) == "RUNNING"
|
|
|
|
case "pid":
|
|
|
|
info.Pid, err = strconv.Atoi(strings.TrimSpace(parts[1]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|