colorization support and redirect stdout and stderr for the command

Этот коммит содержится в:
gedi 2015-06-12 16:16:54 +03:00
родитель 17a24204da
коммит ed9d322492
4 изменённых файлов: 90 добавлений и 12 удалений

Просмотреть файл

@ -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))
}

20
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)
}

53
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
})
}

Просмотреть файл

@ -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))
}