Ejemplo n.º 1
0
func TestInstructionFromArgs(t *testing.T) {
	i, err := pie.InstructionFromArgs([]string{"a", "b", "c", "d"})
	if err != nil {
		t.Fatal(err)
	}
	if len(i) != 2 {
		t.Fatalf("was expecting 2 instructions but got %d", len(i))
	}
}
Ejemplo n.º 2
0
func TestInstructionFromArgsPairError(t *testing.T) {
	_, err := pie.InstructionFromArgs([]string{"a"})
	if err == nil {
		t.Fatal("was expecting an error")
	}
}
Ejemplo n.º 3
0
Archivo: pie.go Proyecto: daaku/pie
func Main() error {
	var (
		goMaxProcs = flag.Int("gomaxprocs", runtime.NumCPU(), "gomaxprocs")
		ignoreRe   = flag.String("ignore", "", "file full path ignore regexp")
		filterRe   = flag.String("filter", "", "file full path filter regexp")
		cpuProfile = flag.String("cpuprofile", "", "write cpu profile to this file")
		inFile     = flag.String("input", "", "read instruction pairs from this file")
		indexFile  = flag.String("index", defaultIndexFile(), "default index file location")
		roots      = flag.String("root", defaultRoot(), "comma separated target paths")
	)

	flag.Usage = usage
	flag.Parse()
	args := flag.Args()

	runtime.GOMAXPROCS(*goMaxProcs)

	if *cpuProfile != "" {
		f, err := os.Create(*cpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	// parse replacement instructions
	r := &pie.Run{
		FileFilter: *filterRe,
		FileIgnore: *ignoreRe,
	}
	var err error
	if *inFile != "" {
		f, err := os.Open(*inFile)
		if err != nil {
			return err
		}
		r.Instruction, err = pie.InstructionFromReader(f)
		if err != nil {
			return err
		}
	} else {
		r.Instruction, err = pie.InstructionFromArgs(args)
		if err != nil {
			return err
		}
	}
	if len(r.Instruction) == 0 {
		flag.Usage()
	}

	// make the index
	iw := index.Create(*indexFile)
	for _, arg := range strings.Split(*roots, ",") {
		filepath.Walk(arg, func(path string, info os.FileInfo, err error) error {
			if _, elem := filepath.Split(path); elem != "" {
				// Skip various temporary or "hidden" files or directories.
				if elem[0] == '.' || elem[0] == '#' || elem[0] == '~' || elem[len(elem)-1] == '~' {
					if info.IsDir() {
						return filepath.SkipDir
					}
					return nil
				}
			}
			if err != nil {
				log.Printf("%s: %s", path, err)
				return nil
			}
			if info != nil && info.Mode()&os.ModeType == 0 {
				iw.AddFile(path)
			}
			return nil
		})
	}
	iw.Flush()
	defer os.Remove(*indexFile)
	r.Index = index.Open(*indexFile)

	return r.Run()
}