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

Watch config file for changes

This commit is contained in:
Hossein Mehrabi 2018-05-28 22:31:43 +04:30
parent eaa89c43f6
commit 6870bc5e4f

132
wtf.go
View File

@ -2,11 +2,15 @@ package main
import ( import (
"flag" "flag"
"log"
"os" "os"
"os/user"
"path/filepath"
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/radovskyb/watcher"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/bamboohr" "github.com/senorprogrammer/wtf/bamboohr"
"github.com/senorprogrammer/wtf/clocks" "github.com/senorprogrammer/wtf/clocks"
@ -109,6 +113,8 @@ func refreshAllWidgets() {
var Config *config.Config var Config *config.Config
var FocusTracker wtf.FocusTracker var FocusTracker wtf.FocusTracker
var Widgets []wtf.Wtfable var Widgets []wtf.Wtfable
var Pages *tview.Pages
var MainPage *tview.Grid
var ( var (
commit = "dev" commit = "dev"
@ -116,39 +122,48 @@ var (
version = "dev" version = "dev"
) )
func main() { func watchForConfigChanges(app *tview.Application, configFlag *string) {
/* w := watcher.New()
This allows the user to pass flags in however they prefer. It supports the likes of:
wtf -help | --help // notify write events.
wtf -version | --version w.FilterOps(watcher.Write)
*/
flagConf := flag.String("config", "~/.wtf/config.yml", "Path to config file")
flagHelp := flag.Bool("help", false, "Show help")
flagVers := flag.Bool("version", false, "Show version info")
flag.Parse() go func() {
for {
select {
case <-w.Event:
Pages.RemovePage("grid")
loadConfig(configFlag)
makeWidgets(app)
MainPage = buildGrid(Widgets)
Pages.AddPage("grid", MainPage, true, true)
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
if *flagHelp { // Watch config file for changes.
help.DisplayHelpInfo(flag.Args()) if err := w.Add(*configFlag); err != nil {
log.Fatalln(err)
} }
if *flagVers { // Trigger 2 events after watcher started.
help.DisplayVersionInfo(version) // go func() {
// w.Wait()
// w.TriggerEvent(watcher.Create, nil)
// w.TriggerEvent(watcher.Remove, nil)
// }()
// Start the watching process - it'll check for changes every 100ms.
if err := w.Start(time.Millisecond * 100); err != nil {
log.Fatalln(err)
}
} }
/* -------------------- end flag parsing and handling -------------------- */ func makeWidgets(app *tview.Application) {
// Responsible for creating the configuration directory and default
// configuration file if they don't already exist
wtf.CreateConfigDir()
wtf.WriteConfigFile()
Config = wtf.LoadConfigFile(*flagConf)
app := tview.NewApplication()
pages := tview.NewPages()
bamboohr.Config = Config bamboohr.Config = Config
clocks.Config = Config clocks.Config = Config
cmdrunner.Config = Config cmdrunner.Config = Config
@ -172,8 +187,8 @@ func main() {
clocks.NewWidget(), clocks.NewWidget(),
cmdrunner.NewWidget(), cmdrunner.NewWidget(),
gcal.NewWidget(), gcal.NewWidget(),
git.NewWidget(app, pages), git.NewWidget(app, Pages),
github.NewWidget(app, pages), github.NewWidget(app, Pages),
jira.NewWidget(), jira.NewWidget(),
newrelic.NewWidget(), newrelic.NewWidget(),
opsgenie.NewWidget(), opsgenie.NewWidget(),
@ -181,10 +196,58 @@ func main() {
security.NewWidget(), security.NewWidget(),
status.NewWidget(), status.NewWidget(),
system.NewWidget(date, version), system.NewWidget(date, version),
textfile.NewWidget(app, pages), textfile.NewWidget(app, Pages),
todo.NewWidget(app, pages), todo.NewWidget(app, Pages),
weather.NewWidget(app, pages), weather.NewWidget(app, Pages),
} }
}
func homeDir() string {
u, err := user.Current()
if err != nil {
return "~/"
}
return u.HomeDir
}
func loadConfig(configFlag *string) {
Config = wtf.LoadConfigFile(*configFlag)
}
func main() {
/*
This allows the user to pass flags in however they prefer. It supports the likes of:
wtf -help | --help
wtf -version | --version
*/
flagConf := flag.String("config", filepath.Join(homeDir(), ".wtf", "config.yml"), "Path to config file")
flagHelp := flag.Bool("help", false, "Show help")
flagVers := flag.Bool("version", false, "Show version info")
flag.Parse()
if *flagHelp {
help.DisplayHelpInfo(flag.Args())
}
if *flagVers {
help.DisplayVersionInfo(version)
}
/* -------------------- end flag parsing and handling -------------------- */
// Responsible for creating the configuration directory and default
// configuration file if they don't already exist
wtf.CreateConfigDir()
wtf.WriteConfigFile()
Config = wtf.LoadConfigFile(*flagConf)
app := tview.NewApplication()
Pages = tview.NewPages()
makeWidgets(app)
FocusTracker = wtf.FocusTracker{ FocusTracker = wtf.FocusTracker{
App: app, App: app,
@ -194,12 +257,13 @@ func main() {
// Loop in a routine to redraw the screen // Loop in a routine to redraw the screen
go redrawApp(app) go redrawApp(app)
go watchForConfigChanges(app, flagConf)
grid := buildGrid(Widgets) MainPage = buildGrid(Widgets)
pages.AddPage("grid", grid, true, true) Pages.AddPage("grid", MainPage, true, true)
app.SetInputCapture(keyboardIntercept) app.SetInputCapture(keyboardIntercept)
if err := app.SetRoot(pages, true).Run(); err != nil { if err := app.SetRoot(Pages, true).Run(); err != nil {
os.Exit(1) os.Exit(1)
} }
} }