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

Move the ModuleValidator from /wtf to /app

This commit is contained in:
Chris Cummer
2019-07-28 06:30:18 -07:00
parent b790b27000
commit 82c89668a0
3 changed files with 33 additions and 4 deletions

19
app/error_messages.go Normal file
View File

@@ -0,0 +1,19 @@
package app
// This file contains the error messages that get written to the terminal when
// something goes wrong with the configuration process.
//
// As a general rule, if one of these has to be shown the app should then die
// via os.Exit(1)
import (
"fmt"
"github.com/logrusorgru/aurora"
)
/* -------------------- Unexported Functions -------------------- */
func displayError(err error) {
fmt.Printf("%s %s\n\n", aurora.Red("Error:"), err.Error())
}

59
app/module_validator.go Normal file
View File

@@ -0,0 +1,59 @@
package app
import (
"fmt"
"os"
"github.com/logrusorgru/aurora"
"github.com/wtfutil/wtf/wtf"
)
type ModuleValidator struct{}
func NewModuleValidator() *ModuleValidator {
val := &ModuleValidator{}
return val
}
// Validate rolls through all the enabled widgets and looks for configuration errors.
// If it finds any it stringifies them, writes them to the console, and kills the app gracefully
func (val *ModuleValidator) Validate(widgets []wtf.Wtfable) {
var errStr string
hasErrors := false
for _, widget := range widgets {
var widgetErrStr string
for _, val := range widget.CommonSettings().Validations() {
if val.HasError() {
hasErrors = true
widgetErrStr += fmt.Sprintf(" - %s\t%s %v\n", val, aurora.Red("Error:"), val.Error())
}
}
if widgetErrStr != "" {
errStr += fmt.Sprintf(
"%s\n",
fmt.Sprintf(
"%s in %s configuration",
aurora.Red("Errors"),
aurora.Yellow(
fmt.Sprintf(
"%s.position",
widget.Name(),
),
),
),
)
errStr += widgetErrStr + "\n"
}
}
if hasErrors {
fmt.Println()
fmt.Printf(errStr)
os.Exit(1)
}
}

View File

@@ -24,6 +24,7 @@ type WtfApp struct {
focusTracker FocusTracker
isCustomConfig bool
pages *tview.Pages
validator *ModuleValidator
widgets []wtf.Wtfable
}
@@ -40,12 +41,13 @@ func NewWtfApp(app *tview.Application, config *config.Config, configFilePath str
wtfApp.widgets = maker.MakeWidgets(wtfApp.app, wtfApp.pages, wtfApp.config)
wtfApp.display = NewDisplay(wtfApp.widgets, wtfApp.config)
wtfApp.focusTracker = NewFocusTracker(wtfApp.app, wtfApp.widgets, wtfApp.config)
wtfApp.validator = NewModuleValidator()
wtfApp.pages.AddPage("grid", wtfApp.display.Grid, true, true)
wtfApp.app.SetRoot(wtfApp.pages, true)
wtfApp.app.SetInputCapture(wtfApp.keyboardIntercept)
wtf.ValidateWidgets(wtfApp.widgets)
wtfApp.validator.Validate(wtfApp.widgets)
return &wtfApp
}