func NewSearchIndex(name, rootDir string, exclusions []*regexp.Regexp) *SearchIndex { files := make([]fuzzy.MatchStruct, 0, 1000000) dir, err := os.Lstat(rootDir) if err != nil { log.Print("Error: unable to open root path: ", err.Error()) } rootDir = strings.TrimSuffix(rootDir, "/") paths := strings.Split(rootDir, "/") rootDir = strings.Join(paths[0:len(paths)-1], "/") indexDir, err := filepath.Abs("./.indices/") codeindexPath := path.Join(indexDir, fmt.Sprintf("%s.ix", name)) if _, err := os.Stat(indexDir); err != nil { if os.IsNotExist(err) { log.Println("Index directory doesn't exist! Creating...") if err := os.MkdirAll(indexDir, 0777); err != nil { log.Println("Error encountered creating ", indexDir) log.Fatal(err) } } else { log.Println("Error checking if index directory exists!") log.Fatal(err) } } log.Println("Creating index file: ", codeindexPath) if _, err := os.Create(codeindexPath); err != nil { log.Println("Error creating index file!") log.Fatal(err) } codeindex := index.Create(codeindexPath) codeindex.AddPaths([]string{rootDir}) files = recursiveSearch(files, codeindex, dir, "", rootDir, exclusions) codeindex.Flush() log.Print("Total Filecount: ", len(files)) return &SearchIndex{fuzzy.NewMatcher(files), *index.Open(codeindexPath), exclusions} }
func TestRun(t *testing.T) { t.Parallel() for _, test := range cases { tmp, err := test.MakeTempCopy() if err != nil { t.Fatalf("faled to make temp copy for %s: %s", test.Name, err) } ixFile := filepath.Join(tmp, ".csearchindex") ixw := index.Create(ixFile) ixw.AddPaths([]string{tmp}) filepath.Walk(tmp, 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 { t.Fatal(err) } if info != nil && info.Mode()&os.ModeType == 0 { ixw.AddFile(path) } return nil }) ixw.Flush() run := &pie.Run{ Index: index.Open(ixFile), Instruction: test.Instruction, FileIgnore: test.FileIgnore, FileFilter: test.FileFilter, NumWorkers: test.NumWorkers, } err = run.Run() if err != nil { t.Fatalf("run for %s failed: %s", test.Name, err) } same, err := test.Compare(tmp) if err != nil { t.Fatalf("compare for %s failed: %s", test.Name, err) } if !same { t.Fatalf("did not get expected result for %s", test.Name) } if *removeTemp { os.RemoveAll(tmp) } } }
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() }