mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Pull command flags out of main() and into its own file
This commit is contained in:
parent
47a077a472
commit
ebf900e91b
21
help/help.go
21
help/help.go
@ -11,22 +11,6 @@ import (
|
|||||||
"github.com/senorprogrammer/wtf/weather"
|
"github.com/senorprogrammer/wtf/weather"
|
||||||
)
|
)
|
||||||
|
|
||||||
//func DisplayCommandInfo(args []string, version string) {
|
|
||||||
//if len(args) == 0 {
|
|
||||||
//return
|
|
||||||
//}
|
|
||||||
|
|
||||||
//cmd := args[0]
|
|
||||||
|
|
||||||
//switch cmd {
|
|
||||||
//case "help", "--help":
|
|
||||||
//DisplayHelpInfo(args)
|
|
||||||
//case "version", "--version":
|
|
||||||
//DisplayVersionInfo(version)
|
|
||||||
//}
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
func DisplayHelpInfo(moduleName string) {
|
func DisplayHelpInfo(moduleName string) {
|
||||||
if moduleName != "" {
|
if moduleName != "" {
|
||||||
fmt.Printf("%s\n", helpFor(moduleName))
|
fmt.Printf("%s\n", helpFor(moduleName))
|
||||||
@ -37,11 +21,6 @@ func DisplayHelpInfo(moduleName string) {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DisplayVersionInfo(version string) {
|
|
||||||
fmt.Printf("Version: %s\n", version)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func helpFor(moduleName string) string {
|
func helpFor(moduleName string) string {
|
||||||
switch moduleName {
|
switch moduleName {
|
||||||
case "git":
|
case "git":
|
||||||
|
48
wtf.go
48
wtf.go
@ -3,11 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
flags "github.com/jessevdk/go-flags"
|
|
||||||
"github.com/olebedev/config"
|
"github.com/olebedev/config"
|
||||||
"github.com/radovskyb/watcher"
|
"github.com/radovskyb/watcher"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
@ -17,7 +15,6 @@ import (
|
|||||||
"github.com/senorprogrammer/wtf/gcal"
|
"github.com/senorprogrammer/wtf/gcal"
|
||||||
"github.com/senorprogrammer/wtf/git"
|
"github.com/senorprogrammer/wtf/git"
|
||||||
"github.com/senorprogrammer/wtf/github"
|
"github.com/senorprogrammer/wtf/github"
|
||||||
"github.com/senorprogrammer/wtf/help"
|
|
||||||
"github.com/senorprogrammer/wtf/jira"
|
"github.com/senorprogrammer/wtf/jira"
|
||||||
"github.com/senorprogrammer/wtf/newrelic"
|
"github.com/senorprogrammer/wtf/newrelic"
|
||||||
"github.com/senorprogrammer/wtf/opsgenie"
|
"github.com/senorprogrammer/wtf/opsgenie"
|
||||||
@ -107,7 +104,7 @@ func refreshAllWidgets() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func watchForConfigChanges(app *tview.Application, configFlag *string, grid *tview.Grid, pages *tview.Pages) {
|
func watchForConfigChanges(app *tview.Application, configFlag string, grid *tview.Grid, pages *tview.Pages) {
|
||||||
watch := watcher.New()
|
watch := watcher.New()
|
||||||
|
|
||||||
// notify write events.
|
// notify write events.
|
||||||
@ -130,7 +127,7 @@ func watchForConfigChanges(app *tview.Application, configFlag *string, grid *tvi
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Watch config file for changes.
|
// Watch config file for changes.
|
||||||
if err := watch.Add(*configFlag); err != nil {
|
if err := watch.Add(configFlag); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,42 +194,13 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig(configFlag *string) {
|
func loadConfig(configFlag string) {
|
||||||
Config = wtf.LoadConfigFile(*configFlag)
|
Config = wtf.LoadConfigFile(configFlag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
cmdFlags := wtf.NewCommandFlags()
|
||||||
/*
|
cmdFlags.Parse(version)
|
||||||
This allows the user to pass flags in however they prefer. It supports the likes of:
|
|
||||||
|
|
||||||
wtf -help | --help
|
|
||||||
wtf -version | --version
|
|
||||||
*/
|
|
||||||
homeDir, err := wtf.Home()
|
|
||||||
if err != nil {
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
var cmdFlags struct {
|
|
||||||
Config string `short:"c" long:"config" optional:"yes" description:"Path to config file"`
|
|
||||||
Version bool `short:"v" long:"version" description:"Show Version Info"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var parser = flags.NewParser(&cmdFlags, flags.Default)
|
|
||||||
if _, err := parser.Parse(); err != nil {
|
|
||||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(cmdFlags.Config) == 0 {
|
|
||||||
cmdFlags.Config = filepath.Join(homeDir, ".wtf", "config.yml")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmdFlags.Version {
|
|
||||||
help.DisplayVersionInfo(version)
|
|
||||||
}
|
|
||||||
|
|
||||||
/* -------------------- end flag parsing and handling -------------------- */
|
/* -------------------- end flag parsing and handling -------------------- */
|
||||||
|
|
||||||
@ -241,7 +209,7 @@ func main() {
|
|||||||
wtf.CreateConfigDir()
|
wtf.CreateConfigDir()
|
||||||
wtf.WriteConfigFile()
|
wtf.WriteConfigFile()
|
||||||
|
|
||||||
loadConfig(&cmdFlags.Config)
|
loadConfig(cmdFlags.Config)
|
||||||
|
|
||||||
app := tview.NewApplication()
|
app := tview.NewApplication()
|
||||||
pages := tview.NewPages()
|
pages := tview.NewPages()
|
||||||
@ -254,7 +222,7 @@ func main() {
|
|||||||
|
|
||||||
// Loop in a routine to redraw the screen
|
// Loop in a routine to redraw the screen
|
||||||
go redrawApp(app)
|
go redrawApp(app)
|
||||||
go watchForConfigChanges(app, &cmdFlags.Config, grid, pages)
|
go watchForConfigChanges(app, cmdFlags.Config, grid, pages)
|
||||||
|
|
||||||
if err := app.SetRoot(pages, true).Run(); err != nil {
|
if err := app.SetRoot(pages, true).Run(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
45
wtf/command_flags.go
Normal file
45
wtf/command_flags.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package wtf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
flags "github.com/jessevdk/go-flags"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CommandFlags struct {
|
||||||
|
Config string `short:"c" long:"config" optional:"yes" description:"Path to config file"`
|
||||||
|
Version bool `short:"v" long:"version" description:"Show Version Info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCommandFlags() *CommandFlags {
|
||||||
|
cmdFlags := CommandFlags{}
|
||||||
|
return &cmdFlags
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
|
||||||
|
func (cmdFlags *CommandFlags) Parse(version string) {
|
||||||
|
parser := flags.NewParser(cmdFlags, flags.Default)
|
||||||
|
if _, err := parser.Parse(); err != nil {
|
||||||
|
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cmdFlags.Config) == 0 {
|
||||||
|
homeDir, err := Home()
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdFlags.Config = filepath.Join(homeDir, ".wtf", "config.yml")
|
||||||
|
fmt.Printf(">> A: %s\n", cmdFlags.Config)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cmdFlags.Version {
|
||||||
|
fmt.Printf("Version: %s\n", version)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
@ -59,6 +59,8 @@ func CreateFile(fileName string) (string, error) {
|
|||||||
|
|
||||||
// LoadConfigFile loads the config.yml file to configure the app
|
// LoadConfigFile loads the config.yml file to configure the app
|
||||||
func LoadConfigFile(filePath string) *config.Config {
|
func LoadConfigFile(filePath string) *config.Config {
|
||||||
|
fmt.Printf(">> B: %s\n", filePath)
|
||||||
|
|
||||||
absPath, _ := ExpandHomeDir(filePath)
|
absPath, _ := ExpandHomeDir(filePath)
|
||||||
|
|
||||||
cfg, err := config.ParseYamlFile(absPath)
|
cfg, err := config.ParseYamlFile(absPath)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user