From ed9d3224924948e76bb6214c944fdbf1f3548ccd Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 12 Jun 2015 16:16:54 +0300 Subject: [PATCH] colorization support and redirect stdout and stderr for the command --- cmd/godog/main.go | 14 +++++++++---- colors.go | 20 ++++++++++++++++++ config.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ runner.go | 15 +++++++------- 4 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 colors.go create mode 100644 config.go diff --git a/cmd/godog/main.go b/cmd/godog/main.go index fdf2e39..99e885d 100644 --- a/cmd/godog/main.go +++ b/cmd/godog/main.go @@ -1,15 +1,19 @@ package main import ( - "log" "os" "os/exec" "strings" "github.com/DATA-DOG/godog" + "github.com/shiena/ansicolor" ) func main() { + // will support Ansi colors for windows + stdout := ansicolor.NewAnsiColorWriter(os.Stdout) + stderr := ansicolor.NewAnsiColorWriter(os.Stdout) + builtFile := os.TempDir() + "/godog_build.go" if err := os.Remove(builtFile); err != nil && !os.IsNotExist(err) { panic(err) @@ -30,10 +34,12 @@ func main() { } w.Close() - cmd := strings.TrimSpace("go run " + builtFile + " " + strings.Join(os.Args[1:], " ")) - out, err := exec.Command("sh", "-c", cmd).CombinedOutput() + c := strings.TrimSpace("go run " + builtFile + " " + strings.Join(os.Args[1:], " ")) + cmd := exec.Command("sh", "-c", c) + cmd.Stdout = stdout + cmd.Stderr = stderr + err = cmd.Run() if err != nil { panic(err) } - log.Println("output:", string(out)) } diff --git a/colors.go b/colors.go new file mode 100644 index 0000000..b98d0a9 --- /dev/null +++ b/colors.go @@ -0,0 +1,20 @@ +package godog + +import "fmt" + +type color int + +const ( + black color = iota + 30 + red + green + yellow + blue + magenta + cyan + white +) + +func cl(s string, c color) string { + return fmt.Sprintf("\033[%dm%s\033[0m", c, s) +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..6fec140 --- /dev/null +++ b/config.go @@ -0,0 +1,53 @@ +package godog + +import ( + "flag" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/DATA-DOG/godog/gherkin" +) + +var cfg config + +type config struct { + featuresPath string + formatterName string +} + +func init() { + flag.StringVar(&cfg.featuresPath, "features", "features", "Path to feature files") + flag.StringVar(&cfg.formatterName, "formatter", "pretty", "Formatter name") +} + +func (c config) validate() error { + inf, err := os.Stat(c.featuresPath) + if err != nil { + return err + } + if !inf.IsDir() { + return fmt.Errorf("feature path \"%s\" is not a directory.", c.featuresPath) + } + switch c.formatterName { + case "pretty": + // ok + default: + return fmt.Errorf("Unsupported formatter name: %s", c.formatterName) + } + return nil +} + +func (c config) features() (lst []*gherkin.Feature, err error) { + return lst, filepath.Walk(cfg.featuresPath, func(p string, f os.FileInfo, err error) error { + if err == nil && !f.IsDir() && strings.HasSuffix(p, ".feature") { + ft, err := gherkin.Parse(p) + if err != nil { + return err + } + lst = append(lst, ft) + } + return err + }) +} diff --git a/runner.go b/runner.go index b9911c8..babc2fb 100644 --- a/runner.go +++ b/runner.go @@ -1,15 +1,14 @@ package godog import ( - "fmt" + "flag" "log" ) -func Run() error { - log.Println("running godoc, num registered steps:", len(stepHandlers), "color test:", red("red")) - return nil -} - -func red(s string) string { - return fmt.Sprintf("\033[31m%s\033[0m", s) +func Run() { + if !flag.Parsed() { + flag.Parse() + } + log.Println("running godoc, num registered steps:", len(stepHandlers), "color test:", cl("red", red)) + log.Println("will read features in path:", cl(cfg.featuresPath, yellow)) }