Compare commits

...
22 Commits
Author SHA1 Message Date
AI BotandRiyyi 1159be59d0 Add safety check before package pruning
Verifies all state packages are installed locally before running
markAllAsDeps and orphan cleanup.
2026-05-04 23:28:58 +02:00
Riyyi 0fc0684801 Improve tarball selection when installing AUR packages 2026-05-04 22:47:07 +02:00
AI BotandRiyyi bcddfd13e7 Also install repo dependencies when installing AUR packages 2026-05-04 21:13:25 +02:00
Riyyi 4472bbc2e9 Add grill-me skill 2026-05-04 20:25:43 +02:00
Riyyi b371c74e9a Make tool request elevated privileges on-demand 2026-05-03 23:04:34 +02:00
Riyyi 76b83437ac Add safety check before rm -rf call 2026-05-03 22:17:51 +02:00
AI BotandRiyyi b04869f0e1 Move log to user owned directory 2026-05-03 21:29:49 +02:00
AI BotandRiyyi 7c4b0eda8b Apply coding conventions 2026-05-03 18:01:00 +02:00
AI BotandRiyyi 317d5f4be9 Cache sudo user to avoid repeated env lookups 2026-05-03 17:49:53 +02:00
AI BotandRiyyi b13c9da7a1 Improve AUR with AUR dependency installations 2026-05-03 17:40:31 +02:00
AI BotandRiyyi c8cdace4bf Update Go file structure conventions in AGENTS.md 2026-05-03 15:48:00 +02:00
AI BotandRiyyi 31fad42ab3 Reorder functions to follow public-first convention 2026-05-03 15:40:07 +02:00
AI BotandRiyyi db4e22a580 Add --prune flag for explicit marking and orphan cleanup 2026-05-03 15:10:54 +02:00
AI BotandRiyyi d59d5c084d Add implicit state file support
Read implicit package list from /home/rick/.config/declpac or ~/.config/declpac
2026-05-03 14:30:16 +02:00
AI BotandRiyyi c4746697b5 Remove merge package, move Merge to input 2026-05-03 14:30:16 +02:00
AI BotandRiyyi 5ad29767c9 Add safety check for package list validation 2026-05-03 14:30:16 +02:00
AI BotandRiyyi 38d26fd2d8 Add Go line length guidelines
Adds 120/100/80 character guidelines to AGENTS.md
2026-05-03 12:17:12 +02:00
AI BotandRiyyi 399a7157e2 Add tilde path expansion for state files 2026-05-03 11:48:05 +02:00
AI BotandRiyyi 578eb16ab2 Add Go file structure conventions to AGENTS.md 2026-05-03 11:27:30 +02:00
AI BotandRiyyi 584f4fbdd9 Skip debug packages when finding built AUR package
The findPKGFile function now takes pkgName as parameter to skip
debug packages (*-debug.pkg.tar.zst) when locating built packages.
2026-05-03 11:13:06 +02:00
Riyyi 39b8d9ac4b Add fmt command to makefile 2026-05-03 10:22:30 +02:00
AI BotandRiyyi d36bba6925 Add centralized command logging function
Refactors command execution to use a new log.Command helper that handles both logging and cmd creation, reducing duplication in sync.go
2026-05-03 10:05:10 +02:00
18 changed files with 765 additions and 297 deletions
+10
View File
@@ -0,0 +1,10 @@
---
name: grill-me
description: Interview the user relentlessly about a plan or design until reaching shared understanding, resolving each branch of the decision tree. Use when user wants to stress-test a plan, get grilled on their design, or mentions "grill me".
---
Interview me relentlessly about every aspect of this plan until we reach a shared understanding. Walk down each branch of the design tree, resolving dependencies between decisions one-by-one. For each question, provide your recommended answer.
Ask the questions one at a time.
If a question can be answered by exploring the codebase, explore the codebase instead.
+25 -1
View File
@@ -2,6 +2,30 @@
## Skills: Always Active
At the start of every conversation, load the following skills using the `skill` tool before responding to the user:
At the start of every conversation, load the following skills using the `skill`
tool before responding to the user:
1. **caveman** — Always use caveman mode (full intensity) for all responses
## Code Organization
### Go: File Structure
- Variables must appear at the top of each file.
- Types must appear after variables.
- Constructors must appear after types.
- Public (exported) functions must appear after constructors.
- Private (unexported) functions must appear at the bottom of each file.
- Within each section, definitions must be sorted alphabetically by name.
- The sections must be separated by exactly these dividers, filling in the section:
// -----------------------------------------
// <section>
### Go: Line Length
Keep Go lines as short as reasonably possible.
- **Hard limit:** 120 characters — never exceed this.
- **Preferred:** 100 characters or fewer in normal cases.
- **Target:** 80 characters if breaking the line produces cleaner, more readable
code (e.g. chained calls, long argument lists, multi-condition `if` statements).
+5 -2
View File
@@ -1,4 +1,4 @@
.PHONY: build fmt vet check githook install uninstall
.PHONY: build fmt fmtcheck vet check githook install uninstall
PKG := github.com/Riyyi/declpac
BIN := declpac
@@ -15,6 +15,9 @@ build:
go build -o $(BUILD) ./cmd/declpac
fmt:
@gofmt -w cmd pkg
fmtcheck:
@if [ -z "$(STAGED_GO)" ]; then exit 0; fi; \
unformatted=$$(gofmt -l $(STAGED_GO)); \
if [ -n "$$unformatted" ]; then \
@@ -26,7 +29,7 @@ vet:
@if [ -z "$(STAGED_GO)" ]; then exit 0; fi; \
go vet $(STAGED_PKGS)
check: fmt vet
check: fmtcheck vet
githook:
@mkdir -p .git/hooks
+19 -19
View File
@@ -5,8 +5,8 @@ Declarative package manager for Arch Linux that syncs your system with a declare
## Features
- Declarative state management — define your desired package list in files or stdin
- Smart orphan cleanup — removes packages no longer needed
- Explicit package tracking — marks declared packages as explicit
- Explicit package tracking — marks declared packages as explicit (with `--prune`)
- Smart orphan cleanup — removes packages no longer needed (with `--prune`)
- AUR support — builds and installs AUR packages automatically
- Machine-readable output — perfect for scripting
@@ -27,22 +27,22 @@ sudo mv declpac /usr/local/bin/
- pacman
- makepkg (for AUR support)
- git (for AUR support)
- Root privileges
- sudo/doas (root privileges)
## Usage
```bash
# Single state file
sudo declpac --state packages.txt
declpac --state packages.txt
# Multiple state files
sudo declpac --state base.txt --state apps.txt
declpac --state base.txt --state apps.txt
# From stdin
cat packages.txt | sudo declpac
cat packages.txt | declpac
# Preview changes without applying
sudo declpac --dry-run --state packages.txt
declpac --dry-run --state packages.txt
```
### State File Format
@@ -57,12 +57,19 @@ docker
# this is a comment
```
### Implicit State File
If `$XDG_CONFIG_HOME/declpac` (or `~/.config/declpac` on fallback) exists, its
contents are automatically included in the package list.
### Options
| Flag | Alias | Description |
|------|-------|-------------|
| `--state` | `-s` | State file to read package list from (can be used multiple times) |
| `--state` | `-s` | State file(s) to read package list from (can be used multiple times) |
| `--nocheck` | | Skip safety check (allow significant package count reductions) |
| `--dry-run` | | Preview changes without applying them |
| `--prune` | | Mark packages as explicit and cleanup orphans |
| `--verbose` | `-v` | Enable verbose output |
| `--help` | `-h` | Show help message |
@@ -73,8 +80,8 @@ docker
3. **Categorize** — Check if packages are in official repos or AUR
4. **Sync** — Install/update packages via pacman
5. **Build** — Build and install AUR packages via makepkg
6. **Mark** — Mark declared packages as explicit, all others as dependencies
7. **Cleanup** — Remove orphaned packages
6. **Mark** (with `--prune`) — Mark declared packages as explicit, all others as dependencies
7. **Cleanup** (with `--prune`) — Remove orphaned packages
### Database Freshness
@@ -82,17 +89,11 @@ If the pacman database is older than 24 hours, it is automatically refreshed.
### Logging
Operations are logged to `/var/log/declpac.log`.
Operation are logged to `$XDG_STATE_HOME/declpac.log`
(or `~/.local/state/declpac.log` on fallback)
## Troubleshooting
### Permission denied
Use sudo:
```bash
sudo declpac --state packages.txt
```
### Package not found
Check if the package exists:
@@ -114,7 +115,6 @@ declpac/
│ └── main.go # Entry point
├── pkg/
│ ├── input/ # State file/stdin reading
│ ├── merge/ # Package merging
│ ├── fetch/ # Package resolution
│ │ ├── aur/ # AUR support
│ │ └── alpm/ # ALPM support
+21 -4
View File
@@ -8,9 +8,9 @@ import (
"github.com/urfave/cli/v3"
"github.com/Riyyi/declpac/pkg/auth"
"github.com/Riyyi/declpac/pkg/input"
"github.com/Riyyi/declpac/pkg/log"
"github.com/Riyyi/declpac/pkg/merge"
"github.com/Riyyi/declpac/pkg/output"
"github.com/Riyyi/declpac/pkg/pacman"
"github.com/Riyyi/declpac/pkg/pacman/read"
@@ -18,8 +18,9 @@ import (
type Config struct {
StateFiles []string
NoConfirm bool
NoCheck bool
DryRun bool
Prune bool
Verbose bool
}
@@ -36,11 +37,21 @@ func main() {
Usage: "State file(s) to read package list from",
Destination: &cfg.StateFiles,
},
&cli.BoolFlag{
Name: "nocheck",
Usage: "Skip safety check",
Destination: &cfg.NoCheck,
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "Simulate the sync without making changes",
Destination: &cfg.DryRun,
},
&cli.BoolFlag{
Name: "prune",
Usage: "Mark packages and cleanup orphans",
Destination: &cfg.Prune,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
@@ -70,7 +81,11 @@ func run(cfg *Config) error {
}
log.Debug("run: packages read (%.2fs)", time.Since(start).Seconds())
merged := merge.Merge(packages)
merged, err := input.Merge(packages)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err
}
if cfg.DryRun {
result, err := read.DryRun(merged)
@@ -83,13 +98,15 @@ func run(cfg *Config) error {
return nil
}
auth.Start()
if err := log.OpenLog(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err
}
defer log.Close()
result, err := pacman.Sync(merged)
result, err := pacman.Sync(merged, cfg.NoCheck, cfg.Prune)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err
+104
View File
@@ -0,0 +1,104 @@
package auth
import (
"fmt"
"os/exec"
"regexp"
"strconv"
"time"
"github.com/Riyyi/declpac/pkg/log"
)
var tool string
var timeout time.Duration = 5 * time.Minute
var refreshCommand []string = []string{"-n", "true"}
// -----------------------------------------
// public
func Command(name string, args ...string) *exec.Cmd {
if tool == "" {
return log.Command(name, args...)
}
args = append([]string{name}, args...)
return log.Command(tool, args...)
}
func Run() {
exec.Command(tool, refreshCommand...).Run()
}
func Start() error {
err := detect()
if err != nil {
return err
}
// Automatically refresh privilege elevation to prevent user prompts
go func() {
for {
Run()
time.Sleep(timeout)
}
}()
return nil
}
// -----------------------------------------
// private
func detect() error {
tool = getTool()
if tool == "" {
return fmt.Errorf("no privilege elevation tool detected in PATH")
}
parseTimeout()
// We have to be a little faster than the actual timeout
timeout -= 30 * time.Second
return nil
}
func execLookPath(name string) string {
path, err := exec.LookPath(name)
if err != nil {
return ""
}
return path
}
func getTool() string {
sudo := execLookPath("sudo")
doas := execLookPath("doas")
if sudo != "" {
return "sudo"
}
if doas != "" {
return "doas"
}
return ""
}
func parseTimeout() {
switch tool {
case "sudo":
out, err := exec.Command("sudo", "sudo", "-V").CombinedOutput()
if err != nil {
return
}
re := regexp.MustCompile(`Authentication timestamp timeout: (\d+)\..*`)
matches := re.FindStringSubmatch(string(out))
if len(matches) == 2 {
if minutes, err := strconv.Atoi(matches[1]); err == nil {
timeout = time.Duration(minutes) * time.Minute
}
}
case "doas":
exec.Command("doas", "true").Run()
}
}
+63 -52
View File
@@ -19,6 +19,67 @@ type Handle struct {
syncDBs []dyalpm.Database
}
func (h *Handle) LocalPackages() (map[string]dyalpm.Package, error) {
start := time.Now()
log.Debug("LocalPackages: starting...")
localPkgs := make(map[string]dyalpm.Package)
err := h.localDB.PkgCache().ForEach(func(pkg dyalpm.Package) error {
localPkgs[pkg.Name()] = pkg
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to iterate local package cache: %w", err)
}
log.Debug("LocalPackages: done (%.2fs)", time.Since(start).Seconds())
return localPkgs, nil
}
func (h *Handle) FindProvidingPackage(depName string) (string, bool) {
pkg := h.handle.FindDBSatisfier(h.syncDBs, depName)
if pkg != nil {
return pkg.Name(), true
}
return "", false
}
func (h *Handle) Release() error {
if h.handle != nil {
h.handle.Release()
}
return nil
}
func (h *Handle) SyncPackages(pkgNames []string) (map[string]dyalpm.Package, error) {
start := time.Now()
log.Debug("SyncPackages: starting...")
syncPkgs := make(map[string]dyalpm.Package)
pkgSet := make(map[string]bool)
for _, name := range pkgNames {
pkgSet[name] = true
}
for _, db := range h.syncDBs {
err := db.PkgCache().ForEach(func(pkg dyalpm.Package) error {
if pkgSet[pkg.Name()] {
if _, exists := syncPkgs[pkg.Name()]; !exists {
syncPkgs[pkg.Name()] = pkg
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to iterate sync database %s: %w", db.Name(), err)
}
}
log.Debug("SyncPackages: done (%.2fs)", time.Since(start).Seconds())
return syncPkgs, nil
}
func New() (*Handle, error) {
start := time.Now()
log.Debug("alpm.New: starting...")
@@ -56,12 +117,8 @@ func New() (*Handle, error) {
}, nil
}
func (h *Handle) Release() error {
if h.handle != nil {
h.handle.Release()
}
return nil
}
// -----------------------------------------
// private
func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) {
log.Debug("registerSyncDBs: starting...")
@@ -89,49 +146,3 @@ func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) {
log.Debug("registerSyncDBs: done (%d dbs)", len(dbs))
return dbs, nil
}
func (h *Handle) LocalPackages() (map[string]dyalpm.Package, error) {
start := time.Now()
log.Debug("LocalPackages: starting...")
localPkgs := make(map[string]dyalpm.Package)
err := h.localDB.PkgCache().ForEach(func(pkg dyalpm.Package) error {
localPkgs[pkg.Name()] = pkg
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to iterate local package cache: %w", err)
}
log.Debug("LocalPackages: done (%.2fs)", time.Since(start).Seconds())
return localPkgs, nil
}
func (h *Handle) SyncPackages(pkgNames []string) (map[string]dyalpm.Package, error) {
start := time.Now()
log.Debug("SyncPackages: starting...")
syncPkgs := make(map[string]dyalpm.Package)
pkgSet := make(map[string]bool)
for _, name := range pkgNames {
pkgSet[name] = true
}
for _, db := range h.syncDBs {
err := db.PkgCache().ForEach(func(pkg dyalpm.Package) error {
if pkgSet[pkg.Name()] {
if _, exists := syncPkgs[pkg.Name()]; !exists {
syncPkgs[pkg.Name()] = pkg
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to iterate sync database %s: %w", db.Name(), err)
}
}
log.Debug("SyncPackages: done (%.2fs)", time.Since(start).Seconds())
return syncPkgs, nil
}
+10 -4
View File
@@ -13,10 +13,16 @@ import (
var AURInfoURL = "https://aur.archlinux.org/rpc?v=5&type=info"
type Package struct {
Name string `json:"Name"`
PackageBase string `json:"PackageBase"`
Version string `json:"Version"`
URL string `json:"URL"`
Name string `json:"Name"`
PackageBase string `json:"PackageBase"`
Version string `json:"Version"`
URL string `json:"URL"`
Depends []string `json:"Depends"`
MakeDepends []string `json:"MakeDepends"`
}
func (p Package) AllDepends() []string {
return append(p.Depends, p.MakeDepends...)
}
type Response struct {
+29 -7
View File
@@ -14,6 +14,7 @@ type PackageInfo struct {
InAUR bool
Exists bool
Installed bool
Provided string
AURInfo *aur.Package
}
@@ -22,6 +23,9 @@ type Fetcher struct {
aurClient *aur.Client
}
// -----------------------------------------
// constructor
func New() (*Fetcher, error) {
start := time.Now()
log.Debug("fetch.Fetcher New: starting...")
@@ -40,13 +44,8 @@ func New() (*Fetcher, error) {
}, nil
}
func (f *Fetcher) Close() error {
return f.alpmHandle.Release()
}
func (f *Fetcher) GetAURPackage(name string) (aur.Package, bool) {
return f.aurClient.Get(name)
}
// -----------------------------------------
// public
func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) {
localPkgs, err := f.alpmHandle.LocalPackages()
@@ -60,6 +59,22 @@ func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) {
return result, nil
}
func (f *Fetcher) Close() error {
return f.alpmHandle.Release()
}
func (f *Fetcher) FetchAur(packages []string) (map[string]aur.Package, error) {
return f.aurClient.Fetch(packages)
}
func (f *Fetcher) FindProvidingPackage(depName string) (string, bool) {
return f.alpmHandle.FindProvidingPackage(depName)
}
func (f *Fetcher) GetAURPackage(name string) (aur.Package, bool) {
return f.aurClient.Get(name)
}
func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
start := time.Now()
log.Debug("fetch.Resolve: starting...")
@@ -116,6 +131,13 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
continue
}
if providedBy, ok := f.FindProvidingPackage(pkg); ok {
log.Debug("fetch.Resolve: %s provided by %s", pkg, providedBy)
info.Provided = providedBy
info.Exists = true
continue
}
return nil, fmt.Errorf("package not found: %s", pkg)
}
}
+54 -9
View File
@@ -2,15 +2,43 @@ package input
import (
"bufio"
"errors"
"os"
"path/filepath"
"strings"
"github.com/Riyyi/declpac/pkg/lib"
)
var ErrEmptyList = errors.New("package list is empty")
// -----------------------------------------
// public
func Merge(packages map[string]bool) ([]string, error) {
result := make([]string, 0, len(packages))
for name := range packages {
result = append(result, name)
}
if len(result) == 0 {
return nil, ErrEmptyList
}
return result, nil
}
func ReadPackages(stateFiles []string) (map[string]bool, error) {
packages := make(map[string]bool)
for _, file := range stateFiles {
if err := readStateFile(file, packages); err != nil {
expanded := lib.ExpandPath(file)
if err := readStateFile(expanded, packages); err != nil {
return nil, err
}
}
implicitStateFile := getImplicitStateFile()
if fileExists(implicitStateFile) {
if err := readStateFile(implicitStateFile, packages); err != nil {
return nil, err
}
}
@@ -22,6 +50,31 @@ func ReadPackages(stateFiles []string) (map[string]bool, error) {
return packages, nil
}
// -----------------------------------------
// private
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func getImplicitStateFile() string {
cfgDir := os.Getenv("XDG_CONFIG_HOME")
if cfgDir == "" {
cfgDir = "~/.config"
}
cfgDir = lib.ExpandPath(cfgDir)
return filepath.Join(cfgDir, "declpac")
}
func normalizePackageName(name string) string {
name = strings.TrimSpace(name)
if name == "" || strings.HasPrefix(name, "#") {
return ""
}
return name
}
func readStateFile(path string, packages map[string]bool) error {
file, err := os.Open(path)
if err != nil {
@@ -60,11 +113,3 @@ func readStdin(packages map[string]bool) error {
return scanner.Err()
}
func normalizePackageName(name string) string {
name = strings.TrimSpace(name)
if name == "" || strings.HasPrefix(name, "#") {
return ""
}
return name
}
+21
View File
@@ -0,0 +1,21 @@
package lib
import (
"os"
"path/filepath"
"strings"
)
// -----------------------------------------
// public
func ExpandPath(path string) string {
if strings.HasPrefix(path, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return path
}
return filepath.Join(home, path[2:])
}
return path
}
+45 -30
View File
@@ -4,44 +4,19 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
// -----------------------------------------
"github.com/Riyyi/declpac/pkg/lib"
)
var logFile *os.File
var Verbose bool
// -----------------------------------------
func Debug(format string, args ...interface{}) {
if !Verbose {
return
}
fmt.Fprintf(os.Stderr, "[debug] "+format+"\n", args...)
}
// -----------------------------------------
func OpenLog() error {
logPath := filepath.Join("/var/log", "declpac.log")
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
logFile = f
writeTimestamp()
return nil
}
func GetLogWriter() io.Writer {
return logFile
}
func Write(msg []byte) {
logFile.Write(msg)
}
// public
func Close() error {
if logFile == nil {
@@ -50,7 +25,47 @@ func Close() error {
return logFile.Close()
}
func Command(name string, args ...string) *exec.Cmd {
cmdStr := name + " " + strings.Join(args, " ")
fmt.Fprintf(logFile, "[cmd] %s\n", cmdStr)
return exec.Command(name, args...)
}
func Debug(format string, args ...any) {
if !Verbose {
return
}
fmt.Fprintf(os.Stderr, "[debug] "+format+"\n", args...)
}
func GetLogWriter() io.Writer {
return logFile
}
func OpenLog() error {
stateDir := os.Getenv("XDG_STATE_HOME")
if stateDir == "" {
stateDir = "~/.local/state"
}
stateDir = lib.ExpandPath(stateDir)
logPath := filepath.Join(stateDir, "declpac.log")
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
logFile = f
writeTimestamp()
return nil
}
func Write(msg []byte) {
logFile.Write(msg)
}
// -----------------------------------------
// private
func writeTimestamp() {
ts := time.Now().Format("2006-01-02 15:04:05")
-9
View File
@@ -1,9 +0,0 @@
package merge
func Merge(packages map[string]bool) []string {
result := make([]string, 0, len(packages))
for name := range packages {
result = append(result, name)
}
return result
}
+6 -3
View File
@@ -12,15 +12,18 @@ type Result struct {
ToRemove []string
}
// -----------------------------------------
// public
func Format(r *Result) string {
var b strings.Builder
b.WriteString(fmt.Sprintf("Installed %d packages, removed %d packages", r.Installed, r.Removed))
b.WriteString(fmt.Sprintf("installed %d packages, removed %d packages", r.Installed, r.Removed))
if len(r.ToInstall) > 0 {
b.WriteString("\nWould install: ")
b.WriteString("\nwould install: ")
b.WriteString(strings.Join(r.ToInstall, ", "))
}
if len(r.ToRemove) > 0 {
b.WriteString("\nWould remove: ")
b.WriteString("\nwould remove: ")
b.WriteString(strings.Join(r.ToRemove, ", "))
}
return b.String()
+82 -31
View File
@@ -13,10 +13,21 @@ import (
"github.com/Riyyi/declpac/pkg/pacman/sync"
)
func Sync(packages []string) (*output.Result, error) {
func Sync(packages []string, noCheck bool, prune bool) (*output.Result, error) {
start := time.Now()
log.Debug("Sync: starting...")
explicitList, err := read.ExplicitList()
if err != nil {
return nil, err
}
explicitCount := len(explicitList)
if !noCheck && len(packages) < explicitCount/2 {
errMsg := "safety check: state packages (%d) less than half of explicitly installed (%d), override with --nocheck"
return nil, fmt.Errorf(errMsg, len(packages), explicitCount)
}
list, err := read.List()
if err != nil {
return nil, err
@@ -65,25 +76,36 @@ func Sync(packages []string) (*output.Result, error) {
if !ok {
return nil, fmt.Errorf("AUR package not found in cache: %s", pkg)
}
if err := sync.InstallAUR(pkg, aurInfo.PackageBase, log.GetLogWriter()); err != nil {
if err := sync.InstallAUR(f, pkg, aurInfo.PackageBase, false, log.GetLogWriter()); err != nil {
return nil, err
}
log.Debug("Sync: AUR package %s installed (%.2fs)", pkg, time.Since(start).Seconds())
}
log.Debug("Sync: marking all as deps...")
markAllAsDeps()
log.Debug("Sync: all marked as deps (%.2fs)", time.Since(start).Seconds())
var removed int
if prune {
log.Debug("Sync: running prune sanity check...")
if err := pruneSanityCheck(packages); err != nil {
return nil, err
}
log.Debug("Sync: prune sanity check passed (%.2fs)", time.Since(start).Seconds())
log.Debug("Sync: marking state packages as explicit...")
if err := sync.MarkAs(packages, "explicit", log.GetLogWriter()); err != nil {
fmt.Fprintf(os.Stderr, "warning: could not mark state packages as explicit: %v\n", err)
}
log.Debug("Sync: state packages marked as explicit (%.2fs)", time.Since(start).Seconds())
log.Debug("Sync: marking all as deps...")
if err := markAllAsDeps(); err != nil {
return nil, err
}
log.Debug("Sync: all marked as deps (%.2fs)", time.Since(start).Seconds())
removed, err := cleanupOrphans()
if err != nil {
return nil, err
log.Debug("Sync: marking state packages as explicit...")
if err := sync.MarkAs(packages, "explicit", log.GetLogWriter()); err != nil {
fmt.Fprintf(os.Stderr, "warning: could not mark state packages as explicit: %v\n", err)
}
log.Debug("Sync: state packages marked as explicit (%.2fs)", time.Since(start).Seconds())
removed, err = cleanupOrphans()
if err != nil {
return nil, err
}
}
list, _ = read.List()
@@ -101,6 +123,9 @@ func Sync(packages []string) (*output.Result, error) {
}, nil
}
// -----------------------------------------
// private
func categorizePackages(f *fetch.Fetcher, packages []string) (pacmanPkgs, aurPkgs []string, err error) {
start := time.Now()
log.Debug("categorizePackages: starting...")
@@ -127,24 +152,6 @@ func categorizePackages(f *fetch.Fetcher, packages []string) (pacmanPkgs, aurPkg
return pacmanPkgs, aurPkgs, nil
}
func markAllAsDeps() error {
start := time.Now()
log.Debug("markAllAsDeps: starting...")
packages, err := read.List()
if err != nil || len(packages) == 0 {
return fmt.Errorf("failed to list packages: %w", err)
}
if err := sync.MarkAs(packages, "deps", log.GetLogWriter()); err != nil {
log.Write([]byte(fmt.Sprintf("error: %v\n", err)))
return err
}
log.Debug("markAllAsDeps: done (%.2fs)", time.Since(start).Seconds())
return nil
}
func cleanupOrphans() (int, error) {
start := time.Now()
log.Debug("cleanupOrphans: starting...")
@@ -164,3 +171,47 @@ func cleanupOrphans() (int, error) {
log.Debug("cleanupOrphans: done (%.2fs)", time.Since(start).Seconds())
return removed, nil
}
func markAllAsDeps() error {
start := time.Now()
log.Debug("markAllAsDeps: starting...")
packages, err := read.List()
if err != nil || len(packages) == 0 {
return fmt.Errorf("failed to list packages: %w", err)
}
if err := sync.MarkAs(packages, "deps", log.GetLogWriter()); err != nil {
log.Write([]byte(fmt.Sprintf("error: %v\n", err)))
return err
}
log.Debug("markAllAsDeps: done (%.2fs)", time.Since(start).Seconds())
return nil
}
// pruneSanityCheck checks if the installation of all state packages succeeded,
// before attempting to do package marking and orphan cleanup.
func pruneSanityCheck(statePackages []string) error {
start := time.Now()
log.Debug("pruneSanityCheck: starting...")
localPackages, err := read.List()
if err != nil {
return fmt.Errorf("failed to list local packages: %w", err)
}
var missing []string
for _, pkg := range statePackages {
if !slices.Contains(localPackages, pkg) {
missing = append(missing, pkg)
}
}
if len(missing) > 0 {
return fmt.Errorf("safety check: missing state packages: %v", missing)
}
log.Debug("pruneSanityCheck: done (%.2fs)", time.Since(start).Seconds())
return nil
}
+64 -46
View File
@@ -16,52 +16,6 @@ import (
var LockFile = "/var/lib/pacman/db.lock"
func List() ([]string, error) {
start := time.Now()
log.Debug("List: starting...")
cmd := exec.Command("pacman", "-Qq")
output, err := cmd.Output()
if err != nil {
return nil, err
}
list := strings.Split(strings.TrimSpace(string(output)), "\n")
if list[0] == "" {
list = nil
}
log.Debug("List: done (%.2fs)", time.Since(start).Seconds())
return list, nil
}
func ListOrphans() ([]string, error) {
start := time.Now()
log.Debug("ListOrphans: starting...")
cmd := exec.Command("pacman", "-Qdtq")
var stderr bytes.Buffer
cmd.Stderr = &stderr
output, err := cmd.Output()
if err != nil {
// pacman -Qdtq exits 1 when there are no orphans, this isnt an error
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 && stderr.Len() == 0 {
return nil, nil
}
return nil, err
}
orphans := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(orphans) > 0 && orphans[0] == "" {
orphans = orphans[1:]
}
log.Debug("ListOrphans: done (%.2fs)", time.Since(start).Seconds())
return orphans, nil
}
func DBFreshness() (bool, error) {
info, err := os.Stat(LockFile)
if err != nil {
@@ -129,3 +83,67 @@ func DryRun(packages []string) (*output.Result, error) {
ToRemove: toRemove,
}, nil
}
func ExplicitList() ([]string, error) {
start := time.Now()
log.Debug("ExplicitList: starting...")
cmd := exec.Command("pacman", "-Qqe")
output, err := cmd.Output()
if err != nil {
return nil, err
}
list := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(list) > 0 && list[0] == "" {
list = nil
}
log.Debug("ExplicitList: done (%.2fs)", time.Since(start).Seconds())
return list, nil
}
func List() ([]string, error) {
start := time.Now()
log.Debug("List: starting...")
cmd := exec.Command("pacman", "-Qq")
output, err := cmd.Output()
if err != nil {
return nil, err
}
list := strings.Split(strings.TrimSpace(string(output)), "\n")
if list[0] == "" {
list = nil
}
log.Debug("List: done (%.2fs)", time.Since(start).Seconds())
return list, nil
}
func ListOrphans() ([]string, error) {
start := time.Now()
log.Debug("ListOrphans: starting...")
cmd := exec.Command("pacman", "-Qdtq")
var stderr bytes.Buffer
cmd.Stderr = &stderr
output, err := cmd.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 && stderr.Len() == 0 {
return nil, nil
}
return nil, err
}
orphans := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(orphans) > 0 && orphans[0] == "" {
orphans = orphans[1:]
}
log.Debug("ListOrphans: done (%.2fs)", time.Since(start).Seconds())
return orphans, nil
}
+201 -80
View File
@@ -4,10 +4,13 @@ import (
"fmt"
"io"
"os"
"os/exec"
"os/user"
"strings"
"time"
"github.com/Riyyi/declpac/pkg/auth"
"github.com/Riyyi/declpac/pkg/fetch"
"github.com/Riyyi/declpac/pkg/fetch/aur"
"github.com/Riyyi/declpac/pkg/log"
)
@@ -16,46 +19,48 @@ type Result struct {
Removed int
}
func SyncPackages(packages []string, logWriter io.Writer) error {
// -----------------------------------------
// public
func InstallAUR(f *fetch.Fetcher, pkgName string, packageBase string, asDeps bool, logWriter io.Writer) error {
start := time.Now()
log.Debug("SyncPackages: starting...")
log.Debug("InstallAUR: starting...")
if logWriter == nil {
logWriter = os.Stderr
}
args := append([]string{"-S", "--needed", "--noconfirm"}, packages...)
cmdStr := "pacman " + strings.Join(args, " ")
fmt.Fprintf(logWriter, "[cmd] %s\n", cmdStr)
cmd := exec.Command("pacman", args...)
cmd.Stdout = logWriter
cmd.Stderr = logWriter
err := cmd.Run()
aurInfo := getAURInfo(f, pkgName, packageBase)
if err := resolveAndInstallDeps(f, aurInfo, logWriter); err != nil {
return err
}
tmpDir := getTempDirName() + "/" + pkgName
if err := createTempDir(tmpDir); err != nil {
return err
}
defer os.RemoveAll(tmpDir)
if err := cloneRepo(packageBase, tmpDir, logWriter); err != nil {
return err
}
log.Debug("InstallAUR: cloned (%.2fs)", time.Since(start).Seconds())
if err := buildPackage(tmpDir, asDeps, logWriter); err != nil {
return err
}
log.Debug("InstallAUR: built (%.2fs)", time.Since(start).Seconds())
pkgFile, err := findPKGFile(pkgName, tmpDir)
if err != nil {
return fmt.Errorf("pacman sync failed: %w", err)
return fmt.Errorf("failed to find built package: %w", err)
}
log.Debug("SyncPackages: done (%.2fs)", time.Since(start).Seconds())
return nil
}
func RefreshDB(logWriter io.Writer) error {
start := time.Now()
log.Debug("RefreshDB: starting...")
if logWriter == nil {
logWriter = os.Stderr
if err := installBuiltPackage(pkgFile, logWriter); err != nil {
return err
}
log.Debug("InstallAUR: done (%.2fs)", time.Since(start).Seconds())
fmt.Fprintf(logWriter, "[cmd] pacman -Syy\n")
cmd := exec.Command("pacman", "-Syy")
cmd.Stdout = logWriter
cmd.Stderr = logWriter
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to refresh pacman database: %w", err)
}
log.Debug("RefreshDB: done (%.2fs)", time.Since(start).Seconds())
return nil
}
@@ -72,9 +77,7 @@ func MarkAs(packages []string, flag string, logWriter io.Writer) error {
}
args := append([]string{"-D", "--" + flagName}, packages...)
cmdStr := "pacman " + strings.Join(args, " ")
fmt.Fprintf(logWriter, "[cmd] %s\n", cmdStr)
cmd := exec.Command("pacman", args...)
cmd := auth.Command("pacman", args...)
cmd.Stdout = logWriter
cmd.Stderr = logWriter
err := cmd.Run()
@@ -86,6 +89,25 @@ func MarkAs(packages []string, flag string, logWriter io.Writer) error {
return nil
}
func RefreshDB(logWriter io.Writer) error {
start := time.Now()
log.Debug("RefreshDB: starting...")
if logWriter == nil {
logWriter = os.Stderr
}
cmd := auth.Command("pacman", "-Syy")
cmd.Stdout = logWriter
cmd.Stderr = logWriter
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to refresh pacman database: %w", err)
}
log.Debug("RefreshDB: done (%.2fs)", time.Since(start).Seconds())
return nil
}
func RemoveOrphans(orphans []string, logWriter io.Writer) (int, error) {
start := time.Now()
log.Debug("RemoveOrphans: starting...")
@@ -102,9 +124,7 @@ func RemoveOrphans(orphans []string, logWriter io.Writer) (int, error) {
args := make([]string, 0, 3+len(orphans))
args = append(args, "pacman", "-Rns", "--noconfirm")
args = append(args, orphans...)
cmdStr := strings.Join(args, " ")
fmt.Fprintf(logWriter, "[cmd] %s\n", cmdStr)
removeCmd := exec.Command(args[0], args[1:]...)
removeCmd := auth.Command(args[0], args[1:]...)
removeCmd.Stdout = logWriter
removeCmd.Stderr = logWriter
err := removeCmd.Run()
@@ -118,80 +138,181 @@ func RemoveOrphans(orphans []string, logWriter io.Writer) (int, error) {
return count, nil
}
func InstallAUR(pkgName string, packageBase string, logWriter io.Writer) error {
func SyncPackages(packages []string, logWriter io.Writer) error {
start := time.Now()
log.Debug("InstallAUR: starting...")
log.Debug("SyncPackages: starting...")
if logWriter == nil {
logWriter = os.Stderr
}
sudoUser := os.Getenv("SUDO_USER")
if sudoUser == "" {
sudoUser = os.Getenv("USER")
if sudoUser == "" {
sudoUser = "root"
}
args := append([]string{"-S", "--needed", "--noconfirm"}, packages...)
cmd := auth.Command("pacman", args...)
cmd.Stdout = logWriter
cmd.Stderr = logWriter
err := cmd.Run()
if err != nil {
return fmt.Errorf("pacman sync failed: %w", err)
}
tmpDir := "/tmp/declpac-aur-" + pkgName
mkdirCmdStr := "su - " + sudoUser + " -c 'rm -rf " + tmpDir + " && mkdir -p " + tmpDir + "'"
fmt.Fprintf(logWriter, "[cmd] %s\n", mkdirCmdStr)
mkdirCmd := exec.Command("su", "-", sudoUser, "-c", "rm -rf "+tmpDir+" && mkdir -p "+tmpDir)
if err := mkdirCmd.Run(); err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
defer os.RemoveAll(tmpDir)
log.Debug("SyncPackages: done (%.2fs)", time.Since(start).Seconds())
return nil
}
cloneURL := "https://aur.archlinux.org/" + packageBase + ".git"
cloneCmdStr := "su - " + sudoUser + " -c 'git clone " + cloneURL + " " + tmpDir + "'"
fmt.Fprintf(logWriter, "[cmd] %s\n", cloneCmdStr)
cloneCmd := exec.Command("su", "-", sudoUser, "-c", "git clone "+cloneURL+" "+tmpDir)
cloneCmd.Stdout = logWriter
cloneCmd.Stderr = logWriter
if err := cloneCmd.Run(); err != nil {
return fmt.Errorf("failed to clone AUR repo: %w", err)
}
log.Debug("InstallAUR: cloned (%.2fs)", time.Since(start).Seconds())
// -----------------------------------------
// private
makepkgCmdStr := "su - " + sudoUser + " -c 'cd " + tmpDir + " && makepkg -s --noconfirm'"
fmt.Fprintf(logWriter, "[cmd] %s\n", makepkgCmdStr)
makepkgCmd := exec.Command("su", "-", sudoUser, "-c", "cd "+tmpDir+" && makepkg -s --noconfirm")
func buildPackage(tmpDir string, asDeps bool, logWriter io.Writer) error {
makepkgArgs := []string{"-D", tmpDir, "-s", "--noconfirm"}
if asDeps {
makepkgArgs = append(makepkgArgs, "--asdeps")
}
makepkgCmd := log.Command("makepkg", makepkgArgs...)
makepkgCmd.Stdout = logWriter
makepkgCmd.Stderr = logWriter
if err := makepkgCmd.Run(); err != nil {
return fmt.Errorf("makepkg failed to build AUR package: %w", err)
}
log.Debug("InstallAUR: built (%.2fs)", time.Since(start).Seconds())
return nil
}
pkgFile, err := findPKGFile(tmpDir)
if err != nil {
return fmt.Errorf("failed to find built package: %w", err)
func cloneRepo(packageBase string, tmpDir string, logWriter io.Writer) error {
cloneURL := "https://aur.archlinux.org/" + packageBase + ".git"
cloneCmd := log.Command("git", "clone", cloneURL, tmpDir)
cloneCmd.Stdout = logWriter
cloneCmd.Stderr = logWriter
if err := cloneCmd.Run(); err != nil {
return fmt.Errorf("failed to clone AUR repo: %w", err)
}
return nil
}
func createTempDir(tmpDir string) error {
if tmpDir == "" || tmpDir == "/" || !strings.HasPrefix(tmpDir, "/tmp") {
return fmt.Errorf("safety check: prevented malformed rm -rf call")
}
installCmdStr := "pacman -U --noconfirm " + pkgFile
fmt.Fprintf(logWriter, "[cmd] %s\n", installCmdStr)
installCmd := exec.Command("pacman", "-U", "--noconfirm", pkgFile)
installCmd.Stdout = logWriter
installCmd.Stderr = logWriter
if err := installCmd.Run(); err != nil {
return fmt.Errorf("failed to install package: %w", err)
rmdirCmd := log.Command("rm", "-rf", tmpDir)
if err := rmdirCmd.Run(); err != nil {
return fmt.Errorf("failed to remove temp directory: %w", err)
}
mkdirCmd := log.Command("mkdir", "-p", tmpDir)
if err := mkdirCmd.Run(); err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
log.Debug("InstallAUR: done (%.2fs)", time.Since(start).Seconds())
return nil
}
func findPKGFile(dir string) (string, error) {
func findPKGFile(pkgName string, dir string) (string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return "", err
}
for _, entry := range entries {
name := entry.Name()
if strings.HasSuffix(name, ".pkg.tar.zst") || strings.HasSuffix(name, ".pkg.tar.gz") {
return strings.Join([]string{dir, name}, "/"), nil
if !strings.HasSuffix(name, ".pkg.tar.zst") && !strings.HasSuffix(name, ".pkg.tar.gz") {
continue
}
// Skip packages that do not start with the exact package name, ex: sunshine-bin -> sunshine
if !strings.HasPrefix(name, pkgName) {
continue
}
// Skip packages that provide a debug package, ex: sunshine-bin -> sunshine-debug
if strings.HasPrefix(name, pkgName+"-debug") {
continue
}
return strings.Join([]string{dir, name}, "/"), nil
}
return "", fmt.Errorf("no package file found in %s", dir)
}
func getAURInfo(f *fetch.Fetcher, pkgName string, packageBase string) *aur.Package {
if packageBase == "" {
return nil
}
info, ok := f.GetAURPackage(pkgName)
if !ok {
return nil
}
return &info
}
func getTempDirName() string {
user, err := user.Current()
if err != nil {
return "/tmp/declpac"
}
return "/tmp/declpac-" + user.Username
}
func installBuiltPackage(pkgFile string, logWriter io.Writer) error {
installCmd := auth.Command("pacman", "-U", "--noconfirm", pkgFile)
installCmd.Stdout = logWriter
installCmd.Stderr = logWriter
if err := installCmd.Run(); err != nil {
return fmt.Errorf("failed to install package: %w", err)
}
return nil
}
func resolveAndInstallDeps(f *fetch.Fetcher, aurInfo *aur.Package, logWriter io.Writer) error {
if aurInfo == nil {
return nil
}
depends := aurInfo.AllDepends()
if len(depends) == 0 {
return nil
}
resolved, err := f.Resolve(depends)
if err != nil {
return fmt.Errorf("failed to resolve dependencies: %w", err)
}
var repoDeps, aurDeps []string
for _, dep := range depends {
info := resolved[dep]
if info.Installed {
continue
}
pkg := dep
if info.Provided != "" {
pkg = info.Provided
}
if info.Exists {
repoDeps = append(repoDeps, pkg)
} else if info.InAUR {
aurDeps = append(aurDeps, pkg)
}
}
if len(repoDeps) > 0 {
if err := SyncPackages(repoDeps, logWriter); err != nil {
return fmt.Errorf("failed to install repo dependencies: %w", err)
}
}
if len(aurDeps) == 0 {
return nil
}
fetched, err := f.FetchAur(aurDeps)
if err != nil {
log.Debug("sync.resolveAndInstallDeps: aur fetch error: %v", err)
}
for _, dep := range aurDeps {
depInfo, ok := fetched[dep]
if !ok {
continue
}
if err := InstallAUR(f, dep, depInfo.PackageBase, true, logWriter); err != nil {
return err
}
}
return nil
}
+6
View File
@@ -5,6 +5,12 @@
"source": "JuliusBrussee/caveman",
"sourceType": "github",
"computedHash": "a818cdc41dcfaa50dd891c5cb5e5705968338de02e7e37949ca56e8c30ad4176"
},
"grill-me": {
"source": "mattpocock/skills",
"sourceType": "github",
"skillPath": "skills/productivity/grill-me/SKILL.md",
"computedHash": "784f0dbb7403b0f00324bce9a112f715342777a0daee7bbb7385f9c6f0a170ea"
}
}
}