Skip to content

Commit 985b823

Browse files
committed
remove top level constructor functions
This removes top level 'New<Thing>' constructor functions in order to make the API slightly smaller and more consistent. Fixes issue #44 Also includes some minor readme and godoc updates. Signed-off-by: Paul Gier <[email protected]>
1 parent 65bdadf commit 985b823

38 files changed

+123
-168
lines changed

Diff for: README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fs, err := procfs.NewFS("/proc")
2222
stats, err := fs.NewStat()
2323
```
2424

25+
Some sub-packages such as `blockdevice`, require access to both the proc and sys filesystems.
26+
27+
```go
28+
fs, err := blockdevice.NewFS("/proc", "/sys")
29+
stats, err := fs.ProcDiskstats()
30+
```
31+
2532
## Building and Testing
2633

2734
The procfs library is normally built as part of another application. However, when making
@@ -30,7 +37,7 @@ changes to the library, the `make test` command can be used to run the API test
3037
### Updating Test Fixtures
3138

3239
The procfs library includes a set of test fixtures which include many example files from
33-
the `/proc` and `/sys` filesystems. These fixtures are included as a ttar (text tar) file
40+
the `/proc` and `/sys` filesystems. These fixtures are included as a [ttar](https://github.com/ideaship/ttar) file
3441
which is extracted automatically during testing. To add/update the test fixtures, first
3542
ensure the `fixtures` directory is up to date by removing the existing directory and then
3643
extracting the ttar file using `make fixtures/.unpacked` or just `make test`.

Diff for: bcache/get.go

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ type FS struct {
3232
sys *fs.FS
3333
}
3434

35+
// NewDefaultFS returns a new Bcache using the default sys fs mount point. It will error
36+
// if the mount point can't be read.
37+
func NewDefaultFS() (FS, error) {
38+
return NewFS(fs.DefaultSysMountPoint)
39+
}
40+
3541
// NewFS returns a new Bcache using the given sys fs mount point. It will error
3642
// if the mount point can't be read.
3743
func NewFS(mountPoint string) (FS, error) {

Diff for: blockdevice/stats.go

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ type FS struct {
9595
sys *fs.FS
9696
}
9797

98+
// NewDefaultFS returns a new blockdevice fs using the default mountPoints for proc and sys.
99+
// It will error if either of these mount points can't be read.
100+
func NewDefaultFS() (FS, error) {
101+
return NewFS(fs.DefaultProcMountPoint, fs.DefaultSysMountPoint)
102+
}
103+
98104
// NewFS returns a new blockdevice fs using the given mountPoints for proc and sys.
99105
// It will error if either of these mount points can't be read.
100106
func NewFS(procMountPoint string, sysMountPoint string) (FS, error) {

Diff for: buddyinfo.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,8 @@ type BuddyInfo struct {
3131
Sizes []float64
3232
}
3333

34-
// NewBuddyInfo reads the buddyinfo statistics.
35-
func NewBuddyInfo() ([]BuddyInfo, error) {
36-
fs, err := NewFS(DefaultMountPoint)
37-
if err != nil {
38-
return nil, err
39-
}
40-
41-
return fs.NewBuddyInfo()
42-
}
43-
4434
// NewBuddyInfo reads the buddyinfo statistics from the specified `proc` filesystem.
45-
func (fs FS) NewBuddyInfo() ([]BuddyInfo, error) {
35+
func (fs FS) BuddyInfo() ([]BuddyInfo, error) {
4636
file, err := os.Open(fs.proc.Path("buddyinfo"))
4737
if err != nil {
4838
return nil, err

Diff for: buddyinfo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
func TestBuddyInfo(t *testing.T) {
22-
buddyInfo, err := getProcFixtures(t).NewBuddyInfo()
22+
buddyInfo, err := getProcFixtures(t).BuddyInfo()
2323
if err != nil {
2424
t.Fatal(err)
2525
}

Diff for: fs.go

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ type FS struct {
2626
// DefaultMountPoint is the common mount point of the proc filesystem.
2727
const DefaultMountPoint = fs.DefaultProcMountPoint
2828

29+
// NewDefaultFS returns a new proc FS mounted under the default proc mountPoint.
30+
// It will error if the mount point directory can't be read or is a file.
31+
func NewDefaultFS() (FS, error) {
32+
return NewFS(DefaultMountPoint)
33+
}
34+
2935
// NewFS returns a new proc FS mounted under the given proc mountPoint. It will error
3036
// if the mount point directory can't be read or is a file.
3137
func NewFS(mountPoint string) (FS, error) {

Diff for: ipvs.go

+4-24
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,8 @@ type IPVSBackendStatus struct {
6262
Weight uint64
6363
}
6464

65-
// NewIPVSStats reads the IPVS statistics.
66-
func NewIPVSStats() (IPVSStats, error) {
67-
fs, err := NewFS(DefaultMountPoint)
68-
if err != nil {
69-
return IPVSStats{}, err
70-
}
71-
72-
return fs.NewIPVSStats()
73-
}
74-
75-
// NewIPVSStats reads the IPVS statistics from the specified `proc` filesystem.
76-
func (fs FS) NewIPVSStats() (IPVSStats, error) {
65+
// IPVSStats reads the IPVS statistics from the specified `proc` filesystem.
66+
func (fs FS) IPVSStats() (IPVSStats, error) {
7767
file, err := os.Open(fs.proc.Path("net/ip_vs_stats"))
7868
if err != nil {
7969
return IPVSStats{}, err
@@ -131,18 +121,8 @@ func parseIPVSStats(file io.Reader) (IPVSStats, error) {
131121
return stats, nil
132122
}
133123

134-
// NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs.
135-
func NewIPVSBackendStatus() ([]IPVSBackendStatus, error) {
136-
fs, err := NewFS(DefaultMountPoint)
137-
if err != nil {
138-
return []IPVSBackendStatus{}, err
139-
}
140-
141-
return fs.NewIPVSBackendStatus()
142-
}
143-
144-
// NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs from the specified `proc` filesystem.
145-
func (fs FS) NewIPVSBackendStatus() ([]IPVSBackendStatus, error) {
124+
// IPVSBackendStatus reads and returns the status of all (virtual,real) server pairs from the specified `proc` filesystem.
125+
func (fs FS) IPVSBackendStatus() ([]IPVSBackendStatus, error) {
146126
file, err := os.Open(fs.proc.Path("net/ip_vs"))
147127
if err != nil {
148128
return nil, err

Diff for: ipvs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func TestIPVSStats(t *testing.T) {
163163
if err != nil {
164164
t.Fatal(err)
165165
}
166-
stats, err := fs.NewIPVSStats()
166+
stats, err := fs.IPVSStats()
167167
if err != nil {
168168
t.Fatal(err)
169169
}
@@ -217,7 +217,7 @@ func TestParseIPPortIPv6(t *testing.T) {
217217
}
218218

219219
func TestIPVSBackendStatus(t *testing.T) {
220-
backendStats, err := getProcFixtures(t).NewIPVSBackendStatus()
220+
backendStats, err := getProcFixtures(t).IPVSBackendStatus()
221221
if err != nil {
222222
t.Fatal(err)
223223
}

Diff for: mdstat.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type MDStat struct {
4343
}
4444

4545
// ParseMDStat parses an mdstat-file and returns a struct with the relevant infos.
46+
// TODO: this should just be called "MDStat" and use a separate parseMDStat function
4647
func (fs FS) ParseMDStat() (mdstates []MDStat, err error) {
4748
mdStatusFilePath := fs.proc.Path("mdstat")
4849
content, err := ioutil.ReadFile(mdStatusFilePath)

Diff for: mountstats_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func TestMountStats(t *testing.T) {
361361
if tt.s != "" {
362362
mounts, err = parseMountStats(strings.NewReader(tt.s))
363363
} else {
364-
proc, e := getProcFixtures(t).NewProc(26231)
364+
proc, e := getProcFixtures(t).Proc(26231)
365365
if e != nil {
366366
t.Fatalf("failed to create proc: %v", err)
367367
}

Diff for: net_dev.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,13 @@ type NetDevLine struct {
4747
// are interface names.
4848
type NetDev map[string]NetDevLine
4949

50-
// NewNetDev returns kernel/system statistics read from /proc/net/dev.
51-
func NewNetDev() (NetDev, error) {
52-
fs, err := NewFS(DefaultMountPoint)
53-
if err != nil {
54-
return nil, err
55-
}
56-
57-
return fs.NewNetDev()
58-
}
59-
60-
// NewNetDev returns kernel/system statistics read from /proc/net/dev.
61-
func (fs FS) NewNetDev() (NetDev, error) {
50+
// NetDev returns kernel/system statistics read from /proc/net/dev.
51+
func (fs FS) NetDev() (NetDev, error) {
6252
return newNetDev(fs.proc.Path("net/dev"))
6353
}
6454

65-
// NewNetDev returns kernel/system statistics read from /proc/[pid]/net/dev.
66-
func (p Proc) NewNetDev() (NetDev, error) {
55+
// NetDev returns kernel/system statistics read from /proc/[pid]/net/dev.
56+
func (p Proc) NetDev() (NetDev, error) {
6757
return newNetDev(p.path("net/dev"))
6858
}
6959

Diff for: net_dev_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func TestNetDevParseLine(t *testing.T) {
3131
}
3232
}
3333

34-
func TestNewNetDev(t *testing.T) {
34+
func TestNetDev(t *testing.T) {
3535
fs, err := NewFS(procTestFixtures)
3636
if err != nil {
3737
t.Fatal(err)
3838
}
3939

40-
netDev, err := fs.NewNetDev()
40+
netDev, err := fs.NetDev()
4141
if err != nil {
4242
t.Fatal(err)
4343
}
@@ -59,13 +59,13 @@ func TestNewNetDev(t *testing.T) {
5959
}
6060
}
6161

62-
func TestProcNewNetDev(t *testing.T) {
63-
p, err := getProcFixtures(t).NewProc(26231)
62+
func TestProcNetDev(t *testing.T) {
63+
p, err := getProcFixtures(t).Proc(26231)
6464
if err != nil {
6565
t.Fatal(err)
6666
}
6767

68-
netDev, err := p.NewNetDev()
68+
netDev, err := p.NetDev()
6969
if err != nil {
7070
t.Fatal(err)
7171
}

Diff for: nfs/nfs.go

+6
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ type FS struct {
275275
proc *fs.FS
276276
}
277277

278+
// NewDefaultFS returns a new FS mounted under the default mountPoint. It will error
279+
// if the mount point can't be read.
280+
func NewDefaultFS() (FS, error) {
281+
return NewFS(fs.DefaultProcMountPoint)
282+
}
283+
278284
// NewFS returns a new FS mounted under the given mountPoint. It will error
279285
// if the mount point can't be read.
280286
func NewFS(mountPoint string) (FS, error) {

Diff for: proc.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewProc(pid int) (Proc, error) {
5454
if err != nil {
5555
return Proc{}, err
5656
}
57-
return fs.NewProc(pid)
57+
return fs.Proc(pid)
5858
}
5959

6060
// AllProcs returns a list of all currently available processes under /proc.
@@ -76,11 +76,11 @@ func (fs FS) Self() (Proc, error) {
7676
if err != nil {
7777
return Proc{}, err
7878
}
79-
return fs.NewProc(pid)
79+
return fs.Proc(pid)
8080
}
8181

82-
// NewProc returns a process for the given pid.
83-
func (fs FS) NewProc(pid int) (Proc, error) {
82+
// Proc returns a process for the given pid.
83+
func (fs FS) Proc(pid int) (Proc, error) {
8484
if _, err := os.Stat(fs.proc.Path(strconv.Itoa(pid))); err != nil {
8585
return Proc{}, err
8686
}

Diff for: proc_io.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type ProcIO struct {
3939
CancelledWriteBytes int64
4040
}
4141

42-
// NewIO creates a new ProcIO instance from a given Proc instance.
43-
func (p Proc) NewIO() (ProcIO, error) {
42+
// IO creates a new ProcIO instance from a given Proc instance.
43+
func (p Proc) IO() (ProcIO, error) {
4444
pio := ProcIO{}
4545

4646
f, err := os.Open(p.path("io"))

Diff for: proc_io_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ package procfs
1616
import "testing"
1717

1818
func TestProcIO(t *testing.T) {
19-
p, err := getProcFixtures(t).NewProc(26231)
19+
p, err := getProcFixtures(t).Proc(26231)
2020
if err != nil {
2121
t.Fatal(err)
2222
}
2323

24-
s, err := p.NewIO()
24+
s, err := p.IO()
2525
if err != nil {
2626
t.Fatal(err)
2727
}

Diff for: proc_limits.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ var (
7777
limitsDelimiter = regexp.MustCompile(" +")
7878
)
7979

80-
// NewLimits returns the current soft limits of the process.
81-
func (p Proc) NewLimits() (ProcLimits, error) {
80+
// Limits returns the current soft limits of the process.
81+
func (p Proc) Limits() (ProcLimits, error) {
8282
f, err := os.Open(p.path("limits"))
8383
if err != nil {
8484
return ProcLimits{}, err

Diff for: proc_limits_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ package procfs
1515

1616
import "testing"
1717

18-
func TestNewLimits(t *testing.T) {
19-
p, err := getProcFixtures(t).NewProc(26231)
18+
func TestLimits(t *testing.T) {
19+
p, err := getProcFixtures(t).Proc(26231)
2020
if err != nil {
2121
t.Fatal(err)
2222
}
2323

24-
l, err := p.NewLimits()
24+
l, err := p.Limits()
2525
if err != nil {
2626
t.Fatal(err)
2727
}

Diff for: proc_ns.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ type Namespace struct {
2929
// Namespaces contains all of the namespaces that the process is contained in.
3030
type Namespaces map[string]Namespace
3131

32-
// NewNamespaces reads from /proc/<pid>/ns/* to get the namespaces of which the
32+
// Namespaces reads from /proc/<pid>/ns/* to get the namespaces of which the
3333
// process is a member.
34-
func (p Proc) NewNamespaces() (Namespaces, error) {
34+
func (p Proc) Namespaces() (Namespaces, error) {
3535
d, err := os.Open(p.path("ns"))
3636
if err != nil {
3737
return nil, err

Diff for: proc_ns_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
)
1919

2020
func TestNewNamespaces(t *testing.T) {
21-
p, err := getProcFixtures(t).NewProc(26231)
21+
p, err := getProcFixtures(t).Proc(26231)
2222
if err != nil {
2323
t.Fatal(err)
2424
}
2525

26-
namespaces, err := p.NewNamespaces()
26+
namespaces, err := p.Namespaces()
2727
if err != nil {
2828
t.Fatal(err)
2929
}

Diff for: proc_psi.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,10 @@ type PSIStats struct {
5151
Full *PSILine
5252
}
5353

54-
// NewPSIStatsForResource reads pressure stall information for the specified
55-
// resource. At time of writing this can be either "cpu", "memory" or "io".
56-
func NewPSIStatsForResource(resource string) (PSIStats, error) {
57-
fs, err := NewFS(DefaultMountPoint)
58-
if err != nil {
59-
return PSIStats{}, err
60-
}
61-
62-
return fs.NewPSIStatsForResource(resource)
63-
}
64-
65-
// NewPSIStatsForResource reads pressure stall information from /proc/pressure/<resource>
66-
func (fs FS) NewPSIStatsForResource(resource string) (PSIStats, error) {
54+
// PSIStatsForResource reads pressure stall information for the specified
55+
// resource from /proc/pressure/<resource>. At time of writing this can be
56+
// either "cpu", "memory" or "io".
57+
func (fs FS) PSIStatsForResource(resource string) (PSIStats, error) {
6758
file, err := os.Open(fs.proc.Path(fmt.Sprintf("%s/%s", "pressure", resource)))
6859
if err != nil {
6960
return PSIStats{}, fmt.Errorf("psi_stats: unavailable for %s", resource)

Diff for: proc_psi_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
func TestPSIStats(t *testing.T) {
2222
t.Run("fake", func(*testing.T) {
23-
stats, err := getProcFixtures(t).NewPSIStatsForResource("fake")
23+
stats, err := getProcFixtures(t).PSIStatsForResource("fake")
2424
if err == nil {
2525
t.Fatal("fake resource does not have PSI statistics")
2626
}
@@ -31,7 +31,7 @@ func TestPSIStats(t *testing.T) {
3131
})
3232

3333
t.Run("cpu", func(t *testing.T) {
34-
stats, err := getProcFixtures(t).NewPSIStatsForResource("cpu")
34+
stats, err := getProcFixtures(t).PSIStatsForResource("cpu")
3535
if err != nil {
3636
t.Fatal(err)
3737
}
@@ -68,7 +68,7 @@ func TestPSIStats(t *testing.T) {
6868

6969
for _, resource := range res {
7070
t.Run(resource, func(t *testing.T) {
71-
stats, err := getProcFixtures(t).NewPSIStatsForResource(resource)
71+
stats, err := getProcFixtures(t).PSIStatsForResource(resource)
7272
if err != nil {
7373
t.Fatal(err)
7474
}

0 commit comments

Comments
 (0)