[minor] support filtering by version in pkg module

Closes #182
This commit is contained in:
Aaron Meihm 2016-05-22 14:07:00 -05:00
Родитель 5a1d965600
Коммит 360f1cbc64
3 изменённых файлов: 34 добавлений и 1 удалений

Просмотреть файл

@ -20,6 +20,10 @@ on the agent system, identifying any packages matching the regular expression
argument to `name`, and returning those packages along with their versions. In
addition to this, the agent will also return what type of package was identified.
It is also possible to optionally filter on the version, so the agent only returns
packages which also match the given version. This can be done by supplying a
regular expression to match against the package version as a `version` option.
Currently the following package managers are supported for query on an agent system.
* **RPM**: RPM based package managers, for Red Hat, CentOS, etc

Просмотреть файл

@ -24,7 +24,11 @@ func printHelp(isCmd bool) {
%sname <regexp> - OS package search
ex: name linux-image
query for installed OS packages matching expression
`, dash)
%sversion <regexp> - Version string search
ex: version ^1\..*
optionally filter returned packages to include version
`, dash, dash)
}
func (r *run) ParamsCreator() (interface{}, error) {
@ -58,6 +62,8 @@ func (r *run) ParamsCreator() (interface{}, error) {
switch checkType {
case "name":
p.PkgMatch.Matches = append(p.PkgMatch.Matches, checkValue)
case "version":
p.VerMatch = checkValue
default:
fmt.Printf("Invalid method!\nTry 'help'\n")
continue
@ -73,6 +79,7 @@ func (r *run) ParamsParser(args []string) (interface{}, error) {
var (
fs flag.FlagSet
pkgMatch flagParam
verMatch string
)
if len(args) < 1 || args[0] == "" || args[0] == "help" {
@ -82,6 +89,7 @@ func (r *run) ParamsParser(args []string) (interface{}, error) {
fs.Init("pkg", flag.ContinueOnError)
fs.Var(&pkgMatch, "name", "see help")
fs.StringVar(&verMatch, "version", "", "see help")
err := fs.Parse(args)
if err != nil {
return nil, err
@ -89,6 +97,9 @@ func (r *run) ParamsParser(args []string) (interface{}, error) {
p := newParameters()
p.PkgMatch.Matches = pkgMatch
if verMatch != "" {
p.VerMatch = verMatch
}
r.Parameters = *p

Просмотреть файл

@ -105,6 +105,17 @@ func (r *run) Run(in io.Reader) (resStr string) {
if !re.MatchString(y.Name) {
continue
}
// If optional version filter was supplied, filter on that
// as well
if r.Parameters.VerMatch != "" {
rev, err := regexp.Compile(r.Parameters.VerMatch)
if err != nil {
panic(err)
}
if !rev.MatchString(y.Version) {
continue
}
}
e.Packages = append(e.Packages, y)
}
}
@ -127,6 +138,12 @@ func (r *run) ValidateParameters() (err error) {
return err
}
}
if r.Parameters.VerMatch != "" {
_, err = regexp.Compile(r.Parameters.VerMatch)
if err != nil {
return err
}
}
return
}
@ -172,6 +189,7 @@ type Statistics struct {
type Parameters struct {
PkgMatch PkgMatch `json:"pkgmatch"` // List of strings to use as regexp package matches.
VerMatch string `json:"vermatch"` // Optionally filter returned packages on version string
}
type PkgMatch struct {