Beispiel #1
0
func (c *StoreDefsCmd) filters() []store.DefFilter {
	var fs []store.DefFilter
	if c.UnitType != "" && c.Unit != "" {
		fs = append(fs, store.ByUnits(unit.ID2{Type: c.UnitType, Name: c.Unit}))
	}
	if (c.UnitType != "" && c.Unit == "") || (c.UnitType == "" && c.Unit != "") {
		log.Fatal("must specify either both or neither of --unit-type and --unit (to filter by source unit)")
	}
	if c.CommitID != "" {
		fs = append(fs, store.ByCommitIDs(c.CommitID))
	}
	if c.Repo != "" {
		fs = append(fs, store.ByRepos(c.Repo))
	}
	if c.RepoCommitIDs != "" {
		fs = append(fs, makeRepoCommitIDsFilter(c.RepoCommitIDs))
	}
	if c.Path != "" {
		fs = append(fs, store.ByDefPath(c.Path))
	}
	if c.File != "" {
		fs = append(fs, store.ByFiles(path.Clean(c.File)))
	}
	if c.Query != "" {
		fs = append(fs, store.ByDefQuery(c.Query))
	}
	if c.Filter != nil {
		fs = append(fs, c.Filter)
	}
	if c.Limit != 0 || c.Offset != 0 {
		fs = append(fs, store.Limit(c.Limit, c.Offset))
	}
	return fs
}
Beispiel #2
0
func (o *DefListOptions) DefFilters() []store.DefFilter {
	var fs []store.DefFilter
	if o.DefKeys != nil {
		fs = append(fs, store.DefFilterFunc(func(def *graph.Def) bool {
			for _, dk := range o.DefKeys {
				if (def.Repo == "" || def.Repo == dk.Repo) && (def.CommitID == "" || def.CommitID == dk.CommitID) &&
					(def.UnitType == "" || def.UnitType == dk.UnitType) && (def.Unit == "" || def.Unit == dk.Unit) &&
					def.Path == dk.Path {
					return true
				}
			}
			return false
		}))
	}
	if o.Name != "" {
		fs = append(fs, store.DefFilterFunc(func(def *graph.Def) bool {
			return def.Name == o.Name
		}))
	}
	if o.ByteEnd != 0 {
		fs = append(fs, store.DefFilterFunc(func(d *graph.Def) bool {
			return d.DefStart == o.ByteStart && d.DefEnd == o.ByteEnd
		}))
	}
	if o.Query != "" {
		fs = append(fs, store.ByDefQuery(o.Query))
	}
	if len(o.RepoRevs) > 0 {
		vs := make([]store.Version, len(o.RepoRevs))
		for i, repoRev := range o.RepoRevs {
			repo, commitID := ParseRepoAndCommitID(repoRev)
			if len(commitID) != 40 {
				log.Printf("WARNING: In DefListOptions.DefFilters, o.RepoRevs[%d]==%q has no commit ID or a non-absolute commit ID. No defs will match it.", i, repoRev)
			}
			vs[i] = store.Version{Repo: repo, CommitID: commitID}
		}
		fs = append(fs, store.ByRepoCommitIDs(vs...))
	}
	if o.Unit != "" && o.UnitType != "" {
		fs = append(fs, store.ByUnits(unit.ID2{Type: o.UnitType, Name: o.Unit}))
	}
	if (o.UnitType != "" && o.Unit == "") || (o.UnitType == "" && o.Unit != "") {
		log.Println("WARNING: DefListOptions.DefFilter: must specify either both or neither of --type and --name (to filter by source unit)")
	}
	if o.File != "" {
		fs = append(fs, store.ByFiles(path.Clean(o.File)))
	}
	if o.FilePathPrefix != "" {
		fs = append(fs, store.ByFiles(path.Clean(o.FilePathPrefix)))
	}
	if len(o.Kinds) > 0 {
		fs = append(fs, store.DefFilterFunc(func(def *graph.Def) bool {
			for _, kind := range o.Kinds {
				if def.Kind == kind {
					return true
				}
			}
			return false
		}))
	}
	if o.Exported {
		fs = append(fs, store.DefFilterFunc(func(def *graph.Def) bool {
			return def.Exported
		}))
	}
	if o.Nonlocal {
		fs = append(fs, store.DefFilterFunc(func(def *graph.Def) bool {
			return !def.Local
		}))
	}
	if !o.IncludeTest {
		fs = append(fs, store.DefFilterFunc(func(def *graph.Def) bool {
			return !def.Test
		}))
	}
	switch o.Sort {
	case "key":
		fs = append(fs, store.DefsSortByKey{})
	case "name":
		fs = append(fs, store.DefsSortByName{})
	}
	return fs
}