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

Merge pull request #522 from wtfutil/20190802-custom-config-handling

Improve the config file handling process
This commit is contained in:
Chris Cummer 2019-08-03 12:33:16 -07:00 committed by GitHub
commit 33ec8528a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 31 deletions

View File

@ -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/...

View File

@ -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
@ -144,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

View File

@ -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)
}

View File

@ -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 {

View File

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/wtfutil/wtf/logger"
"io"
"io/ioutil"
"net/http"
@ -35,7 +34,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
}

View File

@ -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()

View File

@ -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)
}
}

View File

@ -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--