Skip to content

Commit

Permalink
Move command line flags into it's own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Cummer committed Jun 17, 2018
1 parent 889aed1 commit e92289e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 60 deletions.
2 changes: 1 addition & 1 deletion _sample_configs/simple_config.yml
@@ -1,6 +1,6 @@
wtf:
colors:
background: gray
#background: gray
border:
focusable: darkslateblue
focused: orange
Expand Down
54 changes: 54 additions & 0 deletions flags/flags.go
@@ -0,0 +1,54 @@
package flags

import (
"fmt"
"os"
"path/filepath"

goFlags "github.com/jessevdk/go-flags"
"github.com/senorprogrammer/wtf/wtf"
)

type Flags struct {
Config string `short:"c" long:"config" optional:"yes" description:"Path to config file"`
Module string `short:"m" long:"module" optional:"yes" description:"Display info about a specific module, i.e.: 'wtf -m=todo'"`
Version bool `short:"v" long:"version" description:"Show Version Info"`
}

func NewFlags() *Flags {
flags := Flags{}
return &flags
}

/* -------------------- Exported Functions -------------------- */

func (flags *Flags) HasConfig() bool {
return len(flags.Config) > 0
}

func (flags *Flags) HasModule() bool {
return len(flags.Module) > 0
}

func (flags *Flags) Parse(version string) {
parser := goFlags.NewParser(flags, goFlags.Default)
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*goFlags.Error); ok && flagsErr.Type == goFlags.ErrHelp {
os.Exit(0)
}
}

if !flags.HasConfig() {
homeDir, err := wtf.Home()
if err != nil {
os.Exit(1)
}

flags.Config = filepath.Join(homeDir, ".wtf", "config.yml")
}

if flags.Version {
fmt.Printf("Version: %s\n", version)
os.Exit(0)
}
}
13 changes: 7 additions & 6 deletions wtf.go
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/senorprogrammer/wtf/cryptoexchanges/bittrex"
"github.com/senorprogrammer/wtf/cryptoexchanges/blockfolio"
"github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive"
"github.com/senorprogrammer/wtf/flags"
"github.com/senorprogrammer/wtf/gcal"
"github.com/senorprogrammer/wtf/git"
"github.com/senorprogrammer/wtf/github"
Expand Down Expand Up @@ -260,17 +261,17 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)

cmdFlags := wtf.NewCommandFlags()
cmdFlags.Parse(version)
flags := flags.NewFlags()
flags.Parse(version)

if cmdFlags.HasModule() {
help.Display(cmdFlags.Module)
if flags.HasModule() {
help.Display(flags.Module)
}

cfg.CreateConfigDir()
cfg.WriteConfigFile()

loadConfig(cmdFlags.Config)
loadConfig(flags.Config)
setTerm()

app := tview.NewApplication()
Expand All @@ -285,7 +286,7 @@ func main() {

// Loop in a routine to redraw the screen
go redrawApp(app)
go watchForConfigChanges(app, cmdFlags.Config, display.Grid, pages)
go watchForConfigChanges(app, flags.Config, display.Grid, pages)

if err := app.SetRoot(pages, true).Run(); err != nil {
fmt.Printf("An error occurred: %v\n", err)
Expand Down
53 changes: 0 additions & 53 deletions wtf/command_flags.go

This file was deleted.

0 comments on commit e92289e

Please sign in to comment.