diff --git a/modules/pkg/doc.rst b/modules/pkg/doc.rst index eed58e26..58153783 100644 --- a/modules/pkg/doc.rst +++ b/modules/pkg/doc.rst @@ -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 diff --git a/modules/pkg/paramscreator.go b/modules/pkg/paramscreator.go index 17f1b25d..701a9d33 100644 --- a/modules/pkg/paramscreator.go +++ b/modules/pkg/paramscreator.go @@ -24,7 +24,11 @@ func printHelp(isCmd bool) { %sname - OS package search ex: name linux-image query for installed OS packages matching expression -`, dash) + +%sversion - 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 diff --git a/modules/pkg/pkg.go b/modules/pkg/pkg.go index a8d78029..8315a2bb 100644 --- a/modules/pkg/pkg.go +++ b/modules/pkg/pkg.go @@ -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 {