diff --git a/apk/capabilities_linux.go b/apk/capabilities_linux.go index 3d9cc23..2dbdc2e 100644 --- a/apk/capabilities_linux.go +++ b/apk/capabilities_linux.go @@ -12,24 +12,18 @@ import ( ) func latestVersion(ctx context.Context, pkg string) (string, error) { - // Use `apk policy` to get available versions - c := exec.CommandContext(ctx, "apk", "policy", pkg) + // Use `apk search -e` for exact match, output is "pkg-version" + c := exec.CommandContext(ctx, "apk", "search", "-e", 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 { + if len(lines) == 0 || 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) + // Output: "pkg-1.2.3-r0" + _, ver := splitNameVersion(lines[0]) if ver == "" { return "", fmt.Errorf("apk latestVersion %s: %w", pkg, snack.ErrNotFound) } diff --git a/dnf/capabilities_linux.go b/dnf/capabilities_linux.go index 1974751..de9e2ea 100644 --- a/dnf/capabilities_linux.go +++ b/dnf/capabilities_linux.go @@ -15,7 +15,8 @@ import ( ) func latestVersion(ctx context.Context, pkg string) (string, error) { - out, err := run(ctx, []string{"info", "--available", pkg}, snack.Options{}) + // Try "dnf info " which shows both installed and available + out, err := run(ctx, []string{"info", pkg}, snack.Options{}) if err != nil { if strings.Contains(err.Error(), "exit status 1") { return "", fmt.Errorf("dnf latestVersion %s: %w", pkg, snack.ErrNotFound)