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:
commit
33ec8528a1
@ -11,4 +11,4 @@ before_install:
|
|||||||
- cd $HOME/gopath/src/github.com/wtfutil/wtf
|
- cd $HOME/gopath/src/github.com/wtfutil/wtf
|
||||||
- export GOPROXY="https://gocenter.io" && export GO111MODULE=on
|
- 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/...
|
@ -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) {
|
||||||
|
if hasCustom == false {
|
||||||
migrateOldConfig()
|
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()
|
||||||
|
|
||||||
|
if hasCustom == false {
|
||||||
createWtfConfigFile()
|
createWtfConfigFile()
|
||||||
chmodConfigFile()
|
chmodConfigFile()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WtfConfigDir returns the absolute path to the configuration directory
|
// WtfConfigDir returns the absolute path to the configuration directory
|
||||||
@ -144,7 +152,8 @@ func createWtfConfigDir() {
|
|||||||
func createWtfConfigFile() {
|
func createWtfConfigFile() {
|
||||||
filePath, err := CreateFile(WtfConfigFile)
|
filePath, err := CreateFile(WtfConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
displayDefaultConfigCreateError(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the file is empty, write to it
|
// If the file is empty, write to it
|
||||||
|
@ -18,8 +18,14 @@ func displayError(err error) {
|
|||||||
fmt.Printf("%s %s\n\n", aurora.Red("Error:"), 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) {
|
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()
|
fmt.Println()
|
||||||
displayError(err)
|
displayError(err)
|
||||||
}
|
}
|
||||||
|
8
main.go
8
main.go
@ -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 {
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/wtfutil/wtf/logger"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -35,7 +34,6 @@ func GetRoom(roomUri, apiToken string) (*Room, error) {
|
|||||||
parseJson(&rooms, resp.Body)
|
parseJson(&rooms, resp.Body)
|
||||||
|
|
||||||
for _, room := range rooms.Results {
|
for _, room := range rooms.Results {
|
||||||
logger.Log(fmt.Sprintf("room: %s", room))
|
|
||||||
if room.URI == roomUri {
|
if room.URI == roomUri {
|
||||||
return &room, nil
|
return &room, nil
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/logger"
|
|
||||||
"github.com/wtfutil/wtf/wtf"
|
"github.com/wtfutil/wtf/wtf"
|
||||||
"github.com/zmb3/spotify"
|
"github.com/zmb3/spotify"
|
||||||
)
|
)
|
||||||
@ -43,15 +43,12 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func authHandler(w http.ResponseWriter, r *http.Request) {
|
func authHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
logger.Log("[SpotifyWeb] Got an authentication hit!")
|
|
||||||
tok, err := auth.Token(state, r)
|
tok, err := auth.Token(state, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Couldn't get token", http.StatusForbidden)
|
http.Error(w, "Couldn't get token", http.StatusForbidden)
|
||||||
logger.Log(err.Error())
|
|
||||||
}
|
}
|
||||||
if st := r.FormValue("state"); st != state {
|
if st := r.FormValue("state"); st != state {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
logger.Log(fmt.Sprintf("State mismatch: %s != %s\n", st, state))
|
|
||||||
}
|
}
|
||||||
// use the token to get an authenticated client
|
// use the token to get an authenticated client
|
||||||
client := auth.NewClient(tok)
|
client := auth.NewClient(tok)
|
||||||
@ -87,7 +84,6 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// wait for auth to complete
|
// wait for auth to complete
|
||||||
logger.Log("[SpotifyWeb] Waiting for authentication... URL: " + authURL)
|
|
||||||
client = <-tempClientChan
|
client = <-tempClientChan
|
||||||
|
|
||||||
// use the client to make calls that require authorization
|
// 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()
|
playerState, err = client.PlayerState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
logger.Log("[SpotifyWeb] Authentication complete.")
|
|
||||||
widget.client = client
|
widget.client = client
|
||||||
widget.playerState = playerState
|
widget.playerState = playerState
|
||||||
widget.Refresh()
|
widget.Refresh()
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package wtf
|
package wtf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/wtfutil/wtf/cfg"
|
"github.com/wtfutil/wtf/cfg"
|
||||||
"github.com/wtfutil/wtf/logger"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MultiSourceWidget is a widget that supports displaying data from multiple sources
|
||||||
type MultiSourceWidget struct {
|
type MultiSourceWidget struct {
|
||||||
moduleConfig *cfg.Common
|
moduleConfig *cfg.Common
|
||||||
singular string
|
singular string
|
||||||
@ -26,8 +25,6 @@ func NewMultiSourceWidget(moduleConfig *cfg.Common, singular, plural string) Mul
|
|||||||
|
|
||||||
widget.loadSources()
|
widget.loadSources()
|
||||||
|
|
||||||
logger.Log(fmt.Sprintf("%+v", widget.Sources))
|
|
||||||
|
|
||||||
return widget
|
return widget
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +39,7 @@ func (widget *MultiSourceWidget) CurrentSource() string {
|
|||||||
return widget.Sources[widget.Idx]
|
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
|
// source it wraps around to the first source
|
||||||
func (widget *MultiSourceWidget) NextSource() {
|
func (widget *MultiSourceWidget) NextSource() {
|
||||||
widget.Idx++
|
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
|
// source, it wraps around to the last source
|
||||||
func (widget *MultiSourceWidget) PrevSource() {
|
func (widget *MultiSourceWidget) PrevSource() {
|
||||||
widget.Idx--
|
widget.Idx--
|
||||||
|
Loading…
x
Reference in New Issue
Block a user