Skip to content

Commit

Permalink
feat: Added Process struct, GetProcessList API
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMostafa08 committed Sep 26, 2024
1 parent 9b0bd0f commit d3b50c8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 10 deletions.
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func main() {
log.Printf("%v",cpuInfo)
memInfo, err := psutils.GetMemInfo()
log.Printf("%v",memInfo)
log.Fatalf("err %v",err)
log.Printf("err %v",err)
procs, err := psutils.GetProcessList()
log.Printf("%v\n",procs)
log.Printf("err %v",err)

}
8 changes: 4 additions & 4 deletions pkg/cpuInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

type Loader interface {
Load() (string, error)
Load(filePath string) (string, error)
}

type RealCpuLoader struct{}

func (l *RealCpuLoader) Load() (string, error) {
return loadFile("/proc/cpuinfo")
func (l *RealCpuLoader) Load(filePath string) (string, error) {
return loadFile(filePath)
}

type CpuInfo struct {
Expand Down Expand Up @@ -72,7 +72,7 @@ func GetCpuInfo() (cpuInfo CpuInfo, err error) {

func getCpuInfo(loader Loader) (cpuInfo CpuInfo, err error) {

cpuData, err := loader.Load()
cpuData, err := loader.Load("/proc/cpuinfo")
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cpuInfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ cpu cores : 6

type spyCpuLoader struct{}

func (l *spyCpuLoader) Load() (string, error) {
func (l *spyCpuLoader) Load(string) (string, error) {
return cpuinfo, nil
}
func TestGetCpuInfo(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/memInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type MemInfo struct {

type RealMemLoader struct{}

func (l *RealMemLoader) Load() (string, error) {
return loadFile("/proc/meminfo")
func (l *RealMemLoader) Load(filePath string) (string, error) {
return loadFile(filePath)
}

func extractValue(line string) (valueNoKB string) {
Expand Down Expand Up @@ -56,7 +56,7 @@ func GetMemInfo() (memInfo MemInfo, err error) {

func getMemInfo(loader Loader) (memInfo MemInfo, err error) {

memData, err := loader.Load()
memData, err := loader.Load("/proc/meminfo")
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/memInfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Inactive: 6296272 kB

type spyMemLoader struct{}

func (l *spyMemLoader) Load() (string, error) {
func (l *spyMemLoader) Load(string) (string, error) {
return memInfo, nil
}
func TestGetMemInfo(t *testing.T) {
Expand Down
76 changes: 76 additions & 0 deletions pkg/processInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package psutils

import (
"os"
"path/filepath"
"strconv"
"strings"
)

type Process struct {
PID int
ProcessName string
}

type RealProcLoader struct{}

func (l *RealProcLoader) Load(filePath string) (string, error) {
return loadFile(filePath)
}

func setProcInfo(data string) (proc Process, err error) {
lines := strings.SplitN(data, "\n", -1)

for _, line := range lines {
if strings.HasPrefix(line, "Name") {
parts := strings.Split(line, ":")
value := strings.TrimSpace(parts[1])
proc.ProcessName = value

} else if strings.HasPrefix(line, "Pid") {

parts := strings.Split(line, ":")
value := strings.TrimSpace(parts[1])
proc.PID, err = strconv.Atoi(value)
}
}
return
}

func GetProcessList() (procs []Process, err error) {
var _ Loader = (*RealProcLoader)(nil)
return getProcessList(&RealProcLoader{})
}

func getProcessList(loader Loader) (procs []Process, err error) {
procs = make([]Process, 0)
dir, err := os.Open("/proc")
if err != nil {
return
}
defer dir.Close()

entries, err := dir.Readdirnames(0)
if err != nil {
return
}

for _, entry := range entries {

_, err = strconv.Atoi(entry)
if err != nil {
continue
}

statusFile := filepath.Join("/proc", entry, "status")
var data string
data, err = loader.Load(statusFile)
if err != nil {
return
}
var process Process
process, err = setProcInfo(data)
procs = append(procs, process)
}
return
}

0 comments on commit d3b50c8

Please sign in to comment.