1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Configuration used throughout the app

This commit is contained in:
Chris Cummer 2018-04-04 14:30:05 -07:00 committed by Chris Cummer
parent 45d88c6700
commit c3f1d7ee36
13 changed files with 102 additions and 83 deletions

View File

@ -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")
//}

View File

@ -10,26 +10,21 @@ import (
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
Config *config.Config View *tview.TextView
View *tview.TextView
} }
func NewWidget(config *config.Config) *Widget { func NewWidget() *Widget {
refreshInterval, err := config.Int("wtf.bamboohr.refreshInterval")
if err != nil {
refreshInterval = 1
}
widget := Widget{ widget := Widget{
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "BambooHR", Name: "BambooHR",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: refreshInterval, RefreshInt: Config.UInt("wtf.bamboohr.refreshInterval", 900),
}, },
Config: config,
} }
widget.addView() widget.addView()
@ -41,7 +36,7 @@ func NewWidget(config *config.Config) *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
url, _ := widget.Config.String("wtf.bamboohr.url") url, _ := Config.String("wtf.bamboohr.url")
client := NewClient(url) client := NewClient(url)
items := client.Away("timeOff", wtf.Today(), wtf.Today()) items := client.Away("timeOff", wtf.Today(), wtf.Today())

View File

@ -3,5 +3,20 @@ wtf:
bamboohr: bamboohr:
refreshInterval: 900 refreshInterval: 900
url: "https://api.bamboohr.com/api/gateway.php" url: "https://api.bamboohr.com/api/gateway.php"
gcal:
refreshInterval: 300
git:
refreshInterval: 8
jira:
refreshInterval: 900
opsgenie:
refreshInterval: 21600
security:
refreshInterval: 3600
status: status:
refreshInterval: 1 refreshInterval: 1
weather:
cityId: 6173331
language: "EN"
tempUnit: "C"
refreshInterval: 900

View File

@ -23,6 +23,38 @@ import (
"google.golang.org/api/calendar/v3" "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 // getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client. // then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.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) 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 { func today() time.Time {
now := time.Now() now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())

View File

@ -6,13 +6,17 @@ import (
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
"google.golang.org/api/calendar/v3" "google.golang.org/api/calendar/v3"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
View *tview.TextView View *tview.TextView
} }
@ -21,7 +25,7 @@ func NewWidget() *Widget {
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "Calendar", Name: "Calendar",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: 300, RefreshInt: Config.UInt("wtf.gcal.refreshInterval", 300),
}, },
} }

View File

@ -7,10 +7,13 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
View *tview.TextView View *tview.TextView
@ -21,7 +24,7 @@ func NewWidget() *Widget {
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "Git", Name: "Git",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: 15, RefreshInt: Config.UInt("wtf.git.refreshInterval", 15),
}, },
} }

View File

@ -5,10 +5,13 @@ import (
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
View *tview.TextView View *tview.TextView
@ -19,7 +22,7 @@ func NewWidget() *Widget {
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "JIRA", Name: "JIRA",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: 900, RefreshInt: Config.UInt("wtf.jira.refreshInterval", 900),
}, },
} }

View File

@ -6,10 +6,13 @@ import (
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
View *tview.TextView View *tview.TextView
@ -20,7 +23,7 @@ func NewWidget() *Widget {
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "OpsGenie", Name: "OpsGenie",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: 21600, RefreshInt: Config.UInt("wtf.opsgenie.refreshInterval", 21600),
}, },
} }

View File

@ -6,10 +6,13 @@ import (
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
View *tview.TextView View *tview.TextView
@ -20,7 +23,7 @@ func NewWidget() *Widget {
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "Security", Name: "Security",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: 3600, RefreshInt: Config.UInt("wtf.security.refreshInterval", 3600),
}, },
} }

View File

@ -10,27 +10,22 @@ import (
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
Config *config.Config
Current int Current int
View *tview.TextView View *tview.TextView
} }
func NewWidget(config *config.Config) *Widget { func NewWidget() *Widget {
refreshInterval, err := config.Int("wtf.status.refreshInterval")
if err != nil {
refreshInterval = 1
}
widget := Widget{ widget := Widget{
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "Status", Name: "Status",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: refreshInterval, RefreshInt: Config.UInt("wtf.status.refreshInterval", 1),
}, },
Config: config,
Current: 0, Current: 0,
} }

View File

@ -8,17 +8,15 @@ import (
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func Fetch() *owm.CurrentWeatherData { func Fetch(cityID int) *owm.CurrentWeatherData {
apiKey := os.Getenv("WTF_OWM_API_KEY") apiKey := os.Getenv("WTF_OWM_API_KEY")
vancouver := 6173331 return currentWeather(apiKey, cityID)
return currentWeather(apiKey, vancouver)
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func currentWeather(apiKey string, cityCode int) *owm.CurrentWeatherData { 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 { if err != nil {
panic(err) panic(err)
} }

View File

@ -7,10 +7,13 @@ import (
owm "github.com/briandowns/openweathermap" owm "github.com/briandowns/openweathermap"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
var Config *config.Config
type Widget struct { type Widget struct {
wtf.BaseWidget wtf.BaseWidget
View *tview.TextView View *tview.TextView
@ -21,7 +24,7 @@ func NewWidget() *Widget {
BaseWidget: wtf.BaseWidget{ BaseWidget: wtf.BaseWidget{
Name: "Weather", Name: "Weather",
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInt: 900, RefreshInt: Config.UInt("wtf.weather.refreshInterval", 900),
}, },
} }
@ -34,7 +37,7 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { 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.View.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name))
widget.RefreshedAt = time.Now() 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 + strings.Join(descs, ",") + "\n\n"
str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Current", data.Main.Temp) tempUnit := Config.UString("wtf.weather.tempUnit", "C")
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) 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 return str
} }
// icon returns an emoji for the current weather // icon returns an emoji for the current weather
// src: https://github.com/chubin/wttr.in/blob/master/share/translations/en.txt // 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 { func icon(data *owm.CurrentWeatherData) string {
var icon string var icon string

12
wtf.go
View File

@ -47,27 +47,35 @@ func refresher(stat *status.Widget, app *tview.Application) {
} }
func main() { func main() {
bamboo := bamboohr.NewWidget(Config) bamboohr.Config = Config
bamboo := bamboohr.NewWidget()
bamboo.Refresh() bamboo.Refresh()
gcal.Config = Config
cal := gcal.NewWidget() cal := gcal.NewWidget()
cal.Refresh() cal.Refresh()
git.Config = Config
git := git.NewWidget() git := git.NewWidget()
git.Refresh() git.Refresh()
jira.Config = Config
jira := jira.NewWidget() jira := jira.NewWidget()
jira.Refresh() jira.Refresh()
opsgenie.Config = Config
opsgenie := opsgenie.NewWidget() opsgenie := opsgenie.NewWidget()
opsgenie.Refresh() opsgenie.Refresh()
security.Config = Config
sec := security.NewWidget() sec := security.NewWidget()
sec.Refresh() sec.Refresh()
stat := status.NewWidget(Config) status.Config = Config
stat := status.NewWidget()
stat.Refresh() stat.Refresh()
weather.Config = Config
weather := weather.NewWidget() weather := weather.NewWidget()
weather.Refresh() weather.Refresh()