mirror of
https://github.com/gogrlx/snack.git
synced 2026-04-02 05:08:42 -07:00
feat(pacman,apk): implement VersionQuerier, Cleaner, FileOwner, and Grouper interfaces
pacman: - VersionQuerier: latestVersion via pacman -Si, listUpgrades via pacman -Qu, upgradeAvailable via pacman -Qu <pkg>, versionCmp via vercmp - Cleaner: autoremove via pacman -Qdtq | pacman -Rns, clean via pacman -Sc - FileOwner: fileList via pacman -Ql, owner via pacman -Qo - Grouper: groupList/groupInfo via pacman -Sg, groupInstall via pacman -S - Note: Holder skipped (no clean CLI support) apk: - VersionQuerier: latestVersion via apk policy, listUpgrades via apk upgrade --simulate, upgradeAvailable by checking upgrade list, versionCmp via apk version -t - Cleaner: clean via apk cache clean, autoremove is no-op (not supported) - FileOwner: fileList via apk info -L, owner via apk info --who-owns
This commit is contained in:
58
apk/capabilities.go
Normal file
58
apk/capabilities.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package apk
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogrlx/snack"
|
||||
)
|
||||
|
||||
// Compile-time interface checks.
|
||||
var (
|
||||
_ snack.VersionQuerier = (*Apk)(nil)
|
||||
_ snack.Cleaner = (*Apk)(nil)
|
||||
_ snack.FileOwner = (*Apk)(nil)
|
||||
)
|
||||
|
||||
// LatestVersion returns the latest available version from configured repositories.
|
||||
func (a *Apk) LatestVersion(ctx context.Context, pkg string) (string, error) {
|
||||
return latestVersion(ctx, pkg)
|
||||
}
|
||||
|
||||
// ListUpgrades returns packages that have newer versions available.
|
||||
func (a *Apk) ListUpgrades(ctx context.Context) ([]snack.Package, error) {
|
||||
return listUpgrades(ctx)
|
||||
}
|
||||
|
||||
// UpgradeAvailable reports whether a newer version is available.
|
||||
func (a *Apk) UpgradeAvailable(ctx context.Context, pkg string) (bool, error) {
|
||||
return upgradeAvailable(ctx, pkg)
|
||||
}
|
||||
|
||||
// VersionCmp compares two version strings using apk's native comparison.
|
||||
func (a *Apk) VersionCmp(ctx context.Context, ver1, ver2 string) (int, error) {
|
||||
return versionCmp(ctx, ver1, ver2)
|
||||
}
|
||||
|
||||
// Autoremove is not directly supported by apk. apk does not track
|
||||
// "auto-installed" dependencies in a way that allows safe autoremoval.
|
||||
// This is a no-op that returns nil.
|
||||
func (a *Apk) Autoremove(ctx context.Context, opts ...snack.Option) error {
|
||||
return autoremove(ctx, opts...)
|
||||
}
|
||||
|
||||
// Clean removes cached package files.
|
||||
func (a *Apk) Clean(ctx context.Context) error {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
return clean(ctx)
|
||||
}
|
||||
|
||||
// FileList returns all files installed by a package.
|
||||
func (a *Apk) FileList(ctx context.Context, pkg string) ([]string, error) {
|
||||
return fileList(ctx, pkg)
|
||||
}
|
||||
|
||||
// Owner returns the package that owns a given file path.
|
||||
func (a *Apk) Owner(ctx context.Context, path string) (string, error) {
|
||||
return owner(ctx, path)
|
||||
}
|
||||
177
apk/capabilities_linux.go
Normal file
177
apk/capabilities_linux.go
Normal file
@@ -0,0 +1,177 @@
|
||||
//go:build linux
|
||||
|
||||
package apk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/gogrlx/snack"
|
||||
)
|
||||
|
||||
func latestVersion(ctx context.Context, pkg string) (string, error) {
|
||||
// Use `apk policy` to get available versions
|
||||
c := exec.CommandContext(ctx, "apk", "policy", pkg)
|
||||
out, err := c.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("apk latestVersion %s: %w", pkg, snack.ErrNotFound)
|
||||
}
|
||||
// Output format:
|
||||
// pkg-1.2.3-r0:
|
||||
// lib/apk/db/installed
|
||||
// http://...
|
||||
// First line contains the version
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
if len(lines) == 0 {
|
||||
return "", fmt.Errorf("apk latestVersion %s: %w", pkg, snack.ErrNotFound)
|
||||
}
|
||||
// First line: "pkg-1.2.3-r0:"
|
||||
first := strings.TrimSuffix(strings.TrimSpace(lines[0]), ":")
|
||||
_, ver := splitNameVersion(first)
|
||||
if ver == "" {
|
||||
return "", fmt.Errorf("apk latestVersion %s: %w", pkg, snack.ErrNotFound)
|
||||
}
|
||||
return ver, nil
|
||||
}
|
||||
|
||||
func listUpgrades(ctx context.Context) ([]snack.Package, error) {
|
||||
c := exec.CommandContext(ctx, "apk", "upgrade", "--simulate")
|
||||
out, err := c.CombinedOutput()
|
||||
if err != nil {
|
||||
// If simulate fails, it could mean no upgrades or an error
|
||||
outStr := strings.TrimSpace(string(out))
|
||||
if outStr == "" || strings.Contains(outStr, "OK") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("apk listUpgrades: %w", err)
|
||||
}
|
||||
return parseUpgradeSimulation(string(out)), nil
|
||||
}
|
||||
|
||||
// parseUpgradeSimulation parses `apk upgrade --simulate` output.
|
||||
// Lines look like: "(1/3) Upgrading pkg (oldver -> newver)"
|
||||
func parseUpgradeSimulation(output string) []snack.Package {
|
||||
var pkgs []snack.Package
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if !strings.Contains(line, "Upgrading") {
|
||||
continue
|
||||
}
|
||||
// "(1/3) Upgrading pkg (oldver -> newver)"
|
||||
idx := strings.Index(line, "Upgrading ")
|
||||
if idx < 0 {
|
||||
continue
|
||||
}
|
||||
rest := line[idx+len("Upgrading "):]
|
||||
// "pkg (oldver -> newver)"
|
||||
parts := strings.SplitN(rest, " (", 2)
|
||||
if len(parts) < 1 {
|
||||
continue
|
||||
}
|
||||
name := strings.TrimSpace(parts[0])
|
||||
var ver string
|
||||
if len(parts) == 2 {
|
||||
// "oldver -> newver)"
|
||||
verPart := strings.TrimSuffix(parts[1], ")")
|
||||
arrow := strings.Split(verPart, " -> ")
|
||||
if len(arrow) == 2 {
|
||||
ver = strings.TrimSpace(arrow[1])
|
||||
}
|
||||
}
|
||||
pkgs = append(pkgs, snack.Package{
|
||||
Name: name,
|
||||
Version: ver,
|
||||
Installed: true,
|
||||
})
|
||||
}
|
||||
return pkgs
|
||||
}
|
||||
|
||||
func upgradeAvailable(ctx context.Context, pkg string) (bool, error) {
|
||||
upgrades, err := listUpgrades(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, u := range upgrades {
|
||||
if u.Name == pkg {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func versionCmp(ctx context.Context, ver1, ver2 string) (int, error) {
|
||||
c := exec.CommandContext(ctx, "apk", "version", "-t", ver1, ver2)
|
||||
out, err := c.CombinedOutput()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("apk versionCmp: %w", err)
|
||||
}
|
||||
result := strings.TrimSpace(string(out))
|
||||
switch result {
|
||||
case "<":
|
||||
return -1, nil
|
||||
case ">":
|
||||
return 1, nil
|
||||
case "=":
|
||||
return 0, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("apk versionCmp: unexpected output %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
// autoremove is a no-op for apk. Alpine's apk does not have a direct
|
||||
// autoremove equivalent. `apk fix` repairs packages but does not remove
|
||||
// unused dependencies.
|
||||
func autoremove(_ context.Context, _ ...snack.Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func clean(ctx context.Context) error {
|
||||
c := exec.CommandContext(ctx, "apk", "cache", "clean")
|
||||
out, err := c.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("apk clean: %s: %w", strings.TrimSpace(string(out)), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fileList(ctx context.Context, pkg string) ([]string, error) {
|
||||
c := exec.CommandContext(ctx, "apk", "info", "-L", pkg)
|
||||
out, err := c.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("apk fileList %s: %w", pkg, snack.ErrNotInstalled)
|
||||
}
|
||||
var files []string
|
||||
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
// First line is "pkg-version contains:" — skip it
|
||||
if strings.Contains(line, " contains:") {
|
||||
continue
|
||||
}
|
||||
files = append(files, line)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func owner(ctx context.Context, path string) (string, error) {
|
||||
c := exec.CommandContext(ctx, "apk", "info", "--who-owns", path)
|
||||
out, err := c.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("apk owner %s: %w", path, snack.ErrNotFound)
|
||||
}
|
||||
// Output: "/path is owned by pkg-version"
|
||||
outStr := strings.TrimSpace(string(out))
|
||||
if idx := strings.Index(outStr, "is owned by "); idx != -1 {
|
||||
nameVer := strings.TrimSpace(outStr[idx+len("is owned by "):])
|
||||
name, _ := splitNameVersion(nameVer)
|
||||
if name != "" {
|
||||
return name, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("apk owner %s: unexpected output %q", path, outStr)
|
||||
}
|
||||
41
apk/capabilities_other.go
Normal file
41
apk/capabilities_other.go
Normal file
@@ -0,0 +1,41 @@
|
||||
//go:build !linux
|
||||
|
||||
package apk
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogrlx/snack"
|
||||
)
|
||||
|
||||
func latestVersion(_ context.Context, _ string) (string, error) {
|
||||
return "", snack.ErrUnsupportedPlatform
|
||||
}
|
||||
|
||||
func listUpgrades(_ context.Context) ([]snack.Package, error) {
|
||||
return nil, snack.ErrUnsupportedPlatform
|
||||
}
|
||||
|
||||
func upgradeAvailable(_ context.Context, _ string) (bool, error) {
|
||||
return false, snack.ErrUnsupportedPlatform
|
||||
}
|
||||
|
||||
func versionCmp(_ context.Context, _, _ string) (int, error) {
|
||||
return 0, snack.ErrUnsupportedPlatform
|
||||
}
|
||||
|
||||
func autoremove(_ context.Context, _ ...snack.Option) error {
|
||||
return snack.ErrUnsupportedPlatform
|
||||
}
|
||||
|
||||
func clean(_ context.Context) error {
|
||||
return snack.ErrUnsupportedPlatform
|
||||
}
|
||||
|
||||
func fileList(_ context.Context, _ string) ([]string, error) {
|
||||
return nil, snack.ErrUnsupportedPlatform
|
||||
}
|
||||
|
||||
func owner(_ context.Context, _ string) (string, error) {
|
||||
return "", snack.ErrUnsupportedPlatform
|
||||
}
|
||||
Reference in New Issue
Block a user