Files
snack/pacman/pacman_linux.go
Tai Groot 7d3eb1f98b feat(pacman): implement Manager interface for pacman package manager
Adds full pacman wrapper with:
- All Manager interface methods (Install, Remove, Purge, Upgrade, Update,
  List, Search, Info, IsInstalled, Version, Available)
- Linux implementation using exec.CommandContext
- Non-linux stubs returning ErrUnsupportedPlatform
- Output parsing for -Q, -Ss, -Si formats
- Options support: WithSudo, WithAssumeYes, WithDryRun, WithRoot
- Unit tests for parsing and argument building
2026-02-25 20:23:28 +00:00

153 lines
4.0 KiB
Go

//go:build linux
package pacman
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"github.com/gogrlx/snack"
)
func available() bool {
_, err := exec.LookPath("pacman")
return err == nil
}
// buildArgs constructs the command name and argument list from the base args
// and the provided options.
func buildArgs(baseArgs []string, opts snack.Options) (string, []string) {
cmd := "pacman"
args := make([]string, 0, len(baseArgs)+4)
if opts.Root != "" {
args = append(args, "-r", opts.Root)
}
args = append(args, baseArgs...)
if opts.AssumeYes {
args = append(args, "--noconfirm")
}
if opts.DryRun {
args = append(args, "--print")
}
if opts.Sudo {
args = append([]string{cmd}, args...)
cmd = "sudo"
}
return cmd, args
}
func run(ctx context.Context, baseArgs []string, opts snack.Options) (string, error) {
cmd, args := buildArgs(baseArgs, opts)
c := exec.CommandContext(ctx, cmd, args...)
var stdout, stderr bytes.Buffer
c.Stdout = &stdout
c.Stderr = &stderr
err := c.Run()
if err != nil {
se := stderr.String()
if strings.Contains(se, "permission denied") || strings.Contains(se, "requires root") {
return "", fmt.Errorf("pacman: %w", snack.ErrPermissionDenied)
}
return "", fmt.Errorf("pacman: %s: %w", strings.TrimSpace(se), err)
}
return stdout.String(), nil
}
func install(ctx context.Context, pkgs []string, opts ...snack.Option) error {
o := snack.ApplyOptions(opts...)
args := append([]string{"-S", "--noconfirm"}, pkgs...)
_, err := run(ctx, args, o)
return err
}
func remove(ctx context.Context, pkgs []string, opts ...snack.Option) error {
o := snack.ApplyOptions(opts...)
args := append([]string{"-R", "--noconfirm"}, pkgs...)
_, err := run(ctx, args, o)
return err
}
func purge(ctx context.Context, pkgs []string, opts ...snack.Option) error {
o := snack.ApplyOptions(opts...)
args := append([]string{"-Rns", "--noconfirm"}, pkgs...)
_, err := run(ctx, args, o)
return err
}
func upgrade(ctx context.Context, opts ...snack.Option) error {
o := snack.ApplyOptions(opts...)
_, err := run(ctx, []string{"-Syu", "--noconfirm"}, o)
return err
}
func update(ctx context.Context) error {
_, err := run(ctx, []string{"-Sy"}, snack.Options{})
return err
}
func list(ctx context.Context) ([]snack.Package, error) {
out, err := run(ctx, []string{"-Q"}, snack.Options{})
if err != nil {
return nil, fmt.Errorf("pacman list: %w", err)
}
return parseList(out), nil
}
func search(ctx context.Context, query string) ([]snack.Package, error) {
out, err := run(ctx, []string{"-Ss", query}, snack.Options{})
if err != nil {
if strings.Contains(err.Error(), "exit status 1") {
return nil, nil
}
return nil, fmt.Errorf("pacman search: %w", err)
}
return parseSearch(out), nil
}
func info(ctx context.Context, pkg string) (*snack.Package, error) {
out, err := run(ctx, []string{"-Si", pkg}, snack.Options{})
if err != nil {
if strings.Contains(err.Error(), "exit status 1") {
return nil, fmt.Errorf("pacman info %s: %w", pkg, snack.ErrNotFound)
}
return nil, fmt.Errorf("pacman info: %w", err)
}
p := parseInfo(out)
if p == nil {
return nil, fmt.Errorf("pacman info %s: %w", pkg, snack.ErrNotFound)
}
return p, nil
}
func isInstalled(ctx context.Context, pkg string) (bool, error) {
c := exec.CommandContext(ctx, "pacman", "-Q", pkg)
err := c.Run()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
return false, nil
}
return false, fmt.Errorf("pacman isInstalled: %w", err)
}
return true, nil
}
func version(ctx context.Context, pkg string) (string, error) {
out, err := run(ctx, []string{"-Q", pkg}, snack.Options{})
if err != nil {
if strings.Contains(err.Error(), "exit status 1") {
return "", fmt.Errorf("pacman version %s: %w", pkg, snack.ErrNotInstalled)
}
return "", fmt.Errorf("pacman version: %w", err)
}
parts := strings.Fields(strings.TrimSpace(out))
if len(parts) < 2 {
return "", fmt.Errorf("pacman version %s: unexpected output %q", pkg, out)
}
return parts[1], nil
}