diff --git a/config/config.go b/config/config.go index 0d410b9..a375198 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "github.com/jessevdk/go-flags" "github.com/sqshq/sampler/console" "gopkg.in/yaml.v2" "io/ioutil" @@ -17,25 +18,20 @@ type Config struct { AsciiBoxes []AsciiBoxConfig `yaml:"asciiboxes,omitempty"` } -type Flags struct { - ConfigFileName string - Variables map[string]string -} +func Load() (Config, Options) { -func Load() (Config, Flags) { + var opt Options + _, err := flags.Parse(&opt) - if len(os.Args) < 2 { - println("Please specify config file location. See www.github.com/sqshq/sampler for the reference") + if err != nil { os.Exit(0) } - cfg := readFile(os.Args[1]) + cfg := readFile(opt.ConfigFile) cfg.validate() cfg.setDefaults() - flg := Flags{ConfigFileName: os.Args[1]} - - return *cfg, flg + return *cfg, opt } func Update(settings []ComponentSettings) { diff --git a/config/options.go b/config/options.go new file mode 100644 index 0000000..9110368 --- /dev/null +++ b/config/options.go @@ -0,0 +1,7 @@ +package config + +type Options struct { + ConfigFile string `short:"c" long:"config" required:"true" description:"path to YAML config file"` + Variables []string `short:"v" long:"variable" required:"false" description:"variable for script ${var-name} placeholder" long-description:"one or more variables can be specified as flags, in order to replace repeated patterns in the scripts, which can be replaced with {$variable-name} placeholder" ` + Examples []string `short:"e" long:"example" required:"false" choice:"runchart" choice:"barchart" choice:"asciibox" choice:"textbox" choice:"gauge" choice:"sparkline" description:"add an example component to the specified config file" long-description:"one or more example component types can be added to the specified config file, in order to jump-start the configuration"` +} diff --git a/go.mod b/go.mod index 678533e..bdb6652 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,12 @@ module github.com/sqshq/sampler require ( - github.com/davecgh/go-spew v1.1.1 // indirect github.com/gizak/termui/v3 v3.0.0 github.com/hajimehoshi/go-mp3 v0.1.1 github.com/hajimehoshi/oto v0.1.1 - github.com/maruel/panicparse v1.1.1 // indirect + github.com/jessevdk/go-flags v1.4.0 github.com/mattn/go-runewidth v0.0.4 github.com/mbndr/figlet4go v0.0.0-20190224160619-d6cef5b186ea github.com/mitchellh/go-wordwrap v1.0.0 // indirect - github.com/stretchr/testify v1.3.0 // indirect gopkg.in/yaml.v2 v2.2.2 ) diff --git a/go.sum b/go.sum index 8970c59..3e0d9cb 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,5 @@ github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd h1:XtfPmj9tQRilnrEmI1HjQhxXWRhEM+m8CACtaMJE/kM= github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd/go.mod h1:vjcQJUZJYD3MeVGhtZXSMnCHfUNZxsyYzJt90eCYxK4= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gizak/termui v2.3.0+incompatible h1:S8wJoNumYfc/rR5UezUM4HsPEo3RJh0LKdiuDWQpjqw= -github.com/gizak/termui v2.3.0+incompatible/go.mod h1:PkJoWUt/zacQKysNfQtcw1RW+eK2SxkieVBtl+4ovLA= github.com/gizak/termui/v3 v3.0.0 h1:NYTUG6ig/sJK05O5FyhWemwlVPO8ilNpvS/PgRtrKAE= github.com/gizak/termui/v3 v3.0.0/go.mod h1:uinu2dMdtMI+FTIdEFUJQT5y+KShnhQRshvPblXq3lY= github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f h1:FDM3EtwZLyhW48YRiyqjivNlNZjAObv4xt4NnJaU+NQ= @@ -15,8 +10,8 @@ github.com/hajimehoshi/go-mp3 v0.1.1 h1:Y33fAdTma70fkrxnc9u50Uq0lV6eZ+bkAlssdMmC github.com/hajimehoshi/go-mp3 v0.1.1/go.mod h1:4i+c5pDNKDrxl1iu9iG90/+fhP37lio6gNhjCx9WBJw= github.com/hajimehoshi/oto v0.1.1 h1:EG+WxxeAfde1mI0adhLYvGbKgDCxm7bCTd6g+JIA6vI= github.com/hajimehoshi/oto v0.1.1/go.mod h1:hUiLWeBQnbDu4pZsAhOnGqMI1ZGibS6e2qhQdfpwz04= -github.com/maruel/panicparse v1.1.1 h1:k62YPcEoLncEEpjMt92GtG5ugb8WL/510Ys3/h5IkRc= -github.com/maruel/panicparse v1.1.1/go.mod h1:nty42YY5QByNC5MM7q/nj938VbgPU7avs45z6NClpxI= +github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= @@ -27,11 +22,6 @@ github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9 github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840= github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go index 077ccd2..0b1231d 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ import ( type Starter struct { lout *layout.Layout player *asset.AudioPlayer - flags config.Flags + opt config.Options } func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, conponentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) { @@ -30,7 +30,7 @@ func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, conponent func main() { - cfg, flg := config.Load() + cfg, opt := config.Load() console.Init() defer console.Close() @@ -39,9 +39,9 @@ func main() { defer player.Close() width, height := ui.TerminalDimensions() - lout := layout.NewLayout(width, height, component.NewStatusLine(flg.ConfigFileName), component.NewMenu()) + lout := layout.NewLayout(width, height, component.NewStatusLine(opt.ConfigFile), component.NewMenu()) - starter := &Starter{lout, player, flg} + starter := &Starter{lout, player, opt} for _, c := range cfg.RunCharts { cpt := runchart.NewRunChart(c)