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

Improve the config file handling process

* Don't create a default config if a custom config is being passed in
* Textfile: don't die if the file cannot be found
This commit is contained in:
Chris Cummer 2019-08-02 01:49:05 -07:00
parent 45da40e4c9
commit 4c2b52cdbb
3 changed files with 28 additions and 13 deletions

View File

@ -57,12 +57,20 @@ func CreateFile(fileName string) (string, error) {
// Initialize takes care of settings up the initial state of WTF configuration // Initialize takes care of settings up the initial state of WTF configuration
// It ensures necessary directories and files exist // It ensures necessary directories and files exist
func Initialize() { func Initialize(hasCustom bool) {
migrateOldConfig() if hasCustom == false {
migrateOldConfig()
}
// These always get created because this is where modules should write any permanent
// data they need to persist between runs (i.e.: log, textfile, etc.)
createXdgConfigDir() createXdgConfigDir()
createWtfConfigDir() createWtfConfigDir()
createWtfConfigFile()
chmodConfigFile() if hasCustom == false {
createWtfConfigFile()
chmodConfigFile()
}
} }
// WtfConfigDir returns the absolute path to the configuration directory // WtfConfigDir returns the absolute path to the configuration directory

View File

@ -46,14 +46,16 @@ func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
// Manage the configuration directories and config file // Manage the configuration directories and config file
cfg.Initialize()
// Parse and handle flags // Parse and handle flags
flags := flags.NewFlags() flags := flags.NewFlags()
flags.Parse() flags.Parse()
hasCustom := flags.HasCustomConfig()
cfg.Initialize(hasCustom)
// Load the configuration file // Load the configuration file
config := cfg.LoadWtfConfigFile(flags.ConfigFilePath(), flags.HasCustomConfig()) config := cfg.LoadWtfConfigFile(flags.ConfigFilePath(), hasCustom)
flags.RenderIf(version, config) flags.RenderIf(version, config)
if flags.Profile { if flags.Profile {
@ -66,7 +68,7 @@ func main() {
// Build the application // Build the application
tviewApp = tview.NewApplication() tviewApp = tview.NewApplication()
wtfApp := app.NewWtfApp(tviewApp, config, flags.Config, flags.HasCustomConfig()) wtfApp := app.NewWtfApp(tviewApp, config, flags.Config, hasCustom)
wtfApp.Start() wtfApp.Start()
if err := tviewApp.Run(); err != nil { if err := tviewApp.Run(); err != nil {

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -18,6 +17,10 @@ import (
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
) )
const (
pollingIntervalms = 100
)
type Widget struct { type Widget struct {
wtf.KeyboardWidget wtf.KeyboardWidget
wtf.MultiSourceWidget wtf.MultiSourceWidget
@ -137,7 +140,8 @@ func (widget *Widget) watchForFileChanges() {
case <-watch.Event: case <-watch.Event:
widget.display() widget.display()
case err := <-watch.Error: case err := <-watch.Error:
log.Fatalln(err) fmt.Println(err)
os.Exit(1)
case <-watch.Closed: case <-watch.Closed:
return return
} }
@ -149,13 +153,14 @@ func (widget *Widget) watchForFileChanges() {
fullPath, err := utils.ExpandHomeDir(source) fullPath, err := utils.ExpandHomeDir(source)
if err == nil { if err == nil {
if err := watch.Add(fullPath); err != nil { if err := watch.Add(fullPath); err != nil {
log.Fatalln(err) // Ignore it, don't care about a file that doesn't exist
} }
} }
} }
// Start the watching process - it'll check for changes every 100ms. // Start the watching process - it'll check for changes every pollingIntervalms.
if err := watch.Start(time.Millisecond * 100); err != nil { if err := watch.Start(time.Millisecond * pollingIntervalms); err != nil {
log.Fatalln(err) fmt.Println(err)
os.Exit(1)
} }
} }