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

Clean up the flag and config handling in main()

This commit is contained in:
Chris Cummer 2018-06-16 09:13:23 -07:00
parent dd42080ed8
commit 4496cc7c31
3 changed files with 61 additions and 55 deletions

View File

@ -30,6 +30,25 @@ func CreateConfigDir() {
}
}
// CreateConfigFile creates a simple config file in the config directory if
// one does not already exist
func CreateConfigFile() {
filePath, err := CreateFile("config.yml")
if err != nil {
panic(err)
}
// If the file is empty, write to it
file, err := os.Stat(filePath)
if file.Size() == 0 {
err = ioutil.WriteFile(filePath, []byte(simpleConfig), 0644)
if err != nil {
panic(err)
}
}
}
// CreateFile creates the named file in the config directory, if it does not already exist.
// If the file exists it does not recreate it.
// If successful, eturns the absolute path to the file
@ -88,25 +107,6 @@ func ReadConfigFile(fileName string) (string, error) {
return string(fileData), nil
}
// WriteConfigFile creates a simple config file in the config directory if
// one does not already exist
func WriteConfigFile() {
filePath, err := CreateFile("config.yml")
if err != nil {
panic(err)
}
// If the file is empty, write to it
file, err := os.Stat(filePath)
if file.Size() == 0 {
err = ioutil.WriteFile(filePath, []byte(simpleConfig), 0644)
if err != nil {
panic(err)
}
}
}
const simpleConfig = `wtf:
colors:
border:

View File

@ -22,6 +22,10 @@ func NewFlags() *Flags {
/* -------------------- Exported Functions -------------------- */
func (flags *Flags) ConfigFilePath() string {
return flags.Config
}
func (flags *Flags) HasConfig() bool {
return len(flags.Config) > 0
}
@ -42,6 +46,8 @@ func (flags *Flags) Parse(version string) {
}
}
// If no config file is explicitly passed in as a param,
// set the flag to the default config file
if !flags.HasConfig() {
homeDir, err := wtf.Home()
if err != nil {

72
wtf.go
View File

@ -86,8 +86,38 @@ func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
return event
}
func loadConfig(filePath string) {
func loadConfigFile(filePath string) {
Config = cfg.LoadConfigFile(filePath)
// Always in alphabetical order
bamboohr.Config = Config
bargraph.Config = Config
bittrex.Config = Config
blockfolio.Config = Config
circleci.Config = Config
clocks.Config = Config
cmdrunner.Config = Config
cryptolive.Config = Config
gcal.Config = Config
git.Config = Config
github.Config = Config
gitlab.Config = Config
gspreadsheets.Config = Config
ipapi.Config = Config
ipinfo.Config = Config
jenkins.Config = Config
jira.Config = Config
newrelic.Config = Config
opsgenie.Config = Config
power.Config = Config
prettyweather.Config = Config
security.Config = Config
status.Config = Config
system.Config = Config
textfile.Config = Config
todo.Config = Config
weather.Config = Config
wtf.Config = Config
}
// redrawApp redraws the rendered views to screen on a defined interval (set in config.yml)
@ -119,7 +149,7 @@ func setTerm() {
os.Setenv("TERM", Config.UString("wtf.term", os.Getenv("TERM")))
}
func watchForConfigChanges(app *tview.Application, configFlag string, grid *tview.Grid, pages *tview.Pages) {
func watchForConfigChanges(app *tview.Application, configFilePath string, grid *tview.Grid, pages *tview.Pages) {
watch := watcher.New()
// notify write events.
@ -129,7 +159,7 @@ func watchForConfigChanges(app *tview.Application, configFlag string, grid *tvie
for {
select {
case <-watch.Event:
loadConfig(configFlag)
loadConfigFile(configFilePath)
// Disable all widgets to stop scheduler goroutines and rmeove widgets from memory.
disableAllWidgets()
makeWidgets(app, pages)
@ -145,7 +175,7 @@ func watchForConfigChanges(app *tview.Application, configFlag string, grid *tvie
}()
// Watch config file for changes.
if err := watch.Add(configFlag); err != nil {
if err := watch.Add(configFilePath); err != nil {
log.Fatalln(err)
}
@ -217,36 +247,6 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) {
}
func makeWidgets(app *tview.Application, pages *tview.Pages) {
// Always in alphabetical order
bamboohr.Config = Config
bargraph.Config = Config
bittrex.Config = Config
blockfolio.Config = Config
circleci.Config = Config
clocks.Config = Config
cmdrunner.Config = Config
cryptolive.Config = Config
gcal.Config = Config
git.Config = Config
github.Config = Config
gitlab.Config = Config
gspreadsheets.Config = Config
ipapi.Config = Config
ipinfo.Config = Config
jenkins.Config = Config
jira.Config = Config
newrelic.Config = Config
opsgenie.Config = Config
power.Config = Config
prettyweather.Config = Config
security.Config = Config
status.Config = Config
system.Config = Config
textfile.Config = Config
todo.Config = Config
weather.Config = Config
wtf.Config = Config
mods, _ := Config.Map("wtf.mods")
for mod := range mods {
if enabled := Config.UBool("wtf.mods."+mod+".enabled", false); enabled {
@ -275,9 +275,9 @@ func main() {
}
cfg.CreateConfigDir()
cfg.WriteConfigFile()
cfg.CreateConfigFile()
loadConfigFile(flags.ConfigFilePath())
loadConfig(flags.Config)
setTerm()
app := tview.NewApplication()