generic package query functionality and test

This commit is contained in:
Aaron Meihm 2015-07-17 12:16:38 -05:00
Родитель acfca2a23a
Коммит 18a620ea35
2 изменённых файлов: 59 добавлений и 0 удалений

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

@ -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()
}
}

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

@ -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)