This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
package-manager.go
115 lines (95 loc) · 2.53 KB
/
package-manager.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package dep
import (
"fmt"
"github.com/pkg/errors"
"github.com/caos/orbos/mntr"
)
type Software struct {
Package string
Version string
}
func (s *Software) String() string {
return fmt.Sprintf("%s=%s", s.Package, s.Version)
}
type Repository struct {
Repository string
KeyURL string
KeyFingerprint string
}
type PackageManager struct {
monitor mntr.Monitor
os OperatingSystem
installed map[string]string
systemd *SystemD
}
func (p *PackageManager) RefreshInstalled() error {
var err error
switch p.os.Packages {
case DebianBased:
err = p.debbasedInstalled()
case REMBased:
err = p.rembasedInstalled()
}
p.monitor.WithFields(map[string]interface{}{
"packages": len(p.installed),
}).Debug("Refreshed installed packages")
return errors.Wrap(err, "refreshing installed packages failed")
}
func (p *PackageManager) Init() error {
p.monitor.Debug("Initializing package manager")
var err error
switch p.os.Packages {
case DebianBased:
err = p.debSpecificUpdatePackages()
case REMBased:
err = p.remSpecificUpdatePackages()
}
if err != nil {
return errors.Wrapf(err, "updating packages failed", p.os.Packages)
}
p.monitor.Debug("Package manager initialized")
return nil
}
func NewPackageManager(monitor mntr.Monitor, os OperatingSystem, systemd *SystemD) *PackageManager {
return &PackageManager{monitor, os, nil, systemd}
}
func (p *PackageManager) CurrentVersions(possiblePackages ...string) ([]*Software, error) {
software := make([]*Software, 0)
for _, pkg := range possiblePackages {
if version, ok := p.installed[pkg]; ok {
pkg := &Software{
Package: pkg,
Version: version,
}
software = append(software, pkg)
p.monitor.WithFields(map[string]interface{}{
"package": pkg.Package,
"version": pkg.Version,
}).Debug("Found filtered installed package")
}
}
return software, nil
}
func (p *PackageManager) Install(installVersion *Software, more ...*Software) error {
switch p.os.Packages {
case DebianBased:
return p.debbasedInstall(installVersion, more...)
case REMBased:
return p.rembasedInstall(installVersion, more...)
}
return errors.Errorf("Package manager %s is not implemented", p.os.Packages)
}
func (p *PackageManager) Add(repo *Repository) {
var err error
switch p.os.Packages {
case DebianBased:
err = p.debbasedAdd(repo)
case REMBased:
err = p.rembasedAdd(repo)
default:
err = errors.Errorf("Package manager %s is not implemented", p.os.Packages)
}
if err != nil {
p.monitor.Info(errors.Wrap(err, "Adding repo failed").Error())
}
}