diff --git a/src/scribe/package_test.go b/src/scribe/package_test.go new file mode 100644 index 0000000..f6d1294 --- /dev/null +++ b/src/scribe/package_test.go @@ -0,0 +1,26 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Contributor: +// - Aaron Meihm ameihm@mozilla.com + +package scribe_test + +import ( + "fmt" + "scribe" + "testing" +) + +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) != 5 { + t.FailNow() + } +} diff --git a/src/scribe/pkgmgr.go b/src/scribe/pkgmgr.go index 549ad29..cbd605b 100644 --- a/src/scribe/pkgmgr.go +++ b/src/scribe/pkgmgr.go @@ -25,6 +25,27 @@ type pkgmgrInfo struct { pkgtype string } +// Package information from the system as returned by QueryPackages(). +type PackageInfo struct { + Name string `json:"name"` // Package name. + Version string `json:"version"` // Package version. + Type string `json:"type"` // Package type. +} + +// Query packages on the system, returning a slice of all identified packages +// in PackageInfo form. +func QueryPackages() []PackageInfo { + ret := make([]PackageInfo, 0) + for _, x := range getAllPackages().results { + np := PackageInfo{} + np.Name = x.name + np.Version = x.version + np.Type = x.pkgtype + ret = append(ret, np) + } + return ret +} + func getPackage(name string) (ret pkgmgrResult) { ret.results = make([]pkgmgrInfo, 0) if !pkgmgrInitialized { @@ -42,6 +63,18 @@ func getPackage(name string) (ret pkgmgrResult) { return } +func getAllPackages() pkgmgrResult { + ret := pkgmgrResult{} + ret.results = make([]pkgmgrInfo, 0) + if !pkgmgrInitialized { + pkgmgrInit() + } + for _, x := range pkgmgrCache { + ret.results = append(ret.results, x) + } + return ret +} + func pkgmgrInit() { debugPrint("pkgmgrInit(): initializing package manager...\n") pkgmgrCache = make([]pkgmgrInfo, 0)