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

README.md

Lines changed: 8 additions & 1 deletion
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`.

bcache/get.go

Lines changed: 6 additions & 0 deletions
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) {

blockdevice/stats.go

Lines changed: 6 additions & 0 deletions
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) {

buddyinfo.go

Lines changed: 1 addition & 11 deletions
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

buddyinfo_test.go

Lines changed: 1 addition & 1 deletion
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
}

fs.go

Lines changed: 6 additions & 0 deletions
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) {

ipvs.go

Lines changed: 4 additions & 24 deletions
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

ipvs_test.go

Lines changed: 2 additions & 2 deletions
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
}

mdstat.go

Lines changed: 1 addition & 0 deletions
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)

mountstats_test.go

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)