Esempio n. 1
0
func makeSearchers(
	cfg *config.Config,
	useStaleIndex bool) (map[string]*searcher.Searcher, error) {
	// Ensure we have a dbpath
	if _, err := os.Stat(cfg.DbPath); err != nil {
		if err := os.MkdirAll(cfg.DbPath, os.ModePerm); err != nil {
			return nil, err
		}
	}

	validRepos := map[string]*config.Repo{}
	// Now build and initialize a searcher for each repo.
	// TODO(knorton): These could be done in parallel.
	m := map[string]*searcher.Searcher{}
	var err error
	for name, repo := range cfg.Repos {
		for _, branch := range repo.Branches {
			path := filepath.Join(cfg.DbPath, name)

			var s *searcher.Searcher

			if useStaleIndex {
				s, err = searcher.NewFromExisting(path, repo, branch)
			} else {
				s, err = searcher.New(path, repo, branch)
			}
			if err == nil {
				m[strings.ToLower(name)+"@"+strings.ToLower(branch)] = s
				validRepos[name] = repo
			}

		}
	}
	if err != nil {
		err = errors.New("One or more repos failed to index")
	}

	// Update the config to only include repos we have successfully indexed
	cfg.Repos = validRepos
	return m, err
}