diff --git a/bamboohr/bamboohr.go b/bamboohr/bamboohr.go deleted file mode 100644 index 6f936b70..00000000 --- a/bamboohr/bamboohr.go +++ /dev/null @@ -1,18 +0,0 @@ -package bamboohr - -import ( -//"time" -) - -// TODO Move this into the client. Why is it separate? -//func Fetch() []Item { -//client := NewClient() -//result := client.Away("timeOff", today(), today()) - -//return result -//} - -//func today() string { -//localNow := time.Now().Local() -//return localNow.Format("2006-01-02") -//} diff --git a/bamboohr/widget.go b/bamboohr/widget.go index e5d62307..c65fb90a 100644 --- a/bamboohr/widget.go +++ b/bamboohr/widget.go @@ -10,26 +10,21 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget - Config *config.Config - View *tview.TextView + View *tview.TextView } -func NewWidget(config *config.Config) *Widget { - refreshInterval, err := config.Int("wtf.bamboohr.refreshInterval") - if err != nil { - refreshInterval = 1 - } - +func NewWidget() *Widget { widget := Widget{ BaseWidget: wtf.BaseWidget{ Name: "BambooHR", RefreshedAt: time.Now(), - RefreshInt: refreshInterval, + RefreshInt: Config.UInt("wtf.bamboohr.refreshInterval", 900), }, - Config: config, } widget.addView() @@ -41,7 +36,7 @@ func NewWidget(config *config.Config) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - url, _ := widget.Config.String("wtf.bamboohr.url") + url, _ := Config.String("wtf.bamboohr.url") client := NewClient(url) items := client.Away("timeOff", wtf.Today(), wtf.Today()) diff --git a/config.yml b/config.yml index a17c5e48..3c3cfa75 100644 --- a/config.yml +++ b/config.yml @@ -3,5 +3,20 @@ wtf: bamboohr: refreshInterval: 900 url: "https://api.bamboohr.com/api/gateway.php" + gcal: + refreshInterval: 300 + git: + refreshInterval: 8 + jira: + refreshInterval: 900 + opsgenie: + refreshInterval: 21600 + security: + refreshInterval: 3600 status: refreshInterval: 1 + weather: + cityId: 6173331 + language: "EN" + tempUnit: "C" + refreshInterval: 900 diff --git a/gcal/client.go b/gcal/client.go index f2577ad6..ef8ed024 100644 --- a/gcal/client.go +++ b/gcal/client.go @@ -23,6 +23,38 @@ import ( "google.golang.org/api/calendar/v3" ) +/* -------------------- Exported Functions -------------------- */ + +func Fetch() (*calendar.Events, error) { + ctx := context.Background() + + b, err := ioutil.ReadFile("./gcal/client_secret.json") + if err != nil { + return nil, err + } + + config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope) + if err != nil { + return nil, err + } + client := getClient(ctx, config) + + srv, err := calendar.New(client) + if err != nil { + return nil, err + } + + t := today().Format(time.RFC3339) + events, err := srv.Events.List("primary").ShowDeleted(false).SingleEvents(true).TimeMin(t).MaxResults(10).OrderBy("startTime").Do() + if err != nil { + return nil, err + } + + return events, err +} + +/* -------------------- Unexported Functions -------------------- */ + // getClient uses a Context and Config to retrieve a Token // then generate a Client. It returns the generated Client. func getClient(ctx context.Context, config *oauth2.Config) *http.Client { @@ -96,34 +128,6 @@ func saveToken(file string, token *oauth2.Token) { json.NewEncoder(f).Encode(token) } -func Fetch() (*calendar.Events, error) { - ctx := context.Background() - - b, err := ioutil.ReadFile("./gcal/client_secret.json") - if err != nil { - return nil, err - } - - config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope) - if err != nil { - return nil, err - } - client := getClient(ctx, config) - - srv, err := calendar.New(client) - if err != nil { - return nil, err - } - - t := today().Format(time.RFC3339) - events, err := srv.Events.List("primary").ShowDeleted(false).SingleEvents(true).TimeMin(t).MaxResults(10).OrderBy("startTime").Do() - if err != nil { - return nil, err - } - - return events, err -} - func today() time.Time { now := time.Now() return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) diff --git a/gcal/widget.go b/gcal/widget.go index a441e715..4c957de0 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -6,13 +6,17 @@ import ( "time" "github.com/gdamore/tcell" + "github.com/olebedev/config" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" "google.golang.org/api/calendar/v3" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget + View *tview.TextView } @@ -21,7 +25,7 @@ func NewWidget() *Widget { BaseWidget: wtf.BaseWidget{ Name: "Calendar", RefreshedAt: time.Now(), - RefreshInt: 300, + RefreshInt: Config.UInt("wtf.gcal.refreshInterval", 300), }, } diff --git a/git/widget.go b/git/widget.go index 4c83abdf..78098119 100644 --- a/git/widget.go +++ b/git/widget.go @@ -7,10 +7,13 @@ import ( "unicode/utf8" "github.com/gdamore/tcell" + "github.com/olebedev/config" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget View *tview.TextView @@ -21,7 +24,7 @@ func NewWidget() *Widget { BaseWidget: wtf.BaseWidget{ Name: "Git", RefreshedAt: time.Now(), - RefreshInt: 15, + RefreshInt: Config.UInt("wtf.git.refreshInterval", 15), }, } diff --git a/jira/widget.go b/jira/widget.go index 4c960be0..a89c3cf2 100644 --- a/jira/widget.go +++ b/jira/widget.go @@ -5,10 +5,13 @@ import ( "time" "github.com/gdamore/tcell" + "github.com/olebedev/config" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget View *tview.TextView @@ -19,7 +22,7 @@ func NewWidget() *Widget { BaseWidget: wtf.BaseWidget{ Name: "JIRA", RefreshedAt: time.Now(), - RefreshInt: 900, + RefreshInt: Config.UInt("wtf.jira.refreshInterval", 900), }, } diff --git a/opsgenie/widget.go b/opsgenie/widget.go index 090781dc..ff9917e4 100644 --- a/opsgenie/widget.go +++ b/opsgenie/widget.go @@ -6,10 +6,13 @@ import ( "time" "github.com/gdamore/tcell" + "github.com/olebedev/config" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget View *tview.TextView @@ -20,7 +23,7 @@ func NewWidget() *Widget { BaseWidget: wtf.BaseWidget{ Name: "OpsGenie", RefreshedAt: time.Now(), - RefreshInt: 21600, + RefreshInt: Config.UInt("wtf.opsgenie.refreshInterval", 21600), }, } diff --git a/security/widget.go b/security/widget.go index b983ab84..80e21508 100644 --- a/security/widget.go +++ b/security/widget.go @@ -6,10 +6,13 @@ import ( "time" "github.com/gdamore/tcell" + "github.com/olebedev/config" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget View *tview.TextView @@ -20,7 +23,7 @@ func NewWidget() *Widget { BaseWidget: wtf.BaseWidget{ Name: "Security", RefreshedAt: time.Now(), - RefreshInt: 3600, + RefreshInt: Config.UInt("wtf.security.refreshInterval", 3600), }, } diff --git a/status/widget.go b/status/widget.go index 439655be..8f9f2adb 100644 --- a/status/widget.go +++ b/status/widget.go @@ -10,27 +10,22 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget - Config *config.Config Current int View *tview.TextView } -func NewWidget(config *config.Config) *Widget { - refreshInterval, err := config.Int("wtf.status.refreshInterval") - if err != nil { - refreshInterval = 1 - } - +func NewWidget() *Widget { widget := Widget{ BaseWidget: wtf.BaseWidget{ Name: "Status", RefreshedAt: time.Now(), - RefreshInt: refreshInterval, + RefreshInt: Config.UInt("wtf.status.refreshInterval", 1), }, - Config: config, Current: 0, } diff --git a/weather/client.go b/weather/client.go index 765cd88b..467c9477 100644 --- a/weather/client.go +++ b/weather/client.go @@ -8,17 +8,15 @@ import ( /* -------------------- Exported Functions -------------------- */ -func Fetch() *owm.CurrentWeatherData { +func Fetch(cityID int) *owm.CurrentWeatherData { apiKey := os.Getenv("WTF_OWM_API_KEY") - vancouver := 6173331 - - return currentWeather(apiKey, vancouver) + return currentWeather(apiKey, cityID) } /* -------------------- Unexported Functions -------------------- */ func currentWeather(apiKey string, cityCode int) *owm.CurrentWeatherData { - weather, err := owm.NewCurrent("C", "EN", apiKey) + weather, err := owm.NewCurrent(Config.UString("wtf.weather.tempUnit", "C"), Config.UString("wtf.weather.language", "EN"), apiKey) if err != nil { panic(err) } diff --git a/weather/widget.go b/weather/widget.go index d01c2e73..b00d33ef 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -7,10 +7,13 @@ import ( owm "github.com/briandowns/openweathermap" "github.com/gdamore/tcell" + "github.com/olebedev/config" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" ) +var Config *config.Config + type Widget struct { wtf.BaseWidget View *tview.TextView @@ -21,7 +24,7 @@ func NewWidget() *Widget { BaseWidget: wtf.BaseWidget{ Name: "Weather", RefreshedAt: time.Now(), - RefreshInt: 900, + RefreshInt: Config.UInt("wtf.weather.refreshInterval", 900), }, } @@ -34,7 +37,7 @@ func NewWidget() *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - data := Fetch() + data := Fetch(Config.UInt("wtf.weather.cityId", 6176823)) widget.View.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name)) widget.RefreshedAt = time.Now() @@ -66,15 +69,18 @@ func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string { str = str + strings.Join(descs, ",") + "\n\n" - str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Current", data.Main.Temp) - str = str + fmt.Sprintf("%10s: %4.1f° C\n", "High", data.Main.TempMax) - str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Low", data.Main.TempMin) + tempUnit := Config.UString("wtf.weather.tempUnit", "C") + + str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "Current", data.Main.Temp, tempUnit) + str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "High", data.Main.TempMax, tempUnit) + str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "Low", data.Main.TempMin, tempUnit) return str } // icon returns an emoji for the current weather // src: https://github.com/chubin/wttr.in/blob/master/share/translations/en.txt +// Note: these only work for English weather status. Sorry about that func icon(data *owm.CurrentWeatherData) string { var icon string diff --git a/wtf.go b/wtf.go index 9b16846c..33f0411c 100644 --- a/wtf.go +++ b/wtf.go @@ -47,27 +47,35 @@ func refresher(stat *status.Widget, app *tview.Application) { } func main() { - bamboo := bamboohr.NewWidget(Config) + bamboohr.Config = Config + bamboo := bamboohr.NewWidget() bamboo.Refresh() + gcal.Config = Config cal := gcal.NewWidget() cal.Refresh() + git.Config = Config git := git.NewWidget() git.Refresh() + jira.Config = Config jira := jira.NewWidget() jira.Refresh() + opsgenie.Config = Config opsgenie := opsgenie.NewWidget() opsgenie.Refresh() + security.Config = Config sec := security.NewWidget() sec.Refresh() - stat := status.NewWidget(Config) + status.Config = Config + stat := status.NewWidget() stat.Refresh() + weather.Config = Config weather := weather.NewWidget() weather.Refresh()