Index: golxc.go |
=== modified file 'golxc.go' |
--- golxc.go 2012-11-26 10:16:42 +0000 |
+++ golxc.go 2013-02-07 13:53:53 +0000 |
@@ -76,11 +76,11 @@ |
// List lists the existing containers on the system. |
func List() ([]*Container, error) { |
- stdout, err := run("lxc-ls", "-1") |
+ out, err := run("lxc-ls", "-1") |
if err != nil { |
return nil, err |
} |
- names := nameSet(stdout) |
+ names := nameSet(out) |
containers := make([]*Container, len(names)) |
for i, name := range names { |
containers[i] = New(name) |
@@ -254,11 +254,11 @@ |
// Info returns the status and the process id of the container. |
func (c *Container) Info() (State, int, error) { |
- stdout, err := run("lxc-info", "-n", c.name) |
+ out, err := run("lxc-info", "-n", c.name) |
if err != nil { |
return StateUnknown, -1, err |
} |
- kv := keyValues(stdout, ": ") |
+ kv := keyValues(out, ": ") |
state := State(kv["state"]) |
pid, err := strconv.Atoi(kv["pid"]) |
if err != nil { |
@@ -305,21 +305,29 @@ |
return c.containerHome() + "/rootfs/" |
} |
-// run executes the passed command and returns the output. |
+// run executes the passed command and returns the out. |
func run(name string, args ...string) (string, error) { |
cmd := exec.Command(name, args...) |
- output, err := cmd.CombinedOutput() |
+ // LXC tools do not use stdout and stderr in a predictable |
+ // way; based on experimentation, the most convenient |
+ // solution is to combine them and leave the client to |
+ // determine sanity as best it can. |
+ out, err := cmd.CombinedOutput() |
if err != nil { |
- return "", runError(name, err, output) |
+ return "", runError(name, err, out) |
} |
- return string(output), nil |
+ return string(out), nil |
} |
// runError creates an error if run fails. |
-func runError(name string, err error, output []byte) error { |
+func runError(name string, err error, out []byte) error { |
e := &Error{name, err, nil} |
- for _, l := range strings.Split(string(output), "\n") { |
+ for _, l := range strings.Split(string(out), "\n") { |
if strings.HasPrefix(l, name+": ") { |
+ // LXC tools do not always print their output with |
+ // the command name as prefix. The name is part of |
+ // the error struct, so stip it from the output if |
+ // printed. |
l = l[len(name)+2:] |
} |
if l != "" { |
@@ -329,7 +337,7 @@ |
return e |
} |
-// keyValues retrieves key/value pairs out of a command output. |
+// keyValues retrieves key/value pairs out of a command out. |
func keyValues(raw string, sep string) map[string]string { |
kv := map[string]string{} |
lines := strings.Split(raw, "\n") |
@@ -342,7 +350,7 @@ |
return kv |
} |
-// nameSet retrieves a set of names out of a command output. |
+// nameSet retrieves a set of names out of a command out. |
func nameSet(raw string) []string { |
collector := map[string]struct{}{} |
set := []string{} |