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:
parent
45d88c6700
commit
c3f1d7ee36
@ -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")
|
||||
//}
|
@ -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())
|
||||
|
15
config.yml
15
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
|
||||
|
@ -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())
|
||||
|
@ -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),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
12
wtf.go
12
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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user