pkgmgr

package module
v0.0.0-...-03f319e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 17 Imported by: 0

README

go-package-manager

Go Reference

pkgmgr is a Go library that abstracts system package managers behind a single Manager interface. One code path can install, upgrade, search, and manage repositories on Debian, Fedora, openSUSE, Arch, Alpine, macOS, and more, without each caller hard-coding apt/dnf/pacman invocations.

API documentation is available on pkg.go.dev.

Supported managers

Manager Type Format Notes
zypper root rpm openSUSE
dnf root rpm Fedora / RHEL
yum root rpm Legacy RHEL
apt root deb Debian / Ubuntu
apt-get root deb Debian / Ubuntu
pacman root pacman Arch
apk root apk Alpine
brew unpriv. bottle Homebrew; refuses to run as root
yay unpriv. pacman AUR helper; pacman drop-in
paru unpriv. pacman AUR helper; pacman drop-in

Root-type managers escalate privileged commands through a configurable wrapper (sudo by default). Homebrew and the AUR helpers must not run as root, so they bypass the wrapper and instead drop to an unprivileged user via sudo -u when the process is running as root.

Installation

go get github.com/grmrgecko/go-package-manager

Usage

Resolve the active system manager, then drive it through the interface. The example below installs a package, escalating with sudo when not already root.

package main

import (
	"context"
	"log"

	pkgmgr "github.com/grmrgecko/go-package-manager"
)

func main() {
	m := pkgmgr.GetSystemManager()
	if m == nil {
		log.Fatal("no supported package manager found")
	}

	// Wrap privileged commands with sudo when not running as root.
	m.UseSudoWhenNeeded()

	ctx := context.Background()
	if err := m.Sync(ctx, nil); err != nil {
		log.Fatal(err)
	}
	if err := m.Install(ctx, nil, "htop"); err != nil {
		log.Fatal(err)
	}

	results, err := m.Search(ctx, nil, "editor")
	if err != nil {
		log.Fatal(err)
	}
	for _, r := range results {
		log.Printf("%s %s - %s", r.Name, r.Version, r.Summary)
	}
}
Selecting a manager explicitly

GetSystemManager auto-detects in priority order. To target a specific manager, construct it directly:

m := &pkgmgr.Apt{}
m.UseSudoWhenNeeded()

AUR helpers must be constructed through their constructors so the helper binary name is set:

yay := pkgmgr.NewYay()
paru := pkgmgr.NewParu()

Interface

The Manager interface covers the full package lifecycle. Methods that perform I/O take a context.Context for cancellation and timeouts.

  • Identity: Name, Format, Path
  • Privilege: SetCmdWrapper, UseSudoWhenNeeded
  • I/O: SetIO (override stdin/stdout/stderr of spawned commands)
  • Repos: AddRepo, AddRepoURL, RemoveRepo, GetRepo, ListRepos
  • Keys: AddRepoKey, AddRepoKeyFile, AddRepoKeyURL
  • Packages: Sync, Install, Remove, Upgrade, InstallFile, UpgradeAll, Clean
  • Queries: Search, Info, ListInstalled, ListUpgradable

Search returns structured SearchResult values; Info returns the underlying tool's native text output; ListInstalled and ListUpgradable return name -> version maps. ListRepos returns every configured repo as a name -> configuration map, where each value matches what GetRepo returns for that name.

Privilege handling

Two mechanisms cover opposite requirements:

  • Root managers (apt, dnf, pacman, ...) need root for mutating operations. UseSudoWhenNeeded sets the wrapper to sudo when the process is not already root.
  • Non-root managers (brew, yay, paru) refuse to run as root. They use an embedded dropPrivilege to run their command as an unprivileged user, dropping via sudo -u only when the process is root. The drop user defaults to SUDO_USER and can be set explicitly with SetDropUser.

A custom command wrapper (for example []string{"sudo", "-n"}) can be supplied through SetCmdWrapper.

Testing

go test ./...

The suite verifies that every manager satisfies the Manager interface and exercises the command wrapper, I/O overrides, privilege drop logic, and the output parsers.

Documentation

Index

Constants

View Source
const (
	// APK_REPOSITORIES is the single file apk reads its repositories from.
	// There is no drop-in directory, so named repos are delimited with marker
	// comments so they can be located and removed later.
	APK_REPOSITORIES = "/etc/apk/repositories"
	// APK_KEYS_DIR is where apk reads repo signing keys from.
	APK_KEYS_DIR = "/etc/apk/keys"
)
View Source
const (
	// APT_SOURCES_DIR is where apt repo files are written.
	APT_SOURCES_DIR = "/etc/apt/sources.list.d"
	// APT_KEYRINGS_DIR is where apt repo signing keys are written when apt-key
	// is unavailable. It is only used as a fallback on modern systems (apt has
	// removed apt-key), so the armored ".asc" form written here is always read
	// by the apt that is present.
	APT_KEYRINGS_DIR = "/etc/apt/trusted.gpg.d"
)
View Source
const PACMAN_CONF = "/etc/pacman.conf"

PACMAN_CONF is the configuration file pacman reads its repos from. pacman has no drop-in repo directory by default, so repos are managed as [name] sections within this file.

View Source
const YUM_REPO_DIR = "/etc/yum.repos.d"

YUM_REPO_DIR is where rpm-based managers (dnf, yum, zypper) write repo files.

Variables

This section is empty.

Functions

This section is empty.

Types

type Apk

type Apk struct {
	// contains filtered or unexported fields
}

func (*Apk) AddRepo

func (p *Apk) AddRepo(name, config string) error

AddRepo adds a repo from a configuration string (one repository URL per line). The lines are stored in APK_REPOSITORIES delimited by markers.

func (*Apk) AddRepoKey

func (p *Apk) AddRepoKey(ctx context.Context, key string) error

AddRepoKey adds a key for repo package verification.

func (*Apk) AddRepoKeyFile

func (p *Apk) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile adds a key for repo package verification from a file.

func (*Apk) AddRepoKeyURL

func (p *Apk) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL adds a key for repo package verification from a URL.

func (*Apk) AddRepoURL

func (p *Apk) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds repoURL as a repository line. apk repositories are plain URLs, so repoURL is used directly rather than downloaded.

func (*Apk) Clean

func (p *Apk) Clean(ctx context.Context, args []string) error

Clean removes cached package data. It requires a configured apk cache.

func (*Apk) Command

func (p *Apk) Command(ctx context.Context, command string, args ...string) *exec.Cmd

Command builds an *exec.Cmd for the manager, applying the configured command wrapper and I/O streams. The command is bound to ctx for cancellation.

func (*Apk) Format

func (p *Apk) Format() string

Format is the package format the manager installs.

func (*Apk) GetRepo

func (p *Apk) GetRepo(name string) string

GetRepo returns a repo's configuration, or "" if it does not exist.

func (*Apk) Info

func (p *Apk) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages.

func (*Apk) Install

func (p *Apk) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*Apk) InstallFile

func (p *Apk) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Apk) ListInstalled

func (p *Apk) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version.

func (*Apk) ListRepos

func (p *Apk) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every named repo section in APK_REPOSITORIES mapped name->configuration. The value matches what GetRepo returns for that name. Plain repository lines that are not enclosed in named markers have no name and so are not included.

func (*Apk) ListUpgradable

func (p *Apk) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version.

func (*Apk) Name

func (p *Apk) Name() string

Name is the package manager's name.

func (*Apk) Path

func (p *Apk) Path() string

Path is the resolved path to the apk command, or "" if missing.

func (*Apk) Remove

func (p *Apk) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Apk) RemoveRepo

func (p *Apk) RemoveRepo(name string) error

RemoveRepo removes a repo's lines from APK_REPOSITORIES.

func (*Apk) Search

func (p *Apk) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query.

func (*Apk) SetCmdWrapper

func (p *Apk) SetCmdWrapper(wrapper []string)

SetCmdWrapper sets a command wrapper, for example []string{"sudo"}.

func (*Apk) SetIO

func (p *Apk) SetIO(stdin io.Reader, stdout, stderr io.Writer)

SetIO overrides the stdin, stdout, and stderr used by spawned commands.

func (*Apk) Sync

func (p *Apk) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Apk) Upgrade

func (p *Apk) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Apk) UpgradeAll

func (p *Apk) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

func (*Apk) UseSudoWhenNeeded

func (p *Apk) UseSudoWhenNeeded()

UseSudoWhenNeeded sets the command wrapper to sudo when the process is not already root, so privileged operations escalate. It is a no-op when running as root. Managers that must not run as root (brew, yay, paru) bypass the wrapper for their own command, so this only affects their privileged pacman-side helpers and the root-required managers.

type Apt

type Apt struct {
	// contains filtered or unexported fields
}

func (*Apt) AddRepo

func (p *Apt) AddRepo(name, config string) error

AddRepo adds a repo from a configuration string.

func (*Apt) AddRepoKey

func (p *Apt) AddRepoKey(ctx context.Context, key string) error

AddRepoKey adds a key for repo package verification. apt-key is used when it is present, which keeps older releases (where it is the only supported method) working. On modern systems that have removed apt-key, a drop-in keyring file is written instead.

func (*Apt) AddRepoKeyFile

func (p *Apt) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile adds a key for repo package verification from a file.

func (*Apt) AddRepoKeyURL

func (p *Apt) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL adds a key for repo package verification from a URL.

func (*Apt) AddRepoURL

func (p *Apt) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds a repo whose configuration is downloaded from repoURL.

func (*Apt) Clean

func (p *Apt) Clean(ctx context.Context, args []string) error

Clean removes the local package cache.

func (*Apt) Format

func (p *Apt) Format() string

Format is the package format the manager installs.

func (*Apt) GetRepo

func (p *Apt) GetRepo(name string) string

GetRepo returns a repo's configuration, or "" if it does not exist.

func (*Apt) Info

func (p *Apt) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages.

func (*Apt) Install

func (p *Apt) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*Apt) InstallFile

func (p *Apt) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Apt) ListInstalled

func (p *Apt) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version.

func (*Apt) ListRepos

func (p *Apt) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every repo file in APT_SOURCES_DIR keyed by file stem. The value matches what GetRepo returns for that name. Only the .list drop-in files this manager reads and writes are enumerated; the main sources.list and deb822 .sources files are not included.

func (*Apt) ListUpgradable

func (p *Apt) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version. It simulates an upgrade with apt-get, which both apt and apt-get ship, so the listing is consistent and never prompts.

func (*Apt) Name

func (p *Apt) Name() string

Name is the package manager's name.

func (*Apt) Path

func (p *Apt) Path() string

Path is the resolved path to the apt command, or "" if missing.

func (*Apt) Remove

func (p *Apt) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Apt) RemoveRepo

func (p *Apt) RemoveRepo(name string) error

RemoveRepo removes a repo.

func (*Apt) Search

func (p *Apt) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query.

func (*Apt) Sync

func (p *Apt) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Apt) Upgrade

func (p *Apt) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Apt) UpgradeAll

func (p *Apt) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

type AptGet

type AptGet struct {
	// contains filtered or unexported fields
}

func (*AptGet) AddRepo

func (p *AptGet) AddRepo(name, config string) error

AddRepo adds a repo from a configuration string.

func (*AptGet) AddRepoKey

func (p *AptGet) AddRepoKey(ctx context.Context, key string) error

AddRepoKey adds a key for repo package verification. apt-key is used when it is present, which keeps older releases (where it is the only supported method) working. On modern systems that have removed apt-key, a drop-in keyring file is written instead.

func (*AptGet) AddRepoKeyFile

func (p *AptGet) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile adds a key for repo package verification from a file.

func (*AptGet) AddRepoKeyURL

func (p *AptGet) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL adds a key for repo package verification from a URL.

func (*AptGet) AddRepoURL

func (p *AptGet) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds a repo whose configuration is downloaded from repoURL.

func (*AptGet) Clean

func (p *AptGet) Clean(ctx context.Context, args []string) error

Clean removes the local package cache.

func (*AptGet) Format

func (p *AptGet) Format() string

Format is the package format the manager installs.

func (*AptGet) GetRepo

func (p *AptGet) GetRepo(name string) string

GetRepo returns a repo's configuration, or "" if it does not exist.

func (*AptGet) Info

func (p *AptGet) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages. apt-get has no show subcommand, so apt-cache is used.

func (*AptGet) Install

func (p *AptGet) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*AptGet) InstallFile

func (p *AptGet) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*AptGet) ListInstalled

func (p *AptGet) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version.

func (*AptGet) ListRepos

func (p *AptGet) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every repo file in APT_SOURCES_DIR keyed by file stem. The value matches what GetRepo returns for that name. Only the .list drop-in files this manager reads and writes are enumerated; the main sources.list and deb822 .sources files are not included.

func (*AptGet) ListUpgradable

func (p *AptGet) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version. It simulates an upgrade with apt-get, which both apt and apt-get ship, so the listing is consistent and never prompts.

func (*AptGet) Name

func (p *AptGet) Name() string

Name is the package manager's name.

func (*AptGet) Path

func (p *AptGet) Path() string

Path is the resolved path to the apt-get command, or "" if missing.

func (*AptGet) Remove

func (p *AptGet) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*AptGet) RemoveRepo

func (p *AptGet) RemoveRepo(name string) error

RemoveRepo removes a repo.

func (*AptGet) Search

func (p *AptGet) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query. apt-get has no search subcommand, so apt-cache is used.

func (*AptGet) Sync

func (p *AptGet) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*AptGet) Upgrade

func (p *AptGet) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*AptGet) UpgradeAll

func (p *AptGet) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

type Brew

type Brew struct {
	// contains filtered or unexported fields
}

Brew is the Homebrew manager. Homebrew refuses to run as root, so its command is never sudo-wrapped; the embedded dropPrivilege runs it unprivileged and drops to a user via `sudo -u` when the process is root.

func (*Brew) AddRepo

func (p *Brew) AddRepo(name, config string) error

AddRepo adds a repo by tapping it. config, when set, is the tap's clone URL.

func (*Brew) AddRepoKey

func (p *Brew) AddRepoKey(ctx context.Context, key string) error

AddRepoKey is a no-op: Homebrew verifies downloads itself and has no user-managed signing keyring.

func (*Brew) AddRepoKeyFile

func (p *Brew) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile is a no-op: Homebrew manages verification itself.

func (*Brew) AddRepoKeyURL

func (p *Brew) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL is a no-op: Homebrew manages verification itself.

func (*Brew) AddRepoURL

func (p *Brew) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds a repo by tapping name from repoURL.

func (*Brew) Clean

func (p *Brew) Clean(ctx context.Context, args []string) error

Clean removes stale downloads and old installed versions. brew is run unprivileged since Homebrew must not run as root.

func (*Brew) Command

func (p *Brew) Command(ctx context.Context, command string, args ...string) *exec.Cmd

Command builds an *exec.Cmd for the manager, applying the configured command wrapper and I/O streams. The command is bound to ctx for cancellation.

func (*Brew) Format

func (p *Brew) Format() string

Format is the package format the manager installs.

func (*Brew) GetRepo

func (p *Brew) GetRepo(name string) string

GetRepo returns tap information for name, or "" if it is not tapped.

func (*Brew) Info

func (p *Brew) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages. brew is run unprivileged since Homebrew must not run as root.

func (*Brew) Install

func (p *Brew) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*Brew) InstallFile

func (p *Brew) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local formula or cask file.

func (*Brew) ListInstalled

func (p *Brew) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version.

func (*Brew) ListRepos

func (p *Brew) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every tapped repo mapped name->tap-info. The value for each name matches what GetRepo returns for it. brew is run unprivileged since Homebrew must not run as root.

func (*Brew) ListUpgradable

func (p *Brew) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version. brew is run unprivileged since Homebrew must not run as root.

func (*Brew) Name

func (p *Brew) Name() string

Name is the package manager's name.

func (*Brew) Path

func (p *Brew) Path() string

Path is the resolved path to the brew command, or "" if missing.

func (*Brew) Remove

func (p *Brew) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Brew) RemoveRepo

func (p *Brew) RemoveRepo(name string) error

RemoveRepo removes a repo by untapping it.

func (*Brew) Search

func (p *Brew) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query. brew is run unprivileged since Homebrew must not run as root.

func (*Brew) SetCmdWrapper

func (p *Brew) SetCmdWrapper(wrapper []string)

SetCmdWrapper sets a command wrapper, for example []string{"sudo"}.

func (*Brew) SetDropUser

func (d *Brew) SetDropUser(user string)

SetDropUser sets the unprivileged user the command drops to when the process runs as root. When unset, the SUDO_USER environment variable is used.

func (*Brew) SetIO

func (p *Brew) SetIO(stdin io.Reader, stdout, stderr io.Writer)

SetIO overrides the stdin, stdout, and stderr used by spawned commands.

func (*Brew) Sync

func (p *Brew) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Brew) Upgrade

func (p *Brew) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Brew) UpgradeAll

func (p *Brew) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

func (*Brew) UseSudoWhenNeeded

func (p *Brew) UseSudoWhenNeeded()

UseSudoWhenNeeded sets the command wrapper to sudo when the process is not already root, so privileged operations escalate. It is a no-op when running as root. Managers that must not run as root (brew, yay, paru) bypass the wrapper for their own command, so this only affects their privileged pacman-side helpers and the root-required managers.

type Dnf

type Dnf struct {
	// contains filtered or unexported fields
}

func (*Dnf) AddRepo

func (p *Dnf) AddRepo(name, config string) error

AddRepo adds a repo from a configuration string.

func (*Dnf) AddRepoKey

func (p *Dnf) AddRepoKey(ctx context.Context, key string) error

AddRepoKey adds a key for repo package verification.

func (*Dnf) AddRepoKeyFile

func (p *Dnf) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile adds a key for repo package verification from a file.

func (*Dnf) AddRepoKeyURL

func (p *Dnf) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL adds a key for repo package verification from a URL.

func (*Dnf) AddRepoURL

func (p *Dnf) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds a repo whose configuration is downloaded from repoURL.

func (*Dnf) Clean

func (p *Dnf) Clean(ctx context.Context, args []string) error

Clean removes cached repository metadata and packages.

func (*Dnf) Format

func (p *Dnf) Format() string

Format is the package format the manager installs.

func (*Dnf) GetRepo

func (p *Dnf) GetRepo(name string) string

GetRepo returns a repo's configuration, or "" if it does not exist.

func (*Dnf) Info

func (p *Dnf) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages.

func (*Dnf) Install

func (p *Dnf) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*Dnf) InstallFile

func (p *Dnf) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Dnf) ListInstalled

func (p *Dnf) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version. It queries rpm directly so the listing is consistent across dnf, yum, and zypper.

func (*Dnf) ListRepos

func (p *Dnf) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every repo file in YUM_REPO_DIR keyed by file stem. The value matches what GetRepo returns for that name.

func (*Dnf) ListUpgradable

func (p *Dnf) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version.

func (*Dnf) Name

func (p *Dnf) Name() string

Name is the package manager's name.

func (*Dnf) Path

func (p *Dnf) Path() string

Path is the resolved path to the dnf command, or "" if missing.

func (*Dnf) Remove

func (p *Dnf) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Dnf) RemoveRepo

func (p *Dnf) RemoveRepo(name string) error

RemoveRepo removes a repo.

func (*Dnf) Search

func (p *Dnf) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query.

func (*Dnf) Sync

func (p *Dnf) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Dnf) Upgrade

func (p *Dnf) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Dnf) UpgradeAll

func (p *Dnf) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

type Manager

type Manager interface {
	// Name is the package manager's name.
	Name() string
	// Format is the package format the manager installs (deb, rpm, ...).
	Format() string
	// Path is the resolved path to the manager's command, or "" if missing.
	Path() string
	// SetCmdWrapper sets a command wrapper, for example []string{"sudo"}.
	SetCmdWrapper(wrapper []string)
	// UseSudoWhenNeeded wraps privileged commands with sudo when not already
	// root. It is a no-op as root, and managers that must not run as root
	// (brew, yay, paru) bypass the wrapper for their own command.
	UseSudoWhenNeeded()
	// SetIO overrides the stdin, stdout, and stderr used by spawned commands.
	// Passing nil for a stream restores its os default.
	SetIO(stdin io.Reader, stdout, stderr io.Writer)
	// AddRepo adds a repo from a configuration string.
	AddRepo(name, config string) error
	// AddRepoURL adds a repo whose configuration is downloaded from repoURL.
	AddRepoURL(ctx context.Context, name, repoURL string) error
	// RemoveRepo removes a repo.
	RemoveRepo(name string) error
	// GetRepo returns a repo's configuration, or "" if it does not exist.
	GetRepo(name string) string
	// ListRepos returns all configured repos mapped name->configuration. The
	// value for each name matches what GetRepo returns for it.
	ListRepos(ctx context.Context, args []string) (map[string]string, error)
	// AddRepoKey adds a key for repo package verification.
	AddRepoKey(ctx context.Context, key string) error
	// AddRepoKeyFile adds a key for repo package verification from a file.
	AddRepoKeyFile(ctx context.Context, keyFile string) error
	// AddRepoKeyURL adds a key for repo package verification from a URL.
	AddRepoKeyURL(ctx context.Context, keyURL string) error
	// Sync updates repository metadata.
	Sync(ctx context.Context, args []string) error
	// Install installs packages from the repositories.
	Install(ctx context.Context, args []string, packages ...string) error
	// Remove removes packages.
	Remove(ctx context.Context, args []string, packages ...string) error
	// Upgrade upgrades the named packages.
	Upgrade(ctx context.Context, args []string, packages ...string) error
	// InstallFile installs a package from a local file.
	InstallFile(ctx context.Context, args []string, packages ...string) error
	// UpgradeAll upgrades all packages with available updates.
	UpgradeAll(ctx context.Context, args []string) error
	// Clean removes cached package data to reclaim disk space.
	Clean(ctx context.Context, args []string) error
	// Search searches the repositories for packages matching query and returns
	// the matches.
	Search(ctx context.Context, args []string, query string) ([]SearchResult, error)
	// Info returns detailed information about the named packages as the
	// underlying tool's native text output.
	Info(ctx context.Context, args []string, packages ...string) (string, error)
	// ListInstalled returns installed packages mapped to their version.
	ListInstalled(ctx context.Context, args []string) (map[string]string, error)
	// ListUpgradable returns packages with an available update mapped to the
	// candidate version, without applying the updates.
	ListUpgradable(ctx context.Context, args []string) (map[string]string, error)
}

Manager is the interface for working with a system package manager. Methods that run a subprocess or perform network I/O take a context.Context so the caller can apply cancellation and timeouts.

func GetSystemManager

func GetSystemManager() Manager

GetSystemManager finds the system manager with a priority of zypper, dnf, yum, apt, apt-get, pacman, apk, brew. It returns nil when none are found.

type Pacman

type Pacman struct {
	// contains filtered or unexported fields
}

func (*Pacman) AddRepo

func (p *Pacman) AddRepo(name, config string) error

AddRepo adds a repo as a [name] section in PACMAN_CONF.

func (*Pacman) AddRepoKey

func (p *Pacman) AddRepoKey(ctx context.Context, key string) error

AddRepoKey adds a key for repo package verification.

func (*Pacman) AddRepoKeyFile

func (p *Pacman) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile adds a key for repo package verification from a file.

func (*Pacman) AddRepoKeyURL

func (p *Pacman) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL adds a key for repo package verification from a URL.

func (*Pacman) AddRepoURL

func (p *Pacman) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds a repo whose configuration is downloaded from repoURL.

func (*Pacman) Clean

func (p *Pacman) Clean(ctx context.Context, args []string) error

Clean removes cached packages from the package cache.

func (*Pacman) Command

func (p *Pacman) Command(ctx context.Context, command string, args ...string) *exec.Cmd

Command builds an *exec.Cmd for the manager, applying the configured command wrapper and I/O streams. The command is bound to ctx for cancellation.

func (*Pacman) Format

func (p *Pacman) Format() string

Format is the package format the manager installs.

func (*Pacman) GetRepo

func (p *Pacman) GetRepo(name string) string

GetRepo returns a repo's configuration, or "" if it does not exist.

func (*Pacman) Info

func (p *Pacman) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages.

func (*Pacman) Install

func (p *Pacman) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*Pacman) InstallFile

func (p *Pacman) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Pacman) ListInstalled

func (p *Pacman) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version.

func (*Pacman) ListRepos

func (p *Pacman) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every repository section in PACMAN_CONF mapped name->body. The value matches what GetRepo returns for that name. pacman treats every section other than [options] as a repository, so [options] is excluded.

func (*Pacman) ListUpgradable

func (p *Pacman) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version.

func (*Pacman) Name

func (p *Pacman) Name() string

Name is the package manager's name.

func (*Pacman) Path

func (p *Pacman) Path() string

Path is the resolved path to the pacman command, or "" if missing.

func (*Pacman) Remove

func (p *Pacman) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Pacman) RemoveRepo

func (p *Pacman) RemoveRepo(name string) error

RemoveRepo removes a repo's section from PACMAN_CONF.

func (*Pacman) Search

func (p *Pacman) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query.

func (*Pacman) SetCmdWrapper

func (p *Pacman) SetCmdWrapper(wrapper []string)

SetCmdWrapper sets a command wrapper, for example []string{"sudo"}.

func (*Pacman) SetIO

func (p *Pacman) SetIO(stdin io.Reader, stdout, stderr io.Writer)

SetIO overrides the stdin, stdout, and stderr used by spawned commands.

func (*Pacman) Sync

func (p *Pacman) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Pacman) Upgrade

func (p *Pacman) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Pacman) UpgradeAll

func (p *Pacman) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

func (*Pacman) UseSudoWhenNeeded

func (p *Pacman) UseSudoWhenNeeded()

UseSudoWhenNeeded sets the command wrapper to sudo when the process is not already root, so privileged operations escalate. It is a no-op when running as root. Managers that must not run as root (brew, yay, paru) bypass the wrapper for their own command, so this only affects their privileged pacman-side helpers and the root-required managers.

type Paru

type Paru struct {
	// contains filtered or unexported fields
}

Paru is the AUR helper manager backed by the paru command.

func NewParu

func NewParu() *Paru

NewParu returns a Paru manager with its helper binary name set.

func (*Paru) Clean

func (p *Paru) Clean(ctx context.Context, args []string) error

Clean removes cached packages, including the helper's AUR build cache.

func (*Paru) Info

func (p *Paru) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages. It is run unprivileged since the helper refuses to run as root.

func (*Paru) Install

func (p *Paru) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories and the AUR.

func (*Paru) InstallFile

func (p *Paru) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Paru) ListUpgradable

func (p *Paru) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns repo and AUR packages with an available update mapped to the candidate version. The helper's `-Qu` covers the AUR, and like pacman it exits 1 when nothing is upgradable, which is treated as an empty result.

func (*Paru) Name

func (p *Paru) Name() string

Name is the package manager's name.

func (*Paru) Path

func (p *Paru) Path() string

Path is the resolved path to the helper command, or "" if missing.

func (*Paru) Remove

func (p *Paru) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Paru) Search

func (p *Paru) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories and the AUR for packages matching query. The helper is a pacman drop-in, so its output is parsed like pacman's. It is run unprivileged since the helper refuses to run as root.

func (*Paru) Sync

func (p *Paru) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Paru) Upgrade

func (p *Paru) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Paru) UpgradeAll

func (p *Paru) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates, including the AUR.

type SearchResult

type SearchResult struct {
	Name    string
	Version string
	Summary string
}

SearchResult is a single package match returned by Search. Version and Summary are best-effort: a field is left empty when the underlying tool does not report it in its search output.

type Yay

type Yay struct {
	// contains filtered or unexported fields
}

Yay is the AUR helper manager backed by the yay command.

func NewYay

func NewYay() *Yay

NewYay returns a Yay manager with its helper binary name set.

func (*Yay) Clean

func (p *Yay) Clean(ctx context.Context, args []string) error

Clean removes cached packages, including the helper's AUR build cache.

func (*Yay) Info

func (p *Yay) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages. It is run unprivileged since the helper refuses to run as root.

func (*Yay) Install

func (p *Yay) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories and the AUR.

func (*Yay) InstallFile

func (p *Yay) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Yay) ListUpgradable

func (p *Yay) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns repo and AUR packages with an available update mapped to the candidate version. The helper's `-Qu` covers the AUR, and like pacman it exits 1 when nothing is upgradable, which is treated as an empty result.

func (*Yay) Name

func (p *Yay) Name() string

Name is the package manager's name.

func (*Yay) Path

func (p *Yay) Path() string

Path is the resolved path to the helper command, or "" if missing.

func (*Yay) Remove

func (p *Yay) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Yay) Search

func (p *Yay) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories and the AUR for packages matching query. The helper is a pacman drop-in, so its output is parsed like pacman's. It is run unprivileged since the helper refuses to run as root.

func (*Yay) Sync

func (p *Yay) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Yay) Upgrade

func (p *Yay) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Yay) UpgradeAll

func (p *Yay) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates, including the AUR.

type Yum

type Yum struct {
	// contains filtered or unexported fields
}

func (*Yum) AddRepo

func (p *Yum) AddRepo(name, config string) error

AddRepo adds a repo from a configuration string.

func (*Yum) AddRepoKey

func (p *Yum) AddRepoKey(ctx context.Context, key string) error

AddRepoKey adds a key for repo package verification.

func (*Yum) AddRepoKeyFile

func (p *Yum) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile adds a key for repo package verification from a file.

func (*Yum) AddRepoKeyURL

func (p *Yum) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL adds a key for repo package verification from a URL.

func (*Yum) AddRepoURL

func (p *Yum) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds a repo whose configuration is downloaded from repoURL.

func (*Yum) Clean

func (p *Yum) Clean(ctx context.Context, args []string) error

Clean removes cached repository metadata and packages.

func (*Yum) Format

func (p *Yum) Format() string

Format is the package format the manager installs.

func (*Yum) GetRepo

func (p *Yum) GetRepo(name string) string

GetRepo returns a repo's configuration, or "" if it does not exist.

func (*Yum) Info

func (p *Yum) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages.

func (*Yum) Install

func (p *Yum) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*Yum) InstallFile

func (p *Yum) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Yum) ListInstalled

func (p *Yum) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version. It queries rpm directly so the listing is consistent across dnf, yum, and zypper.

func (*Yum) ListRepos

func (p *Yum) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every repo file in YUM_REPO_DIR keyed by file stem. The value matches what GetRepo returns for that name.

func (*Yum) ListUpgradable

func (p *Yum) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version.

func (*Yum) Name

func (p *Yum) Name() string

Name is the package manager's name.

func (*Yum) Path

func (p *Yum) Path() string

Path is the resolved path to the yum command, or "" if missing.

func (*Yum) Remove

func (p *Yum) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Yum) RemoveRepo

func (p *Yum) RemoveRepo(name string) error

RemoveRepo removes a repo.

func (*Yum) Search

func (p *Yum) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query.

func (*Yum) Sync

func (p *Yum) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Yum) Upgrade

func (p *Yum) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Yum) UpgradeAll

func (p *Yum) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

type Zypper

type Zypper struct {
	// contains filtered or unexported fields
}

func (*Zypper) AddRepo

func (p *Zypper) AddRepo(name, config string) error

AddRepo adds a repo from a configuration string.

func (*Zypper) AddRepoKey

func (p *Zypper) AddRepoKey(ctx context.Context, key string) error

AddRepoKey adds a key for repo package verification.

func (*Zypper) AddRepoKeyFile

func (p *Zypper) AddRepoKeyFile(ctx context.Context, keyFile string) error

AddRepoKeyFile adds a key for repo package verification from a file.

func (*Zypper) AddRepoKeyURL

func (p *Zypper) AddRepoKeyURL(ctx context.Context, keyURL string) error

AddRepoKeyURL adds a key for repo package verification from a URL.

func (*Zypper) AddRepoURL

func (p *Zypper) AddRepoURL(ctx context.Context, name, repoURL string) error

AddRepoURL adds a repo whose configuration is downloaded from repoURL.

func (*Zypper) Clean

func (p *Zypper) Clean(ctx context.Context, args []string) error

Clean removes cached repository metadata and packages.

func (*Zypper) Format

func (p *Zypper) Format() string

Format is the package format the manager installs.

func (*Zypper) GetRepo

func (p *Zypper) GetRepo(name string) string

GetRepo returns a repo's configuration, or "" if it does not exist.

func (*Zypper) Info

func (p *Zypper) Info(ctx context.Context, args []string, packages ...string) (string, error)

Info returns detailed information about the named packages.

func (*Zypper) Install

func (p *Zypper) Install(ctx context.Context, args []string, packages ...string) error

Install installs packages from the repositories.

func (*Zypper) InstallFile

func (p *Zypper) InstallFile(ctx context.Context, args []string, packages ...string) error

InstallFile installs a package from a local file.

func (*Zypper) ListInstalled

func (p *Zypper) ListInstalled(ctx context.Context, args []string) (map[string]string, error)

ListInstalled returns installed packages mapped to their version. It queries rpm directly so the listing is consistent across dnf, yum, and zypper.

func (*Zypper) ListRepos

func (p *Zypper) ListRepos(ctx context.Context, args []string) (map[string]string, error)

ListRepos returns every repo file in YUM_REPO_DIR keyed by file stem. The value matches what GetRepo returns for that name.

func (*Zypper) ListUpgradable

func (p *Zypper) ListUpgradable(ctx context.Context, args []string) (map[string]string, error)

ListUpgradable returns packages with an available update mapped to the candidate version.

func (*Zypper) Name

func (p *Zypper) Name() string

Name is the package manager's name.

func (*Zypper) Path

func (p *Zypper) Path() string

Path is the resolved path to the zypper command, or "" if missing.

func (*Zypper) Remove

func (p *Zypper) Remove(ctx context.Context, args []string, packages ...string) error

Remove removes packages.

func (*Zypper) RemoveRepo

func (p *Zypper) RemoveRepo(name string) error

RemoveRepo removes a repo.

func (*Zypper) Search

func (p *Zypper) Search(ctx context.Context, args []string, query string) ([]SearchResult, error)

Search searches the repositories for packages matching query.

func (*Zypper) Sync

func (p *Zypper) Sync(ctx context.Context, args []string) error

Sync updates repository metadata.

func (*Zypper) Upgrade

func (p *Zypper) Upgrade(ctx context.Context, args []string, packages ...string) error

Upgrade upgrades the named packages.

func (*Zypper) UpgradeAll

func (p *Zypper) UpgradeAll(ctx context.Context, args []string) error

UpgradeAll upgrades all packages with available updates.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL