Skip to content

Commit 3a1a4ab

Browse files
committed
Merge branch 'master' of github.com:hfm/mackerel-plugin-mogilefs
2 parents 3930941 + 45b303d commit 3a1a4ab

File tree

8 files changed

+110
-47
lines changed

8 files changed

+110
-47
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: go
22
go:
3-
- 1.5.3
3+
- 1.6
44
- tip
55
env: PATH=/home/travis/gopath/bin:$PATH
66
install: go get github.com/mitchellh/gox
@@ -17,7 +17,7 @@ deploy:
1717
skip_cleanup: true
1818
on:
1919
repo: hfm/mackerel-plugin-mogilefs
20-
condition: "$TRAVIS_GO_VERSION = 1.5.3"
20+
condition: "$TRAVIS_GO_VERSION = 1.6"
2121
tags: true
2222
notifications:
2323
slack:

CHANGELOG.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
CHANGELOG
2-
===
1+
0.4.0 (2016-02-22)
2+
---
3+
4+
### Summary
5+
6+
Built by go 1.6
37

48
0.3.2 (2016-01-14)
59
---
610

11+
### Summary
12+
713
Built by go 1.5.3
814

915
0.3.1 (2016-01-11)
1016
---
1117

18+
### Summary
19+
1220
Fix label name (typo)
1321

1422
### Fixed
@@ -18,6 +26,8 @@ Fix label name (typo)
1826
0.3.0 (2016-01-10)
1927
---
2028

29+
### Summary
30+
2131
Change the repository of mflag
2232

2333
### Fixed
@@ -27,6 +37,8 @@ Change the repository of mflag
2737
0.2.1 (2015-12-30)
2838
---
2939

40+
### Summary
41+
3042
Fix label names
3143

3244
### Fixed
@@ -38,6 +50,8 @@ Fix label names
3850
0.2.0 (2015-12-30)
3951
---
4052

53+
### Summary
54+
4155
Add new graphs
4256

4357
### Added
@@ -49,6 +63,8 @@ Add new graphs
4963
0.1.0 (2015-12-30)
5064
---
5165

66+
### Summary
67+
5268
Initial release
5369

5470
### Added

mogilefs.go renamed to cli.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
65
"io"
76
"net"
8-
"os"
9-
"strings"
107

11-
mp "github.com/mackerelio/go-mackerel-plugin-helper"
128
flag "github.com/docker/docker/pkg/mflag"
9+
mp "github.com/mackerelio/go-mackerel-plugin-helper"
1310
)
1411

1512
// Exit codes are int values that represent an exit code for a particular error.
1613
const (
1714
ExitCodeOK int = 0
18-
ExitCodeError int = 1 + iota
15+
ExitCodeParseFlagError int = 1 + iota
16+
ExitCodeError
1917
)
2018

2119
/* graphdef is Graph definition for mackerelplugin.
@@ -109,35 +107,18 @@ func (m MogilefsPlugin) FetchMetrics() (map[string]interface{}, error) {
109107
return m.parseStats(conn)
110108
}
111109

112-
func (m MogilefsPlugin) parseStats(conn io.Reader) (map[string]interface{}, error) {
113-
scanner := bufio.NewScanner(conn)
114-
stats := make(map[string]interface{})
115-
116-
for scanner.Scan() {
117-
line := scanner.Text()
118-
s := string(line)
119-
if s == "." {
120-
return stats, nil
121-
}
122-
123-
res := strings.Split(s, " ")
124-
stats[res[0]] = res[1]
125-
}
126-
127-
if err := scanner.Err(); err != nil {
128-
return stats, err
129-
}
130-
131-
return nil, nil
132-
}
133-
134110
// GraphDefinition interface for mackerelplugin.
135111
func (m MogilefsPlugin) GraphDefinition() map[string](mp.Graphs) {
136112
return graphdef
137113
}
138114

139-
// Parse flags and Run helper (MackerelPlugin) with the given arguments.
140-
func main() {
115+
// CLI is the object for command line interface.
116+
type CLI struct {
117+
outStream, errStream io.Writer
118+
}
119+
120+
// Run is to parse flags and Run helper (MackerelPlugin) with the given arguments.
121+
func (c *CLI) Run(args []string) int {
141122
// Flags
142123
var (
143124
host string
@@ -155,14 +136,14 @@ func main() {
155136
flags.BoolVar(&version, []string{"v", "version"}, false, "Print version information and quit.")
156137

157138
// Parse commandline flag
158-
if err := flags.Parse(os.Args[1:]); err != nil {
159-
os.Exit(ExitCodeError)
139+
if err := flags.Parse(args[1:]); err != nil {
140+
return ExitCodeError
160141
}
161142

162143
// Show version
163144
if version {
164-
fmt.Fprintf(os.Stderr, "%s version %s\n", Name, Version)
165-
os.Exit(ExitCodeOK)
145+
fmt.Fprintf(c.errStream, "%s version %s\n", Name, Version)
146+
return ExitCodeOK
166147
}
167148

168149
// Create MackerelPlugin for MogileFS
@@ -172,4 +153,6 @@ func main() {
172153
helper.Tempfile = tempfile
173154

174155
helper.Run()
156+
157+
return ExitCodeOK
175158
}

cli_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestGraphDefinition(t *testing.T) {
11+
var mogilefs MogilefsPlugin
12+
13+
graphdef := mogilefs.GraphDefinition()
14+
if len(graphdef) != 5 {
15+
t.Errorf("GetTempfilename: %d should be 5", len(graphdef))
16+
}
17+
}
18+
19+
func TestCLI_Run(t *testing.T) {
20+
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
21+
cli := &CLI{outStream: outStream, errStream: errStream}
22+
args := strings.Split("mackerel-plugin-mogilefs -version", " ")
23+
24+
status := cli.Run(args)
25+
if status != ExitCodeOK {
26+
t.Errorf("ExitStatus=%d, want %d", status, ExitCodeOK)
27+
}
28+
29+
expected := fmt.Sprintf("mackerel-plugin-mogilefs version %s", Version)
30+
if !strings.Contains(errStream.String(), expected) {
31+
t.Errorf("Output=%q, want %q", errStream.String(), expected)
32+
}
33+
34+
}

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import (
4+
"os"
5+
)
6+
7+
func main() {
8+
cli := &CLI{outStream: os.Stdout, errStream: os.Stderr}
9+
os.Exit(cli.Run(os.Args))
10+
}

stats.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"io"
6+
"strings"
7+
)
8+
9+
func (m MogilefsPlugin) parseStats(conn io.Reader) (map[string]interface{}, error) {
10+
scanner := bufio.NewScanner(conn)
11+
stats := make(map[string]interface{})
12+
13+
for scanner.Scan() {
14+
line := scanner.Text()
15+
s := string(line)
16+
if s == "." {
17+
return stats, nil
18+
}
19+
20+
res := strings.Split(s, " ")
21+
stats[res[0]] = res[1]
22+
}
23+
24+
if err := scanner.Err(); err != nil {
25+
return stats, err
26+
}
27+
28+
return nil, nil
29+
}

mogilefs_test.go renamed to stats_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ import (
66
"testing"
77
)
88

9-
func TestGraphDefinition(t *testing.T) {
10-
var mogilefs MogilefsPlugin
11-
12-
graphdef := mogilefs.GraphDefinition()
13-
if len(graphdef) != 5 {
14-
t.Errorf("GetTempfilename: %d should be 5", len(graphdef))
15-
}
16-
}
17-
189
func TestParse(t *testing.T) {
1910
var mogilefs MogilefsPlugin
2011
stub := `uptime 35235

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ package main
44
const Name string = "mackerel-plugin-mogilefs"
55

66
// Version is version string of this application.
7-
const Version string = "0.3.2"
7+
const Version string = "0.4.1"

0 commit comments

Comments
 (0)