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:
parent
45da40e4c9
commit
4c2b52cdbb
@ -57,13 +57,21 @@ func CreateFile(fileName string) (string, error) {
|
||||
|
||||
// Initialize takes care of settings up the initial state of WTF configuration
|
||||
// It ensures necessary directories and files exist
|
||||
func Initialize() {
|
||||
func Initialize(hasCustom bool) {
|
||||
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()
|
||||
createWtfConfigDir()
|
||||
|
||||
if hasCustom == false {
|
||||
createWtfConfigFile()
|
||||
chmodConfigFile()
|
||||
}
|
||||
}
|
||||
|
||||
// WtfConfigDir returns the absolute path to the configuration directory
|
||||
func WtfConfigDir() (string, error) {
|
||||
|
8
main.go
8
main.go
@ -46,14 +46,16 @@ func main() {
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
// Manage the configuration directories and config file
|
||||
cfg.Initialize()
|
||||
|
||||
// Parse and handle flags
|
||||
flags := flags.NewFlags()
|
||||
flags.Parse()
|
||||
|
||||
hasCustom := flags.HasCustomConfig()
|
||||
cfg.Initialize(hasCustom)
|
||||
|
||||
// Load the configuration file
|
||||
config := cfg.LoadWtfConfigFile(flags.ConfigFilePath(), flags.HasCustomConfig())
|
||||
config := cfg.LoadWtfConfigFile(flags.ConfigFilePath(), hasCustom)
|
||||
flags.RenderIf(version, config)
|
||||
|
||||
if flags.Profile {
|
||||
@ -66,7 +68,7 @@ func main() {
|
||||
|
||||
// Build the application
|
||||
tviewApp = tview.NewApplication()
|
||||
wtfApp := app.NewWtfApp(tviewApp, config, flags.Config, flags.HasCustomConfig())
|
||||
wtfApp := app.NewWtfApp(tviewApp, config, flags.Config, hasCustom)
|
||||
wtfApp.Start()
|
||||
|
||||
if err := tviewApp.Run(); err != nil {
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@ -18,6 +17,10 @@ import (
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
const (
|
||||
pollingIntervalms = 100
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.KeyboardWidget
|
||||
wtf.MultiSourceWidget
|
||||
@ -137,7 +140,8 @@ func (widget *Widget) watchForFileChanges() {
|
||||
case <-watch.Event:
|
||||
widget.display()
|
||||
case err := <-watch.Error:
|
||||
log.Fatalln(err)
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
case <-watch.Closed:
|
||||
return
|
||||
}
|
||||
@ -149,13 +153,14 @@ func (widget *Widget) watchForFileChanges() {
|
||||
fullPath, err := utils.ExpandHomeDir(source)
|
||||
if 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.
|
||||
if err := watch.Start(time.Millisecond * 100); err != nil {
|
||||
log.Fatalln(err)
|
||||
// Start the watching process - it'll check for changes every pollingIntervalms.
|
||||
if err := watch.Start(time.Millisecond * pollingIntervalms); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user