From 4c2b52cdbbcd6786e37708c2dababff750640193 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 2 Aug 2019 01:49:05 -0700 Subject: [PATCH 1/6] 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 --- cfg/config_files.go | 16 ++++++++++++---- main.go | 8 +++++--- modules/textfile/widget.go | 17 +++++++++++------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/cfg/config_files.go b/cfg/config_files.go index 2fa6ca34..f2ad94ea 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -57,12 +57,20 @@ 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() { - migrateOldConfig() +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() - createWtfConfigFile() - chmodConfigFile() + + if hasCustom == false { + createWtfConfigFile() + chmodConfigFile() + } } // WtfConfigDir returns the absolute path to the configuration directory diff --git a/main.go b/main.go index 381820e2..84726545 100644 --- a/main.go +++ b/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 { diff --git a/modules/textfile/widget.go b/modules/textfile/widget.go index 558a10a5..39e33c0d 100644 --- a/modules/textfile/widget.go +++ b/modules/textfile/widget.go @@ -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) } } From 52023a4db0cdf14fa68bdd66569939a396cbcc4f Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 2 Aug 2019 02:00:24 -0700 Subject: [PATCH 2/6] Remove a stray Log() statement from MultiSourceWidget --- wtf/multisource_widget.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/wtf/multisource_widget.go b/wtf/multisource_widget.go index ae83b0f0..8bfa8cbd 100644 --- a/wtf/multisource_widget.go +++ b/wtf/multisource_widget.go @@ -1,11 +1,10 @@ package wtf import ( - "fmt" "github.com/wtfutil/wtf/cfg" - "github.com/wtfutil/wtf/logger" ) +// MultiSourceWidget is a widget that supports displaying data from multiple sources type MultiSourceWidget struct { moduleConfig *cfg.Common singular string @@ -26,8 +25,6 @@ func NewMultiSourceWidget(moduleConfig *cfg.Common, singular, plural string) Mul widget.loadSources() - logger.Log(fmt.Sprintf("%+v", widget.Sources)) - return widget } @@ -42,7 +39,7 @@ func (widget *MultiSourceWidget) CurrentSource() string { return widget.Sources[widget.Idx] } -// Next displays the next source in the source list. If the current source is the last +// NextSource displays the next source in the source list. If the current source is the last // source it wraps around to the first source func (widget *MultiSourceWidget) NextSource() { widget.Idx++ @@ -55,7 +52,7 @@ func (widget *MultiSourceWidget) NextSource() { } } -// Prev displays the previous source in the source list. If the current source is the first +// PrevSource displays the previous source in the source list. If the current source is the first // source, it wraps around to the last source func (widget *MultiSourceWidget) PrevSource() { widget.Idx-- From a02e2a22475f3d7a2d96c0c0933ee4317127b98b Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 2 Aug 2019 02:01:07 -0700 Subject: [PATCH 3/6] Remove a stray Log() statement from Gittet --- modules/gitter/client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/gitter/client.go b/modules/gitter/client.go index 0eec0343..13554533 100644 --- a/modules/gitter/client.go +++ b/modules/gitter/client.go @@ -35,7 +35,6 @@ func GetRoom(roomUri, apiToken string) (*Room, error) { parseJson(&rooms, resp.Body) for _, room := range rooms.Results { - logger.Log(fmt.Sprintf("room: %s", room)) if room.URI == roomUri { return &room, nil } From 7234f8307300f0c9ef8693714c98cd626f3e40d0 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 2 Aug 2019 02:03:44 -0700 Subject: [PATCH 4/6] Remove stray Log() statements from SpotifyWeb --- modules/gitter/client.go | 1 - modules/spotifyweb/widget.go | 11 ++++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/modules/gitter/client.go b/modules/gitter/client.go index 13554533..af43ecc7 100644 --- a/modules/gitter/client.go +++ b/modules/gitter/client.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/wtfutil/wtf/logger" "io" "io/ioutil" "net/http" diff --git a/modules/spotifyweb/widget.go b/modules/spotifyweb/widget.go index ca47a9c8..5b1027ce 100644 --- a/modules/spotifyweb/widget.go +++ b/modules/spotifyweb/widget.go @@ -4,9 +4,9 @@ import ( "errors" "fmt" "net/http" + "os" "github.com/rivo/tview" - "github.com/wtfutil/wtf/logger" "github.com/wtfutil/wtf/wtf" "github.com/zmb3/spotify" ) @@ -43,15 +43,12 @@ var ( ) func authHandler(w http.ResponseWriter, r *http.Request) { - logger.Log("[SpotifyWeb] Got an authentication hit!") tok, err := auth.Token(state, r) if err != nil { http.Error(w, "Couldn't get token", http.StatusForbidden) - logger.Log(err.Error()) } if st := r.FormValue("state"); st != state { http.NotFound(w, r) - logger.Log(fmt.Sprintf("State mismatch: %s != %s\n", st, state)) } // use the token to get an authenticated client client := auth.NewClient(tok) @@ -87,7 +84,6 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * go func() { // wait for auth to complete - logger.Log("[SpotifyWeb] Waiting for authentication... URL: " + authURL) client = <-tempClientChan // use the client to make calls that require authorization @@ -98,9 +94,10 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * playerState, err = client.PlayerState() if err != nil { - panic(err) + fmt.Println(err) + os.Exit(1) } - logger.Log("[SpotifyWeb] Authentication complete.") + widget.client = client widget.playerState = playerState widget.Refresh() From 7b4c77b7da2f04c99d87e9678334a20539e4641a Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 2 Aug 2019 02:26:11 -0700 Subject: [PATCH 5/6] Simplify .travis.yml config --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c78203f0..77612619 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,4 @@ before_install: - cd $HOME/gopath/src/github.com/wtfutil/wtf - export GOPROXY="https://gocenter.io" && export GO111MODULE=on -script: go get ./... && go get github.com/go-test/deep && go test -v github.com/wtfutil/wtf/wtf_tests/... +script: go get ./... && go test -v github.com/wtfutil/wtf/... \ No newline at end of file From b9d10177540cc1f4f32cfac600864900caa127df Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 3 Aug 2019 12:27:38 -0700 Subject: [PATCH 6/6] Remove another panic call --- cfg/config_files.go | 3 ++- cfg/error_messages.go | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cfg/config_files.go b/cfg/config_files.go index f2ad94ea..72a62f6f 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -152,7 +152,8 @@ func createWtfConfigDir() { func createWtfConfigFile() { filePath, err := CreateFile(WtfConfigFile) if err != nil { - panic(err) + displayDefaultConfigCreateError(err) + os.Exit(1) } // If the file is empty, write to it diff --git a/cfg/error_messages.go b/cfg/error_messages.go index 55be87c8..435b624f 100644 --- a/cfg/error_messages.go +++ b/cfg/error_messages.go @@ -18,8 +18,14 @@ func displayError(err error) { fmt.Printf("%s %s\n\n", aurora.Red("Error:"), err.Error()) } +func displayDefaultConfigCreateError(err error) { + fmt.Printf("\n%s Could not create the default configuration file.\n", aurora.Red("ERROR")) + fmt.Println() + displayError(err) +} + func displayDefaultConfigWriteError(err error) { - fmt.Printf("\n%s Could not write the default configuration.\n", aurora.Red("ERROR")) + fmt.Printf("\n%s Could not write the default configuration file.\n", aurora.Red("ERROR")) fmt.Println() displayError(err) }