Skip to content

Commit c1cea49

Browse files
doc: adding godoc style comments for APIs
1 parent ff5577f commit c1cea49

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

pkg/cpuInfo.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// psutils package implements a human-friendly lib for querying processes, memory info and cpu info
12
package psutils
23

34
import (
@@ -7,15 +8,16 @@ import (
78
)
89

910
type Loader interface {
10-
Load(filePath string) (string, error)
11+
load(filePath string) (string, error)
1112
}
1213

13-
type RealCpuLoader struct{}
14+
type realCpuLoader struct{}
1415

15-
func (l *RealCpuLoader) Load(filePath string) (string, error) {
16+
func (l *realCpuLoader) load(filePath string) (string, error) {
1617
return loadFile(filePath)
1718
}
1819

20+
// CpuInfo holds some details about the CPU like number of cores, vendorID, model name, cache size, average CPU frequency
1921
type CpuInfo struct {
2022
NumCores int
2123
VendorId string
@@ -65,14 +67,15 @@ func setCpuInfo(cpuData string) (cpuInfo CpuInfo, err error) {
6567
return
6668
}
6769

70+
// GetCpuInfo returns a CpuInfo type about the CPU in this moment
6871
func GetCpuInfo() (cpuInfo CpuInfo, err error) {
69-
var _ Loader = (*RealCpuLoader)(nil)
70-
return getCpuInfo(&RealCpuLoader{})
72+
var _ Loader = (*realCpuLoader)(nil)
73+
return getCpuInfo(&realCpuLoader{})
7174
}
7275

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

75-
cpuData, err := loader.Load("/proc/cpuinfo")
78+
cpuData, err := loader.load("/proc/cpuinfo")
7679
if err != nil {
7780
return
7881
}

pkg/memInfo.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
// psutils package implements a human-friendly lib for querying processes, memory info and cpu info
12
package psutils
23

34
import (
45
"strconv"
56
"strings"
67
)
78

9+
// MemInfo holds some info about the memory like total,used and availabe memory in KiloBytes
810
type MemInfo struct {
911
TotalMemoryKB float64
1012
UsedMemoryKB float64
1113
AvailableMemoryKB float64
1214
}
1315

14-
type RealMemLoader struct{}
16+
type realMemLoader struct{}
1517

16-
func (l *RealMemLoader) Load(filePath string) (string, error) {
18+
func (l *realMemLoader) load(filePath string) (string, error) {
1719
return loadFile(filePath)
1820
}
1921

@@ -49,14 +51,15 @@ func setMemInfo(memData string) (memInfo MemInfo, err error) {
4951
return
5052
}
5153

54+
// GetMemInfo returns a MemInfo type of the memory in this moment
5255
func GetMemInfo() (memInfo MemInfo, err error) {
53-
var _ Loader = (*RealMemLoader)(nil)
54-
return getMemInfo(&RealMemLoader{})
56+
var _ Loader = (*realMemLoader)(nil)
57+
return getMemInfo(&realMemLoader{})
5558
}
5659

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

59-
memData, err := loader.Load("/proc/meminfo")
62+
memData, err := loader.load("/proc/meminfo")
6063
if err != nil {
6164
return
6265
}

pkg/processInfo.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
// psutils package implements a human-friendly lib for querying processes, memory info and cpu info
12
package psutils
23

3-
import "fmt"
4-
54
import (
5+
"fmt"
66
"os"
77
"path/filepath"
88
"strconv"
99
"strings"
1010
)
1111

12+
// Process holds the basic information about a process, like its Pid and name
1213
type Process struct {
1314
PID int
1415
ProcessName string
1516
}
1617

18+
// ProcessDetails holds more detailed information about a process, like its State, PPid, Tgid
1719
type ProcessDetails struct {
1820
State string
1921
PPID int
2022
Tgid int
2123
}
2224

23-
type RealProcLoader struct{}
25+
type realProcLoader struct{}
2426

25-
func (l *RealProcLoader) Load(filePath string) (string, error) {
27+
func (l *realProcLoader) load(filePath string) (string, error) {
2628
return loadFile(filePath)
2729
}
2830

@@ -60,9 +62,10 @@ func setProcInfo(data string) (proc Process, procDetails ProcessDetails, err err
6062
return
6163
}
6264

65+
// GetProcessList returns a list of all currently running processes and an error if exists
6366
func GetProcessList() (procs []Process, err error) {
64-
var _ Loader = (*RealProcLoader)(nil)
65-
return getProcessList(&RealProcLoader{})
67+
var _ Loader = (*realProcLoader)(nil)
68+
return getProcessList(&realProcLoader{})
6669
}
6770

6871
func getProcessList(loader Loader) (procs []Process, err error) {
@@ -87,7 +90,7 @@ func getProcessList(loader Loader) (procs []Process, err error) {
8790

8891
statusFile := filepath.Join("/proc", entry, "status")
8992
var data string
90-
data, err = loader.Load(statusFile)
93+
data, err = loader.load(statusFile)
9194
if err != nil {
9295
return
9396
}
@@ -101,16 +104,18 @@ func getProcessList(loader Loader) (procs []Process, err error) {
101104
return
102105
}
103106

107+
// GetProcessDetails takes a PID as an argument
108+
// and returns its details as ProcessDetails type, and an error if exists
104109
func GetProcessDetails(PID int) (ProcessDetails ProcessDetails, err error) {
105-
var _ Loader = (*RealProcLoader)(nil)
106-
return getProcessDetails(PID, &RealProcLoader{})
110+
var _ Loader = (*realProcLoader)(nil)
111+
return getProcessDetails(PID, &realProcLoader{})
107112
}
108113

109114
func getProcessDetails(PID int, loader Loader) (ProcessDetails ProcessDetails, err error) {
110115

111116
statusFile := filepath.Join("/proc", fmt.Sprint(PID), "status")
112117
var data string
113-
data, err = loader.Load(statusFile)
118+
data, err = loader.load(statusFile)
114119
if err != nil {
115120
return
116121
}

0 commit comments

Comments
 (0)