-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpackage.go
207 lines (176 loc) · 4.98 KB
/
package.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2022 Emanuele Rocca
package main
import (
"log"
"os"
"os/exec"
"strings"
)
// A PetsPackage represents a distribution package.
type PetsPackage string
// PackageManager available on the system. APT on Debian-based distros, YUM on
// RedHat and derivatives.
type PackageManager int
const (
APT = iota
YUM
APK
YAY
PACMAN
)
// WhichPackageManager is available on the system
func WhichPackageManager() PackageManager {
var err error
apt := NewCmd([]string{"apt", "--help"})
_, _, err = RunCmd(apt)
if err == nil {
return APT
}
yum := NewCmd([]string{"yum", "--help"})
_, _, err = RunCmd(yum)
if err == nil {
return YUM
}
apk := NewCmd([]string{"apk", "--version"})
_, _, err = RunCmd(apk)
if err == nil {
return APK
}
// Yay has to be first because yay wraps pacman
yay := NewCmd([]string{"yay", "--version"})
if _, _, err = RunCmd(yay); err == nil {
return YAY
}
pacman := NewCmd([]string{"pacman", "--version"})
if _, _, err = RunCmd(pacman); err == nil {
return PACMAN
}
panic("Unknown Package Manager")
}
func (pp PetsPackage) getPkgInfo() string {
var pkgInfo *exec.Cmd
switch WhichPackageManager() {
case APT:
pkgInfo = NewCmd([]string{"apt-cache", "policy", string(pp)})
case YUM:
pkgInfo = NewCmd([]string{"yum", "info", string(pp)})
case APK:
pkgInfo = NewCmd([]string{"apk", "search", "-e", string(pp)})
case PACMAN:
pkgInfo = NewCmd([]string{"pacman", "-Si", string(pp)})
case YAY:
pkgInfo = NewCmd([]string{"yay", "-Si", string(pp)})
}
stdout, _, err := RunCmd(pkgInfo)
if err != nil {
log.Printf("[ERROR] pkgInfoPolicy() command %s failed: %s\n", pkgInfo, err)
return ""
}
return stdout
}
// IsValid returns true if the given PetsPackage is available in the distro.
func (pp PetsPackage) IsValid() bool {
stdout := pp.getPkgInfo()
family := WhichPackageManager()
if family == APT && strings.HasPrefix(stdout, string(pp)) {
// Return true if the output of apt-cache policy begins with pp
log.Printf("[DEBUG] %s is a valid package name\n", pp)
return true
}
if family == YUM {
for _, line := range strings.Split(stdout, "\n") {
line = strings.TrimSpace(line)
pkgName := strings.SplitN(line, ": ", 2)
if len(pkgName) == 2 {
if strings.TrimSpace(pkgName[0]) == "Name" {
return pkgName[1] == string(pp)
}
}
}
}
if family == APK && strings.HasPrefix(stdout, string(pp)) {
// Return true if the output of apk search -e begins with pp
log.Printf("[DEBUG] %s is a valid package name\n", pp)
return true
}
if (family == PACMAN || family == YAY) && !strings.HasPrefix(stdout, "error:") {
// Return true if the output of pacman -Si doesnt begins with error
log.Printf("[DEBUG] %s is a valid package name\n", pp)
return true
}
log.Printf("[ERROR] %s is not an available package\n", pp)
return false
}
// IsInstalled returns true if the given PetsPackage is installed on the
// system.
func (pp PetsPackage) IsInstalled() bool {
family := WhichPackageManager()
if family == APT {
stdout := pp.getPkgInfo()
for _, line := range strings.Split(stdout, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "Installed: ") {
version := strings.SplitN(line, ": ", 2)
return version[1] != "(none)"
}
}
log.Printf("[ERROR] no 'Installed:' line in apt-cache policy %s\n", pp)
return false
}
if family == YUM {
installed := NewCmd([]string{"rpm", "-qa", string(pp)})
stdout, _, err := RunCmd(installed)
if err != nil {
log.Printf("[ERROR] running %s: '%s'", installed, err)
return false
}
return len(stdout) > 0
}
if family == APK {
installed := NewCmd([]string{"apk", "info", "-e", string(pp)})
stdout, _, err := RunCmd(installed)
if err != nil {
log.Printf("[ERROR] running %s: '%s'\n", installed, err)
return false
}
// apk info -e $pkg prints the package name to stdout if the package is
// installed, nothing otherwise
return strings.TrimSpace(stdout) == string(pp)
}
if family == PACMAN || family == YAY {
installed := NewCmd([]string{"pacman", "-Q", string(pp)})
if family == YAY {
installed = NewCmd([]string{"yay", "-Q", string(pp)})
}
// pacman and yay will return 0 if the package is installed 1 if not
if _, _, err := RunCmd(installed); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return exitError.ExitCode() == 0
}
log.Printf("[ERROR] running %s: '%s'", installed, err)
return false
}
return true
}
return false
}
// InstallCommand returns the command needed to install packages on this
// system.
func InstallCommand() *exec.Cmd {
switch WhichPackageManager() {
case APT:
cmd := NewCmd([]string{"apt-get", "-y", "install"})
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "DEBIAN_FRONTEND=noninteractive")
return cmd
case YUM:
return NewCmd([]string{"yum", "-y", "install"})
case APK:
return NewCmd([]string{"apk", "add"})
case PACMAN:
return NewCmd([]string{"pacman", "-S", "--noconfirm"})
case YAY:
return NewCmd([]string{"yay", "-S", "--noconfirm"})
}
return nil
}