-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
55 lines (48 loc) · 1.28 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"os/exec"
"strings"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/robinmonjo/dock/iowire"
"github.com/robinmonjo/dock/procfs"
)
const exitSignalOffset = 128
// ExitStatus returns the correct exit status for a process based on if it
// was signaled or existed cleanly.
func exitStatus(status syscall.WaitStatus) int {
if status.Signaled() {
return exitSignalOffset + int(status.Signal())
}
return status.ExitStatus()
}
func exitStatusFromError(err error) int {
if msg, ok := err.(*exec.ExitError); ok {
return msg.Sys().(syscall.WaitStatus).ExitStatus()
}
return -1
}
// Print the current process tree
func printProcessTree() {
procfs.WalkProcs(func(p *procfs.Proc) (bool, error) {
status, err := p.Status()
if err != nil {
log.Printf("%d", p.Pid)
} else {
args, err := p.CmdLine()
if err != nil {
args = []string{status.Name}
}
log.Printf("%d\t%d\t%s\t%s", p.Pid, status.PPid, status.State, strings.Join(args, " "))
}
return true, nil
})
}
// prefix args have the following format: --prefix some-prefix[:blue]
func parsePrefixArg(prefix string) (string, iowire.Color) {
comps := strings.Split(prefix, ":")
if len(comps) == 1 {
return comps[0], iowire.NoColor
}
return comps[0], iowire.MapColor(comps[len(comps)-1])
}