1
0

Well, actually, let's move the platform-independent parts out to a process.go file.

This commit is contained in:
Alexander Andreev 2023-12-15 04:07:37 +04:00
parent e53bf0b77d
commit 6ba2e8a471
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 22 additions and 21 deletions

22
process.go Normal file
View File

@ -0,0 +1,22 @@
package main
// Process contains an alias that will be returned when queries, and a process
// name to look for.
type Process struct {
Alias string `json:"alias"`
Process string `json:"process"`
}
// ProcessesState is a map of processes' aliases and its statuses.
type ProcessesState map[string]bool
func GetProcessesState(procs *[]Process) (ps ProcessesState) {
ps = make(ProcessesState)
for _, proc := range *procs {
pids, err := GetProcessPIDs(proc.Process)
ps[proc.Alias] = err == nil && len(pids) > 0
}
return
}

View File

@ -9,27 +9,6 @@ import (
"strings" "strings"
) )
// Process contains an alias that will be returned when queries, and a process
// name to look for.
type Process struct {
Alias string `json:"alias"`
Process string `json:"process"`
}
// ProcessesState is a map of processes' aliases and its statuses.
type ProcessesState map[string]bool
func GetProcessesState(procs *[]Process) (ps ProcessesState) {
ps = make(ProcessesState)
for _, proc := range *procs {
pids, err := GetProcessPIDs(proc.Process)
ps[proc.Alias] = err == nil && len(pids) > 0
}
return
}
// GetProcessPIDs returns a list of PIDs found for a process. // GetProcessPIDs returns a list of PIDs found for a process.
func GetProcessPIDs(name string) (pids []int, err error) { func GetProcessPIDs(name string) (pids []int, err error) {
dir, err := os.ReadDir("/proc/") dir, err := os.ReadDir("/proc/")