From a6d948819fdf334de1275645752b4ebe835c0ccf Mon Sep 17 00:00:00 2001 From: Dominic Breuker Date: Tue, 6 Mar 2018 09:57:58 +0100 Subject: [PATCH] refactor process refresh --- internal/psscanner/proclist.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/internal/psscanner/proclist.go b/internal/psscanner/proclist.go index d6b97d7..49af2fb 100644 --- a/internal/psscanner/proclist.go +++ b/internal/psscanner/proclist.go @@ -35,25 +35,26 @@ func (pl procList) refresh(eventCh chan string) error { pid := pids[i] _, ok := pl[pid] if !ok { - cmd, err := getCmd(pid) - if err != nil { - cmd = "???" // process probably terminated - } - uid, err := getUID(pid) - if err != nil { - uid = "???" - } - eventCh <- fmt.Sprintf("UID=%-4s PID=%-6d | %s", uid, pid, cmd) - // if print { - // log.Printf("\x1b[31;1mCMD: UID=%-4s PID=%-6d | %s\x1b[0m\n", uid, pid, cmd) - // } - pl[pid] = cmd + pl.addPid(pid, eventCh) } } return nil } +func (pl procList) addPid(pid int, eventCh chan string) { + cmd, err := getCmd(pid) + if err != nil { + cmd = "???" // process probably terminated + } + uid, err := getUID(pid) + if err != nil { + uid = "???" + } + eventCh <- fmt.Sprintf("UID=%-4s PID=%-6d | %s", uid, pid, cmd) + pl[pid] = cmd +} + func getPIDs() ([]int, error) { proc, err := procDirReader() if err != nil {