Add options to package managers
This commit is contained in:
parent
1f39f5edf1
commit
c09574e659
8
build.go
8
build.go
@ -374,7 +374,13 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
|
|||||||
}
|
}
|
||||||
|
|
||||||
if removeBuildDeps {
|
if removeBuildDeps {
|
||||||
err = mgr.Remove(vars.BuildDepends...)
|
err = mgr.Remove(
|
||||||
|
&manager.Opts{
|
||||||
|
AsRoot: true,
|
||||||
|
NoConfirm: true,
|
||||||
|
},
|
||||||
|
vars.BuildDepends...,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ func installPkgs(ctx context.Context, pkgs []string, mgr manager.Manager) {
|
|||||||
scripts, notFound := findPkgs(pkgs)
|
scripts, notFound := findPkgs(pkgs)
|
||||||
|
|
||||||
if len(notFound) > 0 {
|
if len(notFound) > 0 {
|
||||||
err = mgr.Install(notFound...)
|
err = mgr.Install(nil, notFound...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error installing native packages").Err(err).Send()
|
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()
|
log.Fatal("Error building package").Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mgr.InstallLocal(builtPkgs...)
|
err = mgr.InstallLocal(nil, builtPkgs...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error installing package").Err(err).Send()
|
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()
|
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 {
|
if err != nil {
|
||||||
log.Fatal("Error removing packages").Err(err).Send()
|
log.Fatal("Error removing packages").Err(err).Send()
|
||||||
}
|
}
|
||||||
|
@ -47,8 +47,9 @@ func (a *APK) SetRootCmd(s string) {
|
|||||||
a.rootCmd = s
|
a.rootCmd = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APK) Sync() error {
|
func (a *APK) Sync(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "update")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apk", "update")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,8 +58,9 @@ func (a *APK) Sync() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APK) Install(pkgs ...string) error {
|
func (a *APK) Install(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "add")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apk", "add")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -68,8 +70,9 @@ func (a *APK) Install(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APK) InstallLocal(pkgs ...string) error {
|
func (a *APK) InstallLocal(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "add", "--allow-untrusted")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apk", "add", "--allow-untrusted")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -79,8 +82,9 @@ func (a *APK) InstallLocal(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APK) Remove(pkgs ...string) error {
|
func (a *APK) Remove(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "del")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apt", "del")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -90,8 +94,9 @@ func (a *APK) Remove(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APK) Upgrade(pkgs ...string) error {
|
func (a *APK) Upgrade(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apk", "upgrade")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apk", "upgrade")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -101,13 +106,21 @@ func (a *APK) Upgrade(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APK) UpgradeAll() error {
|
func (a *APK) UpgradeAll(opts *Opts) error {
|
||||||
return a.Upgrade()
|
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{}
|
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()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -141,3 +154,19 @@ func (a *APK) ListInstalled() (map[string]string, error) {
|
|||||||
|
|
||||||
return out, nil
|
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
|
||||||
|
}
|
||||||
|
@ -47,8 +47,9 @@ func (a *APT) SetRootCmd(s string) {
|
|||||||
a.rootCmd = s
|
a.rootCmd = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APT) Sync() error {
|
func (a *APT) Sync(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "update", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apt", "update")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,8 +58,9 @@ func (a *APT) Sync() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APT) Install(pkgs ...string) error {
|
func (a *APT) Install(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "install", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apt", "install")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -68,12 +70,14 @@ func (a *APT) Install(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APT) InstallLocal(pkgs ...string) error {
|
func (a *APT) InstallLocal(opts *Opts, pkgs ...string) error {
|
||||||
return a.Install(pkgs...)
|
opts = ensureOpts(opts)
|
||||||
|
return a.Install(opts, pkgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APT) Remove(pkgs ...string) error {
|
func (a *APT) Remove(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "remove", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apt", "remove")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -83,12 +87,14 @@ func (a *APT) Remove(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APT) Upgrade(pkgs ...string) error {
|
func (a *APT) Upgrade(opts *Opts, pkgs ...string) error {
|
||||||
return a.Install(pkgs...)
|
opts = ensureOpts(opts)
|
||||||
|
return a.Install(opts, pkgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APT) UpgradeAll() error {
|
func (a *APT) UpgradeAll(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(a.rootCmd), "apt", "upgrade", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := a.getCmd(opts, "apt", "upgrade")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -97,9 +103,16 @@ func (a *APT) UpgradeAll() error {
|
|||||||
return nil
|
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{}
|
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()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -127,3 +140,19 @@ func (a *APT) ListInstalled() (map[string]string, error) {
|
|||||||
|
|
||||||
return out, nil
|
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
|
||||||
|
}
|
||||||
|
@ -47,8 +47,9 @@ func (d *DNF) SetRootCmd(s string) {
|
|||||||
d.rootCmd = s
|
d.rootCmd = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNF) Sync() error {
|
func (d *DNF) Sync(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "upgrade", "--assumeno")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := d.getCmd(opts, "dnf", "upgrade")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,8 +58,9 @@ func (d *DNF) Sync() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNF) Install(pkgs ...string) error {
|
func (d *DNF) Install(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "install", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := d.getCmd(opts, "dnf", "install")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -68,12 +70,14 @@ func (d *DNF) Install(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNF) InstallLocal(pkgs ...string) error {
|
func (d *DNF) InstallLocal(opts *Opts, pkgs ...string) error {
|
||||||
return d.Install(pkgs...)
|
opts = ensureOpts(opts)
|
||||||
|
return d.Install(opts, pkgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNF) Remove(pkgs ...string) error {
|
func (d *DNF) Remove(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "remove", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := d.getCmd(opts, "dnf", "remove")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -83,8 +87,9 @@ func (d *DNF) Remove(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNF) Upgrade(pkgs ...string) error {
|
func (d *DNF) Upgrade(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "upgrade", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := d.getCmd(opts, "dnf", "upgrade")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -94,8 +99,9 @@ func (d *DNF) Upgrade(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNF) UpgradeAll() error {
|
func (d *DNF) UpgradeAll(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(d.rootCmd), "dnf", "upgrade", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := d.getCmd(opts, "dnf", "upgrade")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,9 +110,16 @@ func (d *DNF) UpgradeAll() error {
|
|||||||
return nil
|
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{}
|
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()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -134,3 +147,19 @@ func (d *DNF) ListInstalled() (map[string]string, error) {
|
|||||||
|
|
||||||
return out, nil
|
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
|
||||||
|
}
|
||||||
|
@ -23,6 +23,16 @@ import (
|
|||||||
"os/exec"
|
"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
|
// DefaultRootCmd is the command used for privilege elevation by default
|
||||||
var DefaultRootCmd = "sudo"
|
var DefaultRootCmd = "sudo"
|
||||||
|
|
||||||
@ -52,19 +62,19 @@ type Manager interface {
|
|||||||
// Sets the command used to elevate privileges. Defaults to DefaultRootCmd.
|
// Sets the command used to elevate privileges. Defaults to DefaultRootCmd.
|
||||||
SetRootCmd(string)
|
SetRootCmd(string)
|
||||||
// Sync fetches repositories without installing anything
|
// Sync fetches repositories without installing anything
|
||||||
Sync() error
|
Sync(*Opts) error
|
||||||
// Install installs packages
|
// Install installs packages
|
||||||
Install(...string) error
|
Install(*Opts, ...string) error
|
||||||
// Remove uninstalls packages
|
// Remove uninstalls packages
|
||||||
Remove(...string) error
|
Remove(*Opts, ...string) error
|
||||||
// Upgrade upgrades packages
|
// Upgrade upgrades packages
|
||||||
Upgrade(...string) error
|
Upgrade(*Opts, ...string) error
|
||||||
// InstallLocal installs packages from local files rather than repos
|
// InstallLocal installs packages from local files rather than repos
|
||||||
InstallLocal(...string) error
|
InstallLocal(*Opts, ...string) error
|
||||||
// UpgradeAll upgrades all packages
|
// UpgradeAll upgrades all packages
|
||||||
UpgradeAll() error
|
UpgradeAll(*Opts) error
|
||||||
// ListInstalled returns all installed packages mapped to their versions
|
// 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
|
// Detect returns the package manager detected on the system
|
||||||
@ -101,3 +111,10 @@ func setCmdEnv(cmd *exec.Cmd) {
|
|||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureOpts(opts *Opts) *Opts {
|
||||||
|
if opts == nil {
|
||||||
|
return DefaultOpts
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
}
|
@ -47,8 +47,9 @@ func (p *Pacman) SetRootCmd(s string) {
|
|||||||
p.rootCmd = s
|
p.rootCmd = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pacman) Sync() error {
|
func (p *Pacman) Sync(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-Sy")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := p.getCmd(opts, "pacman", "-Sy")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,8 +58,9 @@ func (p *Pacman) Sync() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pacman) Install(pkgs ...string) error {
|
func (p *Pacman) Install(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-S")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := p.getCmd(opts, "pacman", "-S")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -68,8 +70,9 @@ func (p *Pacman) Install(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pacman) InstallLocal(pkgs ...string) error {
|
func (p *Pacman) InstallLocal(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-U")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := p.getCmd(opts, "pacman", "-U")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -79,8 +82,9 @@ func (p *Pacman) InstallLocal(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pacman) Remove(pkgs ...string) error {
|
func (p *Pacman) Remove(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-R")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := p.getCmd(opts, "pacman", "-R")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -90,12 +94,14 @@ func (p *Pacman) Remove(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pacman) Upgrade(pkgs ...string) error {
|
func (p *Pacman) Upgrade(opts *Opts, pkgs ...string) error {
|
||||||
return p.Install(pkgs...)
|
opts = ensureOpts(opts)
|
||||||
|
return p.Install(opts, pkgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pacman) UpgradeAll() error {
|
func (p *Pacman) UpgradeAll(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(p.rootCmd), "pacman", "--noconfirm", "-Su")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := p.getCmd(opts, "pacman", "-Su")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,9 +110,16 @@ func (p *Pacman) UpgradeAll() error {
|
|||||||
return nil
|
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{}
|
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()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -134,3 +147,19 @@ func (p *Pacman) ListInstalled() (map[string]string, error) {
|
|||||||
|
|
||||||
return out, nil
|
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
|
||||||
|
}
|
||||||
|
@ -47,8 +47,9 @@ func (y *YUM) SetRootCmd(s string) {
|
|||||||
y.rootCmd = s
|
y.rootCmd = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (y *YUM) Sync() error {
|
func (y *YUM) Sync(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "upgrade", "--assumeno")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := y.getCmd(opts, "yum", "upgrade")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,8 +58,9 @@ func (y *YUM) Sync() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (y *YUM) Install(pkgs ...string) error {
|
func (y *YUM) Install(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "install", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := y.getCmd(opts, "yum", "install")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -68,12 +70,14 @@ func (y *YUM) Install(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (y *YUM) InstallLocal(pkgs ...string) error {
|
func (y *YUM) InstallLocal(opts *Opts, pkgs ...string) error {
|
||||||
return y.Install(pkgs...)
|
opts = ensureOpts(opts)
|
||||||
|
return y.Install(opts, pkgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (y *YUM) Remove(pkgs ...string) error {
|
func (y *YUM) Remove(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "remove", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := y.getCmd(opts, "yum", "remove")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -83,8 +87,9 @@ func (y *YUM) Remove(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (y *YUM) Upgrade(pkgs ...string) error {
|
func (y *YUM) Upgrade(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "upgrade", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := y.getCmd(opts, "yum", "upgrade")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -94,8 +99,9 @@ func (y *YUM) Upgrade(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (y *YUM) UpgradeAll() error {
|
func (y *YUM) UpgradeAll(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(y.rootCmd), "yum", "upgrade", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := y.getCmd(opts, "yum", "upgrade")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,9 +110,16 @@ func (y *YUM) UpgradeAll() error {
|
|||||||
return nil
|
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{}
|
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()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -134,3 +147,19 @@ func (y *YUM) ListInstalled() (map[string]string, error) {
|
|||||||
|
|
||||||
return out, nil
|
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
|
||||||
|
}
|
||||||
|
@ -47,8 +47,9 @@ func (z *Zypper) SetRootCmd(s string) {
|
|||||||
z.rootCmd = s
|
z.rootCmd = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zypper) Sync() error {
|
func (z *Zypper) Sync(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "refresh")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := z.getCmd(opts, "zypper", "refresh")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,8 +58,9 @@ func (z *Zypper) Sync() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zypper) Install(pkgs ...string) error {
|
func (z *Zypper) Install(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "install", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := z.getCmd(opts, "zypper", "install", "-y")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -68,12 +70,14 @@ func (z *Zypper) Install(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zypper) InstallLocal(pkgs ...string) error {
|
func (z *Zypper) InstallLocal(opts *Opts, pkgs ...string) error {
|
||||||
return z.Install(pkgs...)
|
opts = ensureOpts(opts)
|
||||||
|
return z.Install(opts, pkgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zypper) Remove(pkgs ...string) error {
|
func (z *Zypper) Remove(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "remove", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := z.getCmd(opts, "zypper", "remove", "-y")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -83,8 +87,9 @@ func (z *Zypper) Remove(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zypper) Upgrade(pkgs ...string) error {
|
func (z *Zypper) Upgrade(opts *Opts, pkgs ...string) error {
|
||||||
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "update", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := z.getCmd(opts, "zypper", "update", "-y")
|
||||||
cmd.Args = append(cmd.Args, pkgs...)
|
cmd.Args = append(cmd.Args, pkgs...)
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
@ -94,8 +99,9 @@ func (z *Zypper) Upgrade(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zypper) UpgradeAll() error {
|
func (z *Zypper) UpgradeAll(opts *Opts) error {
|
||||||
cmd := exec.Command(getRootCmd(z.rootCmd), "zypper", "update", "-y")
|
opts = ensureOpts(opts)
|
||||||
|
cmd := z.getCmd(opts, "zypper", "update", "-y")
|
||||||
setCmdEnv(cmd)
|
setCmdEnv(cmd)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,9 +110,16 @@ func (z *Zypper) UpgradeAll() error {
|
|||||||
return nil
|
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{}
|
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()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -134,3 +147,19 @@ func (z *Zypper) ListInstalled() (map[string]string, error) {
|
|||||||
|
|
||||||
return out, nil
|
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
|
||||||
|
}
|
||||||
|
@ -53,7 +53,7 @@ func upgradeCmd(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkForUpdates(ctx context.Context, mgr manager.Manager, info *distro.OSRelease) ([]string, 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user