1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/hibp/widget.go
Sam Roberts 4bb725db9e golangci-lint configuration file
golangci-lint can run all the currently enabled linters, and as far as I
can tell, does it in under 5 seconds as opposed to over 180 seconds
(compare `time make cilint` and `time make lint`).

Some of the linters that are listed in the "enabled" section but
commented out looked like a good idea to me, and fairly low hanging
fruit to fix, but they are not passing at the moment.

All the linters covered in the current Makefile are run.

TODO:
- replace lint target in Makefile with golangci-lint
- remove .github/workflow/errcheck.yml
2020-05-16 12:34:57 -07:00

107 lines
2.0 KiB
Go

package hibp
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
)
// Widget is the container for hibp data
type Widget struct {
view.TextWidget
settings *Settings
statuses []*Status
err error
}
// NewWidget creates a new instance of a widget
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := &Widget{
TextWidget: view.NewTextWidget(app, settings.common),
settings: settings,
}
return widget
}
/* -------------------- Exported Functions -------------------- */
// Fetch retrieves HIBP data from the HIBP API
func (widget *Widget) Fetch(accounts []string) ([]*Status, error) {
data := []*Status{}
for _, account := range accounts {
stat, err := widget.fetchForAccount(account, widget.settings.since)
if err != nil {
return nil, err
}
data = append(data, stat)
}
return data, nil
}
// Refresh updates the data for this widget and displays it onscreen
func (widget *Widget) Refresh() {
statuses, err := widget.Fetch(widget.settings.accounts)
if err != nil {
widget.err = err
widget.statuses = nil
} else {
widget.err = nil
widget.statuses = statuses
}
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
title := widget.CommonSettings().Title
if widget.err != nil {
return title, widget.err.Error(), true
}
title += widget.sinceDateForTitle()
str := ""
for _, status := range widget.statuses {
color := widget.settings.colors.ok
if status.HasBeenCompromised() {
color = widget.settings.colors.pwned
}
if status != nil {
str += fmt.Sprintf(" [%s]%s[white]\n", color, status.Account)
}
}
return title, str, false
}
func (widget *Widget) sinceDateForTitle() string {
dateStr := ""
if widget.settings.HasSince() {
sinceStr := ""
dt, err := widget.settings.SinceDate()
if err != nil {
sinceStr = widget.settings.since
} else {
sinceStr = dt.Format("Jan _2, 2006")
}
dateStr = dateStr + " since " + sinceStr
}
return dateStr
}