mirror of
https://github.com/gogrlx/snack.git
synced 2026-04-02 05:08:42 -07:00
pkg implements Manager, VersionQuerier, Cleaner, and FileOwner interfaces wrapping FreeBSD's pkg(8) CLI. ports implements Manager wrapping OpenBSD's pkg_add/pkg_delete/pkg_info tools. Both use build tags (freebsd/openbsd) for real implementations with stub files for other platforms. Includes parser tests for all output formats.
137 lines
3.7 KiB
Go
137 lines
3.7 KiB
Go
//go:build openbsd
|
|
|
|
package ports
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/gogrlx/snack"
|
|
)
|
|
|
|
func available() bool {
|
|
_, err := exec.LookPath("pkg_add")
|
|
return err == nil
|
|
}
|
|
|
|
func runCmd(ctx context.Context, name string, args []string, opts snack.Options) (string, error) {
|
|
cmdName := name
|
|
cmdArgs := make([]string, 0, len(args)+2)
|
|
cmdArgs = append(cmdArgs, args...)
|
|
|
|
if opts.Sudo {
|
|
cmdArgs = append([]string{cmdName}, cmdArgs...)
|
|
cmdName = "sudo"
|
|
}
|
|
|
|
c := exec.CommandContext(ctx, cmdName, cmdArgs...)
|
|
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, "need root") {
|
|
return "", fmt.Errorf("ports: %w", snack.ErrPermissionDenied)
|
|
}
|
|
return "", fmt.Errorf("ports: %s: %w", strings.TrimSpace(se), err)
|
|
}
|
|
return stdout.String(), nil
|
|
}
|
|
|
|
func install(ctx context.Context, pkgs []snack.Target, opts ...snack.Option) error {
|
|
o := snack.ApplyOptions(opts...)
|
|
args := snack.TargetNames(pkgs)
|
|
_, err := runCmd(ctx, "pkg_add", args, o)
|
|
return err
|
|
}
|
|
|
|
func remove(ctx context.Context, pkgs []snack.Target, opts ...snack.Option) error {
|
|
o := snack.ApplyOptions(opts...)
|
|
args := snack.TargetNames(pkgs)
|
|
_, err := runCmd(ctx, "pkg_delete", args, o)
|
|
return err
|
|
}
|
|
|
|
func purge(ctx context.Context, pkgs []snack.Target, opts ...snack.Option) error {
|
|
o := snack.ApplyOptions(opts...)
|
|
args := append([]string{"-c"}, snack.TargetNames(pkgs)...)
|
|
_, err := runCmd(ctx, "pkg_delete", args, o)
|
|
return err
|
|
}
|
|
|
|
func upgrade(ctx context.Context, opts ...snack.Option) error {
|
|
o := snack.ApplyOptions(opts...)
|
|
_, err := runCmd(ctx, "pkg_add", []string{"-u"}, o)
|
|
return err
|
|
}
|
|
|
|
func update(_ context.Context) error {
|
|
// No-op on OpenBSD; updates handled via fw_update or syspatch.
|
|
return nil
|
|
}
|
|
|
|
func list(ctx context.Context) ([]snack.Package, error) {
|
|
out, err := runCmd(ctx, "pkg_info", nil, snack.Options{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ports list: %w", err)
|
|
}
|
|
return parseList(out), nil
|
|
}
|
|
|
|
func search(ctx context.Context, query string) ([]snack.Package, error) {
|
|
out, err := runCmd(ctx, "pkg_info", []string{"-Q", query}, snack.Options{})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "exit status 1") {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("ports search: %w", err)
|
|
}
|
|
return parseSearchResults(out), nil
|
|
}
|
|
|
|
func info(ctx context.Context, pkg string) (*snack.Package, error) {
|
|
out, err := runCmd(ctx, "pkg_info", []string{pkg}, snack.Options{})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "exit status 1") {
|
|
return nil, fmt.Errorf("ports info %s: %w", pkg, snack.ErrNotFound)
|
|
}
|
|
return nil, fmt.Errorf("ports info: %w", err)
|
|
}
|
|
p := parseInfoOutput(out, pkg)
|
|
if p == nil {
|
|
return nil, fmt.Errorf("ports info %s: %w", pkg, snack.ErrNotFound)
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func isInstalled(ctx context.Context, pkg string) (bool, error) {
|
|
c := exec.CommandContext(ctx, "pkg_info", pkg)
|
|
err := c.Run()
|
|
if err != nil {
|
|
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() != 0 {
|
|
return false, nil
|
|
}
|
|
return false, fmt.Errorf("ports isInstalled: %w", err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func version(ctx context.Context, pkg string) (string, error) {
|
|
out, err := runCmd(ctx, "pkg_info", []string{pkg}, snack.Options{})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "exit status 1") {
|
|
return "", fmt.Errorf("ports version %s: %w", pkg, snack.ErrNotInstalled)
|
|
}
|
|
return "", fmt.Errorf("ports version: %w", err)
|
|
}
|
|
p := parseInfoOutput(out, pkg)
|
|
if p == nil || p.Version == "" {
|
|
return "", fmt.Errorf("ports version %s: %w", pkg, snack.ErrNotInstalled)
|
|
}
|
|
return p.Version, nil
|
|
}
|