From ebf900e91b2de7b0f47cb2b45aa0cd8bce8c0a1d Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 29 May 2018 17:38:56 -0700 Subject: [PATCH] Pull command flags out of main() and into its own file --- help/help.go | 21 ------------------- wtf.go | 48 ++++++++------------------------------------ wtf/command_flags.go | 45 +++++++++++++++++++++++++++++++++++++++++ wtf/config_files.go | 2 ++ 4 files changed, 55 insertions(+), 61 deletions(-) create mode 100644 wtf/command_flags.go diff --git a/help/help.go b/help/help.go index e129b5de..dec44109 100644 --- a/help/help.go +++ b/help/help.go @@ -11,22 +11,6 @@ import ( "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) { if moduleName != "" { fmt.Printf("%s\n", helpFor(moduleName)) @@ -37,11 +21,6 @@ func DisplayHelpInfo(moduleName string) { os.Exit(0) } -func DisplayVersionInfo(version string) { - fmt.Printf("Version: %s\n", version) - os.Exit(0) -} - func helpFor(moduleName string) string { switch moduleName { case "git": diff --git a/wtf.go b/wtf.go index c06c84b3..ab649fc7 100644 --- a/wtf.go +++ b/wtf.go @@ -3,11 +3,9 @@ package main import ( "log" "os" - "path/filepath" "time" "github.com/gdamore/tcell" - flags "github.com/jessevdk/go-flags" "github.com/olebedev/config" "github.com/radovskyb/watcher" "github.com/rivo/tview" @@ -17,7 +15,6 @@ import ( "github.com/senorprogrammer/wtf/gcal" "github.com/senorprogrammer/wtf/git" "github.com/senorprogrammer/wtf/github" - "github.com/senorprogrammer/wtf/help" "github.com/senorprogrammer/wtf/jira" "github.com/senorprogrammer/wtf/newrelic" "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() // notify write events. @@ -130,7 +127,7 @@ func watchForConfigChanges(app *tview.Application, configFlag *string, grid *tvi }() // Watch config file for changes. - if err := watch.Add(*configFlag); err != nil { + if err := watch.Add(configFlag); err != nil { log.Fatalln(err) } @@ -197,42 +194,13 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) { } } -func loadConfig(configFlag *string) { - Config = wtf.LoadConfigFile(*configFlag) +func loadConfig(configFlag string) { + Config = wtf.LoadConfigFile(configFlag) } func main() { - - /* - 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) - } + cmdFlags := wtf.NewCommandFlags() + cmdFlags.Parse(version) /* -------------------- end flag parsing and handling -------------------- */ @@ -241,7 +209,7 @@ func main() { wtf.CreateConfigDir() wtf.WriteConfigFile() - loadConfig(&cmdFlags.Config) + loadConfig(cmdFlags.Config) app := tview.NewApplication() pages := tview.NewPages() @@ -254,7 +222,7 @@ func main() { // Loop in a routine to redraw the screen 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 { os.Exit(1) diff --git a/wtf/command_flags.go b/wtf/command_flags.go new file mode 100644 index 00000000..9163634e --- /dev/null +++ b/wtf/command_flags.go @@ -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) + } +} diff --git a/wtf/config_files.go b/wtf/config_files.go index ff57654d..1dbf86e5 100644 --- a/wtf/config_files.go +++ b/wtf/config_files.go @@ -59,6 +59,8 @@ func CreateFile(fileName string) (string, error) { // LoadConfigFile loads the config.yml file to configure the app func LoadConfigFile(filePath string) *config.Config { + fmt.Printf(">> B: %s\n", filePath) + absPath, _ := ExpandHomeDir(filePath) cfg, err := config.ParseYamlFile(absPath)