forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.go
69 lines (60 loc) · 1.94 KB
/
packages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"fmt"
"strings"
)
// PackagesInfo defines installed package information
type PackagesInfo struct {
Installed []*PackageInfo `json:"installed"`
}
// PackageInfo defines installed package information
type PackageInfo struct {
Label string `json:"label"`
Version string `json:"version"`
Vendor string `json:"vendor"`
InstallDate string `json:"install_date"`
}
func (info *PackagesInfo) String() string {
var result strings.Builder
result.WriteString("Installed Packages:")
if info != nil && info.Installed != nil {
for _, packageInfo := range info.Installed {
result.WriteString(fmt.Sprintf("\t[%s]: vendor %q, version %q, installed on %q\n", packageInfo.Label, packageInfo.Vendor, packageInfo.Version, packageInfo.InstallDate))
}
return result.String()
}
return "No packages found"
}
// Packages returns a pointer to a PackageInfo collection containing information
// about the host's installed packages
func Packages(opts ...*WithOption) (*PackagesInfo, error) {
mergeOpts := mergeOptions(opts...)
ctx := &context{
chroot: *mergeOpts.Chroot,
}
info := &PackagesInfo{}
if err := ctx.packagesFillInfo(info); err != nil {
return nil, err
}
return info, nil
}
// simple private struct used to encapsulate product information in a top-level
// "product" YAML/JSON map/object key
type packagePrinter struct {
Info *PackagesInfo `json:"product"`
}
// YAMLString returns a string with the product information formatted as YAML
// under a top-level "dmi:" key
func (info *PackagesInfo) YAMLString() string {
return safeYAML(packagePrinter{info})
}
// JSONString returns a string with the product information formatted as JSON
// under a top-level "product:" key
func (info *PackagesInfo) JSONString(indent bool) string {
return safeJSON(packagePrinter{info}, indent)
}