Add options to package managers

This commit is contained in:
Elara 2022-10-01 01:30:13 -07:00
parent 1f39f5edf1
commit c09574e659
10 changed files with 293 additions and 96 deletions

View File

@ -374,7 +374,13 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
}
if removeBuildDeps {
err = mgr.Remove(vars.BuildDepends...)
err = mgr.Remove(
&manager.Opts{
AsRoot: true,
NoConfirm: true,
},
vars.BuildDepends...,
)
if err != nil {
return nil, nil, err
}

View File

@ -50,7 +50,7 @@ func installPkgs(ctx context.Context, pkgs []string, mgr manager.Manager) {
scripts, notFound := findPkgs(pkgs)
if len(notFound) > 0 {
err = mgr.Install(notFound...)
err = mgr.Install(nil, notFound...)
if err != nil {
log.Fatal("Error installing native packages").Err(err).Send()
}
@ -66,7 +66,7 @@ func installScripts(ctx context.Context, mgr manager.Manager, scripts []string)
log.Fatal("Error building package").Err(err).Send()
}
err = mgr.InstallLocal(builtPkgs...)
err = mgr.InstallLocal(nil, builtPkgs...)
if err != nil {
log.Fatal("Error installing package").Err(err).Send()
}
@ -84,7 +84,7 @@ func removeCmd(c *cli.Context) error {
log.Fatal("Unable to detect supported package manager on system").Send()
}
err := mgr.Remove(c.Args().Slice()...)
err := mgr.Remove(nil, c.Args().Slice()...)
if err != nil {
log.Fatal("Error removing packages").Err(err).Send()
}

View File

@ -47,8 +47,9 @@ func (a *APK) SetRootCmd(s string) {
a.rootCmd = s
}
func (a *APK) Sync() error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "update")
func (a *APK) Sync(opts *Opts) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apk", "update")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -57,8 +58,9 @@ func (a *APK) Sync() error {
return nil
}
func (a *APK) Install(pkgs ...string) error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "add")
func (a *APK) Install(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apk", "add")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -68,8 +70,9 @@ func (a *APK) Install(pkgs ...string) error {
return nil
}
func (a *APK) InstallLocal(pkgs ...string) error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "add", "--allow-untrusted")
func (a *APK) InstallLocal(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apk", "add", "--allow-untrusted")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -79,8 +82,9 @@ func (a *APK) InstallLocal(pkgs ...string) error {
return nil
}
func (a *APK) Remove(pkgs ...string) error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "del")
func (a *APK) Remove(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apt", "del")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -90,8 +94,9 @@ func (a *APK) Remove(pkgs ...string) error {
return nil
}
func (a *APK) Upgrade(pkgs ...string) error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "upgrade")
func (a *APK) Upgrade(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apk", "upgrade")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -101,13 +106,21 @@ func (a *APK) Upgrade(pkgs ...string) error {
return nil
}
func (a *APK) UpgradeAll() error {
return a.Upgrade()
func (a *APK) UpgradeAll(opts *Opts) error {
opts = ensureOpts(opts)
return a.Upgrade(opts)
}
func (a *APK) ListInstalled() (map[string]string, error) {
func (a *APK) ListInstalled(opts *Opts) (map[string]string, error) {
opts = ensureOpts(opts)
out := map[string]string{}
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "list", "-I")
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(a.rootCmd), "apk", "list", "-I")
} else {
cmd = exec.Command("apk", "list", "-I")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -141,3 +154,19 @@ func (a *APK) ListInstalled() (map[string]string, error) {
return out, nil
}
func (a *APK) getCmd(opts *Opts, mgrCmd string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(a.rootCmd), mgrCmd)
cmd.Args = append(cmd.Args, args...)
} else {
cmd = exec.Command(mgrCmd, args...)
}
if !opts.NoConfirm {
cmd.Args = append(cmd.Args, "-i")
}
return cmd
}

View File

@ -47,8 +47,9 @@ func (a *APT) SetRootCmd(s string) {
a.rootCmd = s
}
func (a *APT) Sync() error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "update", "-y")
func (a *APT) Sync(opts *Opts) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apt", "update")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -57,8 +58,9 @@ func (a *APT) Sync() error {
return nil
}
func (a *APT) Install(pkgs ...string) error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "install", "-y")
func (a *APT) Install(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apt", "install")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -68,12 +70,14 @@ func (a *APT) Install(pkgs ...string) error {
return nil
}
func (a *APT) InstallLocal(pkgs ...string) error {
return a.Install(pkgs...)
func (a *APT) InstallLocal(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
return a.Install(opts, pkgs...)
}
func (a *APT) Remove(pkgs ...string) error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "remove", "-y")
func (a *APT) Remove(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apt", "remove")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -83,12 +87,14 @@ func (a *APT) Remove(pkgs ...string) error {
return nil
}
func (a *APT) Upgrade(pkgs ...string) error {
return a.Install(pkgs...)
func (a *APT) Upgrade(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
return a.Install(opts, pkgs...)
}
func (a *APT) UpgradeAll() error {
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "upgrade", "-y")
func (a *APT) UpgradeAll(opts *Opts) error {
opts = ensureOpts(opts)
cmd := a.getCmd(opts, "apt", "upgrade")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -97,9 +103,16 @@ func (a *APT) UpgradeAll() error {
return nil
}
func (a *APT) ListInstalled() (map[string]string, error) {
func (a *APT) ListInstalled(opts *Opts) (map[string]string, error) {
opts = ensureOpts(opts)
out := map[string]string{}
cmd := exec.Command(getRootCmd(a.rootCmd), "dpkg-query", "-f", "${Package}\u200b${Version}\\n", "-W")
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(a.rootCmd), "dpkg-query", "-f", "${Package}\u200b${Version}\\n", "-W")
} else {
cmd = exec.Command("dpkg-query", "-f", "${Package}\u200b${Version}\\n", "-W")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -127,3 +140,19 @@ func (a *APT) ListInstalled() (map[string]string, error) {
return out, nil
}
func (a *APT) getCmd(opts *Opts, mgrCmd string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(a.rootCmd), mgrCmd)
cmd.Args = append(cmd.Args, args...)
} else {
cmd = exec.Command(mgrCmd, args...)
}
if opts.NoConfirm {
cmd.Args = append(cmd.Args, "-y")
}
return cmd
}

View File

@ -47,8 +47,9 @@ func (d *DNF) SetRootCmd(s string) {
d.rootCmd = s
}
func (d *DNF) Sync() error {
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "upgrade", "--assumeno")
func (d *DNF) Sync(opts *Opts) error {
opts = ensureOpts(opts)
cmd := d.getCmd(opts, "dnf", "upgrade")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -57,8 +58,9 @@ func (d *DNF) Sync() error {
return nil
}
func (d *DNF) Install(pkgs ...string) error {
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "install", "-y")
func (d *DNF) Install(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := d.getCmd(opts, "dnf", "install")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -68,12 +70,14 @@ func (d *DNF) Install(pkgs ...string) error {
return nil
}
func (d *DNF) InstallLocal(pkgs ...string) error {
return d.Install(pkgs...)
func (d *DNF) InstallLocal(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
return d.Install(opts, pkgs...)
}
func (d *DNF) Remove(pkgs ...string) error {
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "remove", "-y")
func (d *DNF) Remove(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := d.getCmd(opts, "dnf", "remove")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -83,8 +87,9 @@ func (d *DNF) Remove(pkgs ...string) error {
return nil
}
func (d *DNF) Upgrade(pkgs ...string) error {
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "upgrade", "-y")
func (d *DNF) Upgrade(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := d.getCmd(opts, "dnf", "upgrade")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -94,8 +99,9 @@ func (d *DNF) Upgrade(pkgs ...string) error {
return nil
}
func (d *DNF) UpgradeAll() error {
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "upgrade", "-y")
func (d *DNF) UpgradeAll(opts *Opts) error {
opts = ensureOpts(opts)
cmd := d.getCmd(opts, "dnf", "upgrade")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -104,9 +110,16 @@ func (d *DNF) UpgradeAll() error {
return nil
}
func (d *DNF) ListInstalled() (map[string]string, error) {
func (d *DNF) ListInstalled(opts *Opts) (map[string]string, error) {
opts = ensureOpts(opts)
out := map[string]string{}
cmd := exec.Command(getRootCmd(d.rootCmd), "rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(d.rootCmd), "rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
} else {
cmd = exec.Command("rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -134,3 +147,19 @@ func (d *DNF) ListInstalled() (map[string]string, error) {
return out, nil
}
func (d *DNF) getCmd(opts *Opts, mgrCmd string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(d.rootCmd), mgrCmd)
cmd.Args = append(cmd.Args, args...)
} else {
cmd = exec.Command(mgrCmd, args...)
}
if opts.NoConfirm {
cmd.Args = append(cmd.Args, "-y")
}
return cmd
}

View File

@ -23,6 +23,16 @@ import (
"os/exec"
)
type Opts struct {
AsRoot bool
NoConfirm bool
}
var DefaultOpts = &Opts{
AsRoot: true,
NoConfirm: false,
}
// DefaultRootCmd is the command used for privilege elevation by default
var DefaultRootCmd = "sudo"
@ -52,19 +62,19 @@ type Manager interface {
// Sets the command used to elevate privileges. Defaults to DefaultRootCmd.
SetRootCmd(string)
// Sync fetches repositories without installing anything
Sync() error
Sync(*Opts) error
// Install installs packages
Install(...string) error
Install(*Opts, ...string) error
// Remove uninstalls packages
Remove(...string) error
Remove(*Opts, ...string) error
// Upgrade upgrades packages
Upgrade(...string) error
Upgrade(*Opts, ...string) error
// InstallLocal installs packages from local files rather than repos
InstallLocal(...string) error
InstallLocal(*Opts, ...string) error
// UpgradeAll upgrades all packages
UpgradeAll() error
UpgradeAll(*Opts) error
// ListInstalled returns all installed packages mapped to their versions
ListInstalled() (map[string]string, error)
ListInstalled(*Opts) (map[string]string, error)
}
// Detect returns the package manager detected on the system
@ -101,3 +111,10 @@ func setCmdEnv(cmd *exec.Cmd) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
func ensureOpts(opts *Opts) *Opts {
if opts == nil {
return DefaultOpts
}
return opts
}

View File

@ -47,8 +47,9 @@ func (p *Pacman) SetRootCmd(s string) {
p.rootCmd = s
}
func (p *Pacman) Sync() error {
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-Sy")
func (p *Pacman) Sync(opts *Opts) error {
opts = ensureOpts(opts)
cmd := p.getCmd(opts, "pacman", "-Sy")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -57,8 +58,9 @@ func (p *Pacman) Sync() error {
return nil
}
func (p *Pacman) Install(pkgs ...string) error {
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-S")
func (p *Pacman) Install(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := p.getCmd(opts, "pacman", "-S")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -68,8 +70,9 @@ func (p *Pacman) Install(pkgs ...string) error {
return nil
}
func (p *Pacman) InstallLocal(pkgs ...string) error {
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-U")
func (p *Pacman) InstallLocal(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := p.getCmd(opts, "pacman", "-U")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -79,8 +82,9 @@ func (p *Pacman) InstallLocal(pkgs ...string) error {
return nil
}
func (p *Pacman) Remove(pkgs ...string) error {
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-R")
func (p *Pacman) Remove(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := p.getCmd(opts, "pacman", "-R")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -90,12 +94,14 @@ func (p *Pacman) Remove(pkgs ...string) error {
return nil
}
func (p *Pacman) Upgrade(pkgs ...string) error {
return p.Install(pkgs...)
func (p *Pacman) Upgrade(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
return p.Install(opts, pkgs...)
}
func (p *Pacman) UpgradeAll() error {
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-Su")
func (p *Pacman) UpgradeAll(opts *Opts) error {
opts = ensureOpts(opts)
cmd := p.getCmd(opts, "pacman", "-Su")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -104,9 +110,16 @@ func (p *Pacman) UpgradeAll() error {
return nil
}
func (p *Pacman) ListInstalled() (map[string]string, error) {
func (p *Pacman) ListInstalled(opts *Opts) (map[string]string, error) {
opts = ensureOpts(opts)
out := map[string]string{}
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "-Q")
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(p.rootCmd), "pacman", "-Q")
} else {
cmd = exec.Command("pacman", "-Q")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -134,3 +147,19 @@ func (p *Pacman) ListInstalled() (map[string]string, error) {
return out, nil
}
func (p *Pacman) getCmd(opts *Opts, mgrCmd string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(p.rootCmd), mgrCmd)
cmd.Args = append(cmd.Args, args...)
} else {
cmd = exec.Command(mgrCmd, args...)
}
if opts.NoConfirm {
cmd.Args = append(cmd.Args, "--noconfirm")
}
return cmd
}

View File

@ -47,8 +47,9 @@ func (y *YUM) SetRootCmd(s string) {
y.rootCmd = s
}
func (y *YUM) Sync() error {
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "upgrade", "--assumeno")
func (y *YUM) Sync(opts *Opts) error {
opts = ensureOpts(opts)
cmd := y.getCmd(opts, "yum", "upgrade")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -57,8 +58,9 @@ func (y *YUM) Sync() error {
return nil
}
func (y *YUM) Install(pkgs ...string) error {
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "install", "-y")
func (y *YUM) Install(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := y.getCmd(opts, "yum", "install")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -68,12 +70,14 @@ func (y *YUM) Install(pkgs ...string) error {
return nil
}
func (y *YUM) InstallLocal(pkgs ...string) error {
return y.Install(pkgs...)
func (y *YUM) InstallLocal(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
return y.Install(opts, pkgs...)
}
func (y *YUM) Remove(pkgs ...string) error {
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "remove", "-y")
func (y *YUM) Remove(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := y.getCmd(opts, "yum", "remove")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -83,8 +87,9 @@ func (y *YUM) Remove(pkgs ...string) error {
return nil
}
func (y *YUM) Upgrade(pkgs ...string) error {
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "upgrade", "-y")
func (y *YUM) Upgrade(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := y.getCmd(opts, "yum", "upgrade")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -94,8 +99,9 @@ func (y *YUM) Upgrade(pkgs ...string) error {
return nil
}
func (y *YUM) UpgradeAll() error {
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "upgrade", "-y")
func (y *YUM) UpgradeAll(opts *Opts) error {
opts = ensureOpts(opts)
cmd := y.getCmd(opts, "yum", "upgrade")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -104,9 +110,16 @@ func (y *YUM) UpgradeAll() error {
return nil
}
func (y *YUM) ListInstalled() (map[string]string, error) {
func (y *YUM) ListInstalled(opts *Opts) (map[string]string, error) {
opts = ensureOpts(opts)
out := map[string]string{}
cmd := exec.Command(getRootCmd(y.rootCmd), "rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(y.rootCmd), "rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
} else {
cmd = exec.Command("rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -134,3 +147,19 @@ func (y *YUM) ListInstalled() (map[string]string, error) {
return out, nil
}
func (y *YUM) getCmd(opts *Opts, mgrCmd string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(y.rootCmd), mgrCmd)
cmd.Args = append(cmd.Args, args...)
} else {
cmd = exec.Command(mgrCmd, args...)
}
if opts.NoConfirm {
cmd.Args = append(cmd.Args, "-y")
}
return cmd
}

View File

@ -47,8 +47,9 @@ func (z *Zypper) SetRootCmd(s string) {
z.rootCmd = s
}
func (z *Zypper) Sync() error {
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "refresh")
func (z *Zypper) Sync(opts *Opts) error {
opts = ensureOpts(opts)
cmd := z.getCmd(opts, "zypper", "refresh")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -57,8 +58,9 @@ func (z *Zypper) Sync() error {
return nil
}
func (z *Zypper) Install(pkgs ...string) error {
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "install", "-y")
func (z *Zypper) Install(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := z.getCmd(opts, "zypper", "install", "-y")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -68,12 +70,14 @@ func (z *Zypper) Install(pkgs ...string) error {
return nil
}
func (z *Zypper) InstallLocal(pkgs ...string) error {
return z.Install(pkgs...)
func (z *Zypper) InstallLocal(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
return z.Install(opts, pkgs...)
}
func (z *Zypper) Remove(pkgs ...string) error {
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "remove", "-y")
func (z *Zypper) Remove(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := z.getCmd(opts, "zypper", "remove", "-y")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -83,8 +87,9 @@ func (z *Zypper) Remove(pkgs ...string) error {
return nil
}
func (z *Zypper) Upgrade(pkgs ...string) error {
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "update", "-y")
func (z *Zypper) Upgrade(opts *Opts, pkgs ...string) error {
opts = ensureOpts(opts)
cmd := z.getCmd(opts, "zypper", "update", "-y")
cmd.Args = append(cmd.Args, pkgs...)
setCmdEnv(cmd)
err := cmd.Run()
@ -94,8 +99,9 @@ func (z *Zypper) Upgrade(pkgs ...string) error {
return nil
}
func (z *Zypper) UpgradeAll() error {
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "update", "-y")
func (z *Zypper) UpgradeAll(opts *Opts) error {
opts = ensureOpts(opts)
cmd := z.getCmd(opts, "zypper", "update", "-y")
setCmdEnv(cmd)
err := cmd.Run()
if err != nil {
@ -104,9 +110,16 @@ func (z *Zypper) UpgradeAll() error {
return nil
}
func (z *Zypper) ListInstalled() (map[string]string, error) {
func (z *Zypper) ListInstalled(opts *Opts) (map[string]string, error) {
opts = ensureOpts(opts)
out := map[string]string{}
cmd := exec.Command(getRootCmd(z.rootCmd), "rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(z.rootCmd), "rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
} else {
cmd = exec.Command("rpm", "-qa", "--queryformat", "%{NAME}\u200b%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\n")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -134,3 +147,19 @@ func (z *Zypper) ListInstalled() (map[string]string, error) {
return out, nil
}
func (z *Zypper) getCmd(opts *Opts, mgrCmd string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
if opts.AsRoot {
cmd = exec.Command(getRootCmd(z.rootCmd), mgrCmd)
cmd.Args = append(cmd.Args, args...)
} else {
cmd = exec.Command(mgrCmd, args...)
}
if opts.NoConfirm {
cmd.Args = append(cmd.Args, "-y")
}
return cmd
}

View File

@ -53,7 +53,7 @@ func upgradeCmd(c *cli.Context) error {
}
func checkForUpdates(ctx context.Context, mgr manager.Manager, info *distro.OSRelease) ([]string, error) {
installed, err := mgr.ListInstalled()
installed, err := mgr.ListInstalled(nil)
if err != nil {
return nil, err
}