From 5d730b5fecfc2ebb2062d22cd908316456159624 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 27 Jul 2019 00:15:35 -0400 Subject: [PATCH] address linter warnings --- client/backend.go | 2 +- component/asciibox/asciibox.go | 1 + component/barchart/barchart.go | 19 +++++++++++-------- component/gauge/gauge.go | 1 + component/layout/layout.go | 1 + component/runchart/grid.go | 6 +++--- component/runchart/legend.go | 8 +++----- component/runchart/runchart.go | 29 +++++++++++++++-------------- component/sparkline/sparkline.go | 1 + component/textbox/textbox.go | 1 + component/util/geometry.go | 7 ------- config/arrangement.go | 3 +-- config/component.go | 7 +++++-- config/options.go | 1 + data/int_basic.go | 6 +++--- data/int_pty.go | 6 ++++-- data/item.go | 9 ++++++--- metadata/license.go | 23 ++++++++++++----------- metadata/statistics.go | 23 ++++++++++++++--------- 19 files changed, 84 insertions(+), 70 deletions(-) diff --git a/client/backend.go b/client/backend.go index c72d6cc..a26cb95 100644 --- a/client/backend.go +++ b/client/backend.go @@ -19,7 +19,7 @@ const ( jsonContentType = "application/json" ) -// Backend client is used to verify license and to send telemetry reports +// BackendClient is used to verify license and to send telemetry reports // for analyses (anonymous usage data statistics and crash reports) type BackendClient struct { client http.Client diff --git a/component/asciibox/asciibox.go b/component/asciibox/asciibox.go index 64a0127..a1d1f31 100644 --- a/component/asciibox/asciibox.go +++ b/component/asciibox/asciibox.go @@ -12,6 +12,7 @@ import ( "strings" ) +// AsciiBox represents a component with ascii-style text type AsciiBox struct { *ui.Block *data.Consumer diff --git a/component/barchart/barchart.go b/component/barchart/barchart.go index 14913df..17844a8 100644 --- a/component/barchart/barchart.go +++ b/component/barchart/barchart.go @@ -17,17 +17,18 @@ const ( barIndent int = 1 ) +// BarChart presents categorical data with rectangular bars type BarChart struct { *ui.Block *data.Consumer - bars []Bar + bars []bar scale int maxValue float64 count int64 palette console.Palette } -type Bar struct { +type bar struct { label string color ui.Color value float64 @@ -39,14 +40,14 @@ func NewBarChart(c config.BarChartConfig, palette console.Palette) *BarChart { chart := BarChart{ Block: component.NewBlock(c.Title, true, palette), Consumer: data.NewConsumer(), - bars: []Bar{}, + bars: []bar{}, scale: *c.Scale, maxValue: -math.MaxFloat64, palette: palette, } for _, i := range c.Items { - chart.AddBar(*i.Label, *i.Color) + chart.addBar(*i.Label, *i.Color) } go func() { @@ -68,13 +69,14 @@ func (b *BarChart) consumeSample(sample *data.Sample) { b.count++ float, err := util.ParseFloat(sample.Value) + if err != nil { b.HandleConsumeFailure("Failed to parse a number", err, sample) return - } else { - b.HandleConsumeSuccess() } + b.HandleConsumeSuccess() + index := -1 for i, bar := range b.bars { if bar.label == sample.Label { @@ -97,8 +99,8 @@ func (b *BarChart) consumeSample(sample *data.Sample) { } } -func (b *BarChart) AddBar(label string, color ui.Color) { - b.bars = append(b.bars, Bar{label: label, color: color, value: 0}) +func (b *BarChart) addBar(label string, color ui.Color) { + b.bars = append(b.bars, bar{label: label, color: color, value: 0}) } func (b *BarChart) reselectMaxValue() { @@ -111,6 +113,7 @@ func (b *BarChart) reselectMaxValue() { b.maxValue = maxValue } +// Draw renders the barchart func (b *BarChart) Draw(buffer *ui.Buffer) { b.Block.Draw(buffer) diff --git a/component/gauge/gauge.go b/component/gauge/gauge.go index 8712de7..542ba54 100644 --- a/component/gauge/gauge.go +++ b/component/gauge/gauge.go @@ -17,6 +17,7 @@ const ( CurValueLabel = "cur" ) +// Gauge displays cur value between specified min and max values type Gauge struct { *ui.Block *data.Consumer diff --git a/component/layout/layout.go b/component/layout/layout.go index 7fff6f3..ee95aac 100644 --- a/component/layout/layout.go +++ b/component/layout/layout.go @@ -13,6 +13,7 @@ import ( "time" ) +// Layout represents component arrangement on the screen type Layout struct { ui.Block Components []*component.Component diff --git a/component/runchart/grid.go b/component/runchart/grid.go index 99ba77f..d78c8e9 100644 --- a/component/runchart/grid.go +++ b/component/runchart/grid.go @@ -10,7 +10,7 @@ import ( const defaultValueLength = 4 -type ChartGrid struct { +type chartGrid struct { timeRange TimeRange timePerPoint time.Duration valueExtrema ValueExtrema @@ -19,12 +19,12 @@ type ChartGrid struct { minTimeWidth int } -func (c *RunChart) newChartGrid() ChartGrid { +func (c *RunChart) newChartGrid() chartGrid { linesCount := (c.Inner.Max.X - c.Inner.Min.X - c.grid.minTimeWidth) / xAxisGridWidth timeRange := c.getTimeRange(linesCount) - return ChartGrid{ + return chartGrid{ timeRange: timeRange, timePerPoint: c.timescale / time.Duration(xAxisGridWidth), valueExtrema: getLocalExtrema(c.lines, timeRange), diff --git a/component/runchart/legend.go b/component/runchart/legend.go index d0885b7..b3139d5 100644 --- a/component/runchart/legend.go +++ b/component/runchart/legend.go @@ -18,7 +18,7 @@ const ( timeFormat = "15:04:05.000" ) -type Legend struct { +type legend struct { Enabled bool Details bool } @@ -102,15 +102,13 @@ func getColumnWidth(mode Mode, lines []TimeLine, scale int) int { func getDiffWithPreviousValue(line TimeLine) float64 { if len(line.points) < 2 { return 0 - } else { - return line.points[len(line.points)-1].value - line.points[len(line.points)-2].value } + return line.points[len(line.points)-1].value - line.points[len(line.points)-2].value } func getCurrentValue(line TimeLine) float64 { if len(line.points) == 0 { return 0 - } else { - return line.points[len(line.points)-1].value } + return line.points[len(line.points)-1].value } diff --git a/component/runchart/runchart.go b/component/runchart/runchart.go index 2ec7449..7801e03 100644 --- a/component/runchart/runchart.go +++ b/component/runchart/runchart.go @@ -38,17 +38,18 @@ const ( CommandMoveSelection = "MOVE_SELECTION" ) +// RunChart displays observed data in a time sequence type RunChart struct { *ui.Block *data.Consumer lines []TimeLine - grid ChartGrid + grid chartGrid timescale time.Duration mutex *sync.Mutex mode Mode selection time.Time scale int - legend Legend + legend legend palette console.Palette } @@ -87,7 +88,7 @@ func NewRunChart(c config.RunChartConfig, palette console.Palette) *RunChart { mutex: &sync.Mutex{}, scale: *c.Scale, mode: ModeDefault, - legend: Legend{Enabled: c.Legend.Enabled, Details: c.Legend.Details}, + legend: legend{Enabled: c.Legend.Enabled, Details: c.Legend.Details}, palette: palette, } @@ -159,10 +160,10 @@ func (c *RunChart) consumeSample(sample *data.Sample) { if err != nil { c.HandleConsumeFailure("Failed to parse a number", err, sample) return - } else { - c.HandleConsumeSuccess() } + c.HandleConsumeSuccess() + c.mutex.Lock() index := -1 @@ -318,13 +319,13 @@ func (c *RunChart) moveSelection(shift int) { c.mode = ModePinpoint c.selection = getMidRangeTime(c.grid.timeRange) return - } else { - c.selection = c.selection.Add(c.grid.timePerPoint * time.Duration(shift)) - if c.selection.After(c.grid.timeRange.max) { - c.selection = c.grid.timeRange.max - } else if c.selection.Before(c.grid.timeRange.min) { - c.selection = c.grid.timeRange.min - } + } + + c.selection = c.selection.Add(c.grid.timePerPoint * time.Duration(shift)) + if c.selection.After(c.grid.timeRange.max) { + c.selection = c.grid.timeRange.max + } else if c.selection.Before(c.grid.timeRange.min) { + c.selection = c.grid.timeRange.min } for i := range c.lines { @@ -352,9 +353,9 @@ func calculateTimescale(rateMs int) time.Duration { if timescale.Seconds() == 0 { return time.Second - } else { - return timescale } + + return timescale } func braillePoint(point image.Point) image.Point { diff --git a/component/sparkline/sparkline.go b/component/sparkline/sparkline.go index 62ef153..27428a3 100644 --- a/component/sparkline/sparkline.go +++ b/component/sparkline/sparkline.go @@ -11,6 +11,7 @@ import ( "sync" ) +// SparkLine displays general shape of a measurement variation over time type SparkLine struct { *ui.Block *data.Consumer diff --git a/component/textbox/textbox.go b/component/textbox/textbox.go index a1501a8..19afeb6 100644 --- a/component/textbox/textbox.go +++ b/component/textbox/textbox.go @@ -9,6 +9,7 @@ import ( "image" ) +// TextBox represents a component with regular text type TextBox struct { *ui.Block *data.Consumer diff --git a/component/util/geometry.go b/component/util/geometry.go index dc10676..7dc9e97 100644 --- a/component/util/geometry.go +++ b/component/util/geometry.go @@ -2,7 +2,6 @@ package util import ( "image" - "math" ) func GetRectLeftSideCenter(rect image.Rectangle) image.Point { @@ -33,12 +32,6 @@ func GetRectBottomSideCenter(rect image.Rectangle) image.Point { } } -func GetDistance(p1 image.Point, p2 image.Point) float64 { - x := math.Abs(float64(p1.X - p2.X)) - y := math.Abs(float64(p1.Y - p2.Y)) - return math.Sqrt(x*x + y*y) -} - func GetRectCoordinates(area image.Rectangle, width int, height int) (int, int, int, int) { x1 := area.Min.X + area.Dx()/2 - width/2 y1 := area.Min.Y + area.Dy()/2 - height diff --git a/config/arrangement.go b/config/arrangement.go index bf6b7da..18923b6 100644 --- a/config/arrangement.go +++ b/config/arrangement.go @@ -189,9 +189,8 @@ func countEmptyCellsBelow(grid [console.RowsCount][console.ColumnsCount]int, row for r := row; r < console.RowsCount; r++ { if grid[r][column] == 1 { return count - } else { - count++ } + count++ } return count } diff --git a/config/component.go b/config/component.go index 42897bb..0300970 100644 --- a/config/component.go +++ b/config/component.go @@ -36,9 +36,12 @@ func (c *ComponentConfig) GetSize() Size { func (c *ComponentConfig) GetRectangle() image.Rectangle { if c.Position == nil || len(c.Position) == 0 { return image.ZR - } else { - return image.Rect(c.Position[0][0], c.Position[0][1], c.Position[0][0]+c.Position[1][0], c.Position[0][1]+c.Position[1][1]) } + return image.Rect( + c.Position[0][0], + c.Position[0][1], + c.Position[0][0]+c.Position[1][0], + c.Position[0][1]+c.Position[1][1]) } type TriggerConfig struct { diff --git a/config/options.go b/config/options.go index 1f2213b..6b63b9e 100644 --- a/config/options.go +++ b/config/options.go @@ -1,5 +1,6 @@ package config +// Options with cli flags type Options struct { ConfigFile *string `short:"c" long:"config" description:"Path to YAML config file"` LicenseKey *string `short:"l" long:"license" description:"License key. Visit www.sampler.dev for details"` diff --git a/data/int_basic.go b/data/int_basic.go index fb8c31a..3cd648a 100644 --- a/data/int_basic.go +++ b/data/int_basic.go @@ -10,6 +10,7 @@ import ( "time" ) +// BasicInteractiveShell represents non-PTY interactive shell sampling metadata type BasicInteractiveShell struct { item *Item variables []string @@ -93,7 +94,7 @@ func (s *BasicInteractiveShell) execute() (string, error) { _ = s.cmd.Wait() s.item.basicShell = nil // restart session } - return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err)) + return "", fmt.Errorf("failed to execute command: %s", err) } timeout := make(chan bool, 1) @@ -121,9 +122,8 @@ func (s *BasicInteractiveShell) execute() (string, error) { case <-timeout: if errorText.Len() > 0 { return "", errors.New(errorText.String()) - } else { - return s.item.transform(resultText.String()) } + return s.item.transform(resultText.String()) } } } diff --git a/data/int_pty.go b/data/int_pty.go index 09e6b05..1f10525 100644 --- a/data/int_pty.go +++ b/data/int_pty.go @@ -1,8 +1,9 @@ +//+build !windows + package data import ( "bufio" - "errors" "fmt" "github.com/kr/pty" "github.com/lunixbochs/vtclean" @@ -12,6 +13,7 @@ import ( "time" ) +// PtyInteractiveShell represents PTY interactive shell sampling metadata type PtyInteractiveShell struct { item *Item variables []string @@ -73,7 +75,7 @@ func (s *PtyInteractiveShell) execute() (string, error) { _ = s.file.Close() s.item.ptyShell = nil // restart session } - return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err)) + return "", fmt.Errorf("failed to execute command: %s", err) } softTimeout := make(chan bool, 1) diff --git a/data/item.go b/data/item.go index b637110..38a8898 100644 --- a/data/item.go +++ b/data/item.go @@ -74,14 +74,17 @@ func (i *Item) execute(variables []string, script string) (string, error) { } func (i *Item) initInteractiveShell(v []string) error { + timeout := time.Duration(i.rateMs) * time.Millisecond * 3 / 4 + if i.pty { i.ptyShell = &PtyInteractiveShell{item: i, variables: v, timeout: timeout} return i.ptyShell.init() - } else { - i.basicShell = &BasicInteractiveShell{item: i, variables: v, timeout: timeout} - return i.basicShell.init() } + + i.basicShell = &BasicInteractiveShell{item: i, variables: v, timeout: timeout} + + return i.basicShell.init() } func (i *Item) transform(sample string) (string, error) { diff --git a/metadata/license.go b/metadata/license.go index a4d62e8..0ab75b1 100644 --- a/metadata/license.go +++ b/metadata/license.go @@ -23,20 +23,21 @@ const ( const licenseFileName = "license.yml" func GetLicense() *License { + if !fileExists(licenseFileName) { return nil - } else { - file := readStorageFile(getPlatformStoragePath(licenseFileName)) - - license := new(License) - err := yaml.Unmarshal(file, license) - - if err != nil { - log.Fatalf("Failed to read license file: %v", err) - } - - return license } + + file := readStorageFile(getPlatformStoragePath(licenseFileName)) + + license := new(License) + err := yaml.Unmarshal(file, license) + + if err != nil { + log.Fatalf("Failed to read license file: %v", err) + } + + return license } func SaveLicense(license License) { diff --git a/metadata/statistics.go b/metadata/statistics.go index 141ebff..1662793 100644 --- a/metadata/statistics.go +++ b/metadata/statistics.go @@ -9,7 +9,7 @@ import ( "runtime" ) -// Anonymous usage data, which we collect for analyses and improvements +// Statistics represents anonymous usage data, which we collect for analyses and improvements // User can disable it, along with crash reports, using --telemetry flag type Statistics struct { Version string @@ -22,6 +22,7 @@ type Statistics struct { const statisticsFileName = "statistics.yml" +// PersistStatistics in file func PersistStatistics(config *config.Config) *Statistics { statistics := new(Statistics) @@ -65,7 +66,9 @@ func PersistStatistics(config *config.Config) *Statistics { return statistics } +// GetStatistics from file func GetStatistics(cfg *config.Config) *Statistics { + if !fileExists(statisticsFileName) { return &Statistics{ Version: console.AppVersion, @@ -75,15 +78,17 @@ func GetStatistics(cfg *config.Config) *Statistics { WindowHeight: 0, ComponentsCount: countComponentsPerType(cfg), } - } else { - file := readStorageFile(getPlatformStoragePath(statisticsFileName)) - license := new(Statistics) - err := yaml.Unmarshal(file, license) - if err != nil { - log.Fatalf("Failed to read statistics file: %v", err) - } - return license } + + file := readStorageFile(getPlatformStoragePath(statisticsFileName)) + license := new(Statistics) + + err := yaml.Unmarshal(file, license) + if err != nil { + log.Fatalf("Failed to read statistics file: %v", err) + } + + return license } func countComponentsPerType(config *config.Config) map[string]int {