From a09f9dba9fb990fd947a3ab0fc9bd3e6a66bcfa2 Mon Sep 17 00:00:00 2001 From: Ivan Daniluk Date: Fri, 1 May 2015 19:13:23 +0300 Subject: [PATCH] Linting --- expvars.go | 35 ++++++++++++++++++++--------------- main.go | 4 +++- service.go | 6 +++++- source.go | 5 ----- ui_dummy.go | 7 ++++++- ui_termui.go | 3 ++- 6 files changed, 36 insertions(+), 24 deletions(-) delete mode 100644 source.go diff --git a/expvars.go b/expvars.go index 5815bdd..f04bb2e 100644 --- a/expvars.go +++ b/expvars.go @@ -8,30 +8,35 @@ import ( "github.com/antonholmquist/jason" ) -const ExpvarsUrl = "/debug/vars" +// ExpvarsUrl is the default url for fetching expvar info. +const ExpvarsURL = "/debug/vars" -type Expvar *jason.Object +// Expvar represents fetched expvar variable. +type Expvar struct { + *jason.Object +} // FetchExpvar fetches expvar by http for the given addr (host:port) -func FetchExpvar(addr string) (*jason.Object, error) { - var e jason.Object +func FetchExpvar(addr string) (*Expvar, error) { + e := &Expvar{&jason.Object{}} resp, err := http.Get(addr) if err != nil { - return &e, err + return e, err } defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { - return &e, errors.New("Vars not found. Did you import expvars?") - } else { - expvar, err := ParseExpvar(resp.Body) - e = *expvar - if err != nil { - return &e, err - } + return e, errors.New("Vars not found. Did you import expvars?") } - return &e, nil + + e, err = ParseExpvar(resp.Body) + if err != nil { + return e, err + } + return e, nil } -func ParseExpvar(r io.Reader) (*jason.Object, error) { - return jason.NewObjectFromReader(r) +// ParseExpvar parses expvar data from reader. +func ParseExpvar(r io.Reader) (*Expvar, error) { + object, err := jason.NewObjectFromReader(r) + return &Expvar{object}, err } diff --git a/main.go b/main.go index eac23d8..0777ae4 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,9 @@ func main() { if *dummy { ui = &DummyUI{} } - ui.Init(*data) + if err := ui.Init(*data); err != nil { + log.Fatal(err) + } defer ui.Close() tick := time.NewTicker(*interval) diff --git a/service.go b/service.go index abf7659..d3da6c0 100644 --- a/service.go +++ b/service.go @@ -7,6 +7,7 @@ import ( //"github.com/pyk/byten" ) +// Services is just a slice of Service. type Services []*Service // Service represents constantly updating info about single service. @@ -68,7 +69,7 @@ func (s *Service) updateCmdline(cmdline []string) { // // If host is not specified, 'localhost' is used. func (s Service) Addr() string { - return fmt.Sprintf("http://localhost:%s%s", s.Port, ExpvarsUrl) + return fmt.Sprintf("http://localhost:%s%s", s.Port, ExpvarsURL) } // StatusLine returns status line for services with it's name and status. @@ -80,6 +81,7 @@ func (s Service) StatusLine() string { return fmt.Sprintf("[R] %s", s.Name) } +// Value returns current value for the given var of this service. func (s Service) Value(name VarName) string { if s.Err != nil { return "N/A" @@ -95,6 +97,8 @@ func (s Service) Value(name VarName) string { return fmt.Sprintf("%d", val.Front()) } +// Values returns slice of ints with recent values of the given var, +// to be used with sparkline. func (s Service) Values(name VarName) []int { if s.Err != nil { return nil diff --git a/source.go b/source.go deleted file mode 100644 index 17920dc..0000000 --- a/source.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -type Source interface { - Update() (interface{}, error) -} diff --git a/ui_dummy.go b/ui_dummy.go index f6712cb..a133ae0 100644 --- a/ui_dummy.go +++ b/ui_dummy.go @@ -7,8 +7,13 @@ import ( // DummyUI is an simple console UI mockup, for testing purposes. type DummyUI struct{} +// Init implements UI. func (*DummyUI) Init(UIData) error { return nil } -func (*DummyUI) Close() {} + +// Close implements UI. +func (*DummyUI) Close() {} + +// Update implements UI. func (*DummyUI) Update(data UIData) { if data.Services == nil { return diff --git a/ui_termui.go b/ui_termui.go index fb67712..667e504 100644 --- a/ui_termui.go +++ b/ui_termui.go @@ -32,6 +32,7 @@ func (t *TermUI) Init(data UIData) error { p.TextFgColor = termui.ColorWhite p.Border.Label = "Services Monitor" p.Border.FgColor = termui.ColorCyan + t.Title.Text = fmt.Sprintf("monitoring %d services, press q to quit", len(data.Services)) return p }() t.Status = func() *termui.Par { @@ -99,8 +100,8 @@ func (t *TermUI) Init(data UIData) error { return nil } +// Update updates UI widgets from UIData. func (t *TermUI) Update(data UIData) { - t.Title.Text = fmt.Sprintf("monitoring %d services, press q to quit", len(data.Services)) t.Status.Text = fmt.Sprintf("Last update: %v", data.LastTimestamp.Format("15:04:05 02/Jan/06")) var services []string