Exemplo n.º 1
0
func TestPackageQuery(t *testing.T) {
	scribe.Bootstrap()
	scribe.TestHooks(true)
	pinfo := scribe.QueryPackages()
	for _, x := range pinfo {
		fmt.Println(x.Name, x.Version, x.Type)
	}
	if len(pinfo) != 7 {
		t.FailNow()
	}
}
Exemplo n.º 2
0
func (r *run) Run(in io.Reader) (resStr string) {
	defer func() {
		if e := recover(); e != nil {
			// return error in json
			r.Results.Errors = append(r.Results.Errors, fmt.Sprintf("%v", e))
			r.Results.Success = false
			endCounters()
			r.Results.Statistics = stats
			err, _ := json.Marshal(r.Results)
			resStr = string(err)
			return
		}
	}()

	// Restrict go runtime processor utilization here, this might be moved
	// into a more generic agent module function at some point.
	runtime.GOMAXPROCS(1)

	startCounters()

	// Read module parameters from stdin
	err := modules.ReadInputParameters(in, &r.Parameters)
	if err != nil {
		panic(err)
	}

	err = r.ValidateParameters()
	if err != nil {
		panic(err)
	}

	e := &elements{}
	e.Packages = make([]scribelib.PackageInfo, 0)
	pkglist := scribelib.QueryPackages()
	for _, x := range r.Parameters.PkgMatch.Matches {
		re, err := regexp.Compile(x)
		if err != nil {
			panic(err)
		}
		for _, y := range pkglist {
			if !re.MatchString(y.Name) {
				continue
			}
			// If optional version filter was supplied, filter on that
			// as well
			if r.Parameters.VerMatch != "" {
				vs := r.Parameters.VerMatch
				invver := false
				if vs[0] == '!' && len(vs) > 1 {
					vs = vs[1:]
					invver = true
				}
				rev, err := regexp.Compile(vs)
				if err != nil {
					panic(err)
				}
				if !invver {
					if !rev.MatchString(y.Version) {
						continue
					}
				} else {
					if rev.MatchString(y.Version) {
						continue
					}
				}
			}
			e.Packages = append(e.Packages, y)
		}
	}
	buf, err := buildResults(*e, &r.Results)
	if err != nil {
		panic(err)
	}
	resStr = string(buf)
	return
}