Esempio n. 1
0
File: term.go Progetto: xingskycn/go
// MaskOut masks the output stream forWrites.
//
// WARNING! This uses os pipes, so kernel buffering may cut the tail!
func MaskOut(out *os.File, enc encoding.Encoding) (*os.File, error) {
	pr, pw, err := os.Pipe()
	if err != nil {
		return out, err
	}
	iohlp.SetDirect(pr)
	iohlp.SetDirect(pw)
	// pw -> pr -> out
	go func() {
		defer out.Close()
		io.Copy(text.NewWriter(out, enc), pr)
	}()
	return pw, nil
}
Esempio n. 2
0
func main() {
	flagGen := flag.Bool("gen", false, "generate, not check")
	flagVerbose := flag.Bool("v", false, "verbose log")
	flagDiffThreshold := flag.Int("threshold", 10, "difference threshold percent")

	flag.Parse()
	lvl := log15.LvlInfo
	if *flagVerbose {
		lvl = log15.LvlDebug
	}
	Log.SetHandler(
		log15.LvlFilterHandler(lvl,
			log15.StreamHandler(text.NewWriter(os.Stderr, term.GetTTYEncoding()),
				log15.TerminalFormat())),
	)
	wd, err := os.Getwd()
	if err != nil {
		Log.Crit("get wd", "error", err)
		os.Exit(1)
	}
	parentDir = filepath.Dir(wd)
	cmd := exec.Command("go", "build")
	cmd.Dir = parentDir
	Log.Info("building " + parentDir)
	if err := cmd.Run(); err != nil {
		Log.Crit("compiling", "error", err)
		os.Exit(1)
	}
	names := flag.Args()
	if len(names) == 0 {
		dh, err := os.Open("testdata")
		if err != nil {
			Log.Crit("open testdata", "error", err)
			os.Exit(2)
		}

		names, err = dh.Readdirnames(-1)
		_ = dh.Close()
		if err != nil {
			Log.Crit("cannot list", "dir", dh.Name(), "error", err)
			os.Exit(2)
		}
		for i, fn := range names {
			names[i] = filepath.Join(dh.Name(), fn)
		}
	}
	diffThreshold := *flagDiffThreshold
	if diffThreshold > 100 || diffThreshold < 0 {
		diffThreshold = 10
	}
	errCnt := 0
	for _, fn := range names {
		if !strings.HasSuffix(fn, ".eml.gz") && !strings.HasSuffix(fn, ".eml") {
			continue
		}
		if *flagGen {
			if err := genOne(fn); err != nil {
				errCnt++
				Log.Error("gen", "file", fn, "error", err)
			}
		} else if err := checkOne(fn, diffThreshold); err != nil {
			errCnt++
			Log.Error("run", "file", fn, "error", err)
		}
	}
	if errCnt > 0 {
		os.Exit(3)
	}
}