Compare commits

...
41 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
AI BotandRiyyi b1a376dbbe Add build, install and uninstall targets to Makefile 2026-04-24 21:14:03 +02:00
Riyyi 8a074e02b0 Update README.md 2026-04-19 11:20:08 +02:00
AI BotandRiyyi fdd9ecb3a5 Handle pacman -Qdtq exit code 1 as non-error
pacman -Qdtq exits with code 1 when there are no orphans, which is not an error condition. Handle this gracefully by returning nil instead of an error.
2026-04-19 11:08:32 +02:00
AI BotandRiyyi c90d2e8916 Add verbose flag for debug output control
Introduces log.Debug() function that respects a Verbose config flag, replacing direct [debug] fmt.Fprintf calls throughout the codebase. This allows debug logging to be toggled via -v/--verbose CLI flag.
2026-04-19 11:06:52 +02:00
Riyyi 1dffd70db2 Skip interactive prompt when removing orphans 2026-04-19 10:21:57 +02:00
AI BotandRiyyi 5ebb958bf1 Add command logging and skip already installed AUR packages 2026-04-19 10:15:00 +02:00
Riyyi c7bed95107 Skip confirmation when installing packages 2026-04-18 20:35:18 +02:00
AI BotandRiyyi a934dfa1f0 Update make-commit skill with message format rules 2026-04-18 18:50:25 +02:00
AI BotandRiyyi e64382c1bd Fix issues with dry-run package reporting
- Filter orphans to only those not in requested packages
- Fix Removed count to reflect only actual removals
2026-04-18 18:30:45 +02:00
AI BotandRiyyi 83de6acc74 Refactor fetch and pacman packages into subpackages
Split fetch into alpm and aur subpackages for better organization.
Rename state to log. Split pacman into read and sync subpackages.
Remove validation in favor of read.DBFreshness.
2026-04-18 18:18:23 +02:00
Riyyi 88ff093365 Extract PacmanState constant 2026-04-18 16:16:13 +02:00
Riyyi c2a7b9ab9c Improve make-commit skill commit message formatting
- Fix typo 'thed' to 'the'
- Add 72-character title convention
- Change --stat flag formatting to be more standard
2026-04-18 15:39:43 +02:00
AI BotandRiyyi 86c6d078d6 Output actual commands run instead of generic progress messages 2026-04-18 15:35:22 +02:00
AI BotandRiyyi bd1108a93f Dont use CombinedOutput() to capture error separately 2026-04-18 15:07:56 +02:00
AI BotandRiyyi 82aeceacac Fixup: correct log path in add-operation-logging, fix pacman -Rns flag in declpac-cli-tool, update pkg path in fix-sync-dbs-notloaded 2026-04-18 14:27:47 +02:00
AI BotandRiyyi 86c1abaad4 Add opsx-fixup command for diverging change specs 2026-04-18 14:07:47 +02:00
AI BotandRiyyi 2b0cb39746 Update operation logging spec to use single timestamp per tool call 2026-04-18 13:44:48 +02:00
Riyyi fc18e27c25 Log timestamp once per tool invocation 2026-04-18 13:37:50 +02:00
AI BotandRiyyi 8348cc21df Suppress command output from stdout, only log to file 2026-04-17 23:34:53 +02:00
28 changed files with 1374 additions and 779 deletions
+1 -1
View File
@@ -32,4 +32,4 @@ go.work.sum
# .vscode/ # .vscode/
# Binary output # Binary output
/declpac bin/
+28
View File
@@ -0,0 +1,28 @@
---
description: Fixup diverging archived openspec change artifacts
---
Fixup archived openspec change artifacts in @openspec/changes/archive/ that have diverged from the codebase.
For each archived change directory:
1. Read the .md artifact
2. Review related code to verify consistency
3. Fix any mismatches (status, fields, missing links, etc.)
Only run make-commit if changes were made, with a specific message like "Fixup: add missing X field to Y change".
---
**Steps**
1. **For each archived openspec change**
Read the change artifact.
Go through the related code.
Correct the change artifact where needed.
2. **Run make-commit if changes were made**
skill [name=make-commit] message: "Fixup: <specific fix>"
+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.
+51 -24
View File
@@ -7,13 +7,26 @@ description: >
license: GPL-3.0 license: GPL-3.0
metadata: metadata:
author: riyyi author: riyyi
version: "1.0" version: "1.1"
--- ---
Make a git commit, distinguishing between user and AI contributions. Make a git commit, distinguishing between user and AI contributions.
--- ---
**Commit message format**
A valid commit message consists of a required **title** and an optional **body**:
Rules:
- **Title** (required): max 72 characters, starts with a capital letter —
unless referring to a tool/project that explicitly uses lowercase (e.g.,
"go", "npm", "rustc"). No trailing period.
- **Body** (optional): any further elaboration. Each line max 72 characters.
Wrap manually — do not rely on the terminal to wrap.
---
**Steps** **Steps**
1. **[REQUIRED] Ask user if this commit is by them or by AI** 1. **[REQUIRED] Ask user if this commit is by them or by AI**
@@ -25,49 +38,63 @@ Make a git commit, distinguishing between user and AI contributions.
- "By me" - User made the commit - "By me" - User made the commit
- "By AI" - AI made the commit - "By AI" - AI made the commit
2. **Check for commit message** 2. **Compose the commit message**
**Capitalization rule**: Commit messages should start with a capital letter, If the user did NOT provide a commit message, generate one from staged
unless it refers to a tool or project that explicitly uses lowercase as its changes:
name (e.g., "go", "npm", "rustc").
If the user did NOT provide a commit message, generate one from staged changes:
```bash ```bash
git diff --staged --stat git --no-pager diff --staged
``` ```
Create a reasonable commit message based on the changes. Write a commit message following the format above.
If thed user DID provide a message, format it into a proper commit message. If the user **DID** provide a message, treat it as raw input and apply the
format rules to it.
3. **Show commit message and confirm** 3. **Validate the commit message**
Before presenting the message to the user, check it against every rule:
- [ ] Title is present and non-empty
- [ ] Title is at most 72 characters
- [ ] Title starts with a capital letter (or an intentionally lowercase name)
- [ ] Title has no trailing period
- [ ] Every line in the body is at most 72 characters
Fix any violations silently before showing the message to the user.
4. **Show commit message and confirm**
Use the **question tool** to ask: Use the **question tool** to ask:
> "Is this commit message okay, or would you like to make tweaks?" > "Is this commit message okay, or would you like to make tweaks?"
> ```
> <message> > <message>
> ```
Options: Options:
- "Looks good" - Proceed with this message - "Looks good" - Proceed with this message
- "Make tweaks" - User will provide a new message - "Make tweaks" - User will provide a new message or describe changes
**If user wants tweaks**: Ask them for the new commit message. **If user wants tweaks**: apply the same validation (step 3) to the revised
message before committing.
4. **Make the commit** 5. **Make the commit**
Use the commit message provided by the user. For a title-only message:
**If by user:**
```bash ```bash
git commit -m "<message>" git commit -m "<title>"
``` ```
(Uses git config user as both committer and author)
**If by AI:** For a message with a body, pass `-m` twice (git inserts the blank line):
```bash ```bash
git commit -m "<message>" --author="AI Bot <ai@local>" git commit -m "<title>" -m "<body>"
```
Append `--author="AI Bot <ai@local>"` when the commit is by AI:
```bash
git commit -m "<title>" [-m "<body>"] --author="AI Bot <ai@local>"
``` ```
(Uses git config for committer, but sets author to AI Bot)
**Output** **Output**
- Tell user the commit was made - Tell user the commit was made.
- If AI commit, mention that the author was set to "AI Bot <ai@local>" - If AI commit, mention that the author was set to "AI Bot <ai@local>".
+25 -1
View File
@@ -2,6 +2,30 @@
## Skills: Always Active ## 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 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).
+23 -2
View File
@@ -1,9 +1,23 @@
.PHONY: fmt vet check githook .PHONY: build fmt fmtcheck vet check githook install uninstall
PKG := github.com/Riyyi/declpac
BIN := declpac
BUILD_DIR := bin
BUILD := $(BUILD_DIR)/$(BIN)
PREFIX ?= /usr/local
DESTDIR ?=
STAGED_GO := $(shell git diff --cached --name-only --diff-filter=ACM | grep '\.go$$') STAGED_GO := $(shell git diff --cached --name-only --diff-filter=ACM | grep '\.go$$')
STAGED_PKGS := $(shell echo "$(STAGED_GO)" | tr ' ' '\n' | xargs -I{} dirname {} | sort -u | sed 's|^|./|') STAGED_PKGS := $(shell echo "$(STAGED_GO)" | tr ' ' '\n' | xargs -I{} dirname {} | sort -u | sed 's|^|./|')
build:
@mkdir -p $(BUILD_DIR)
go build -o $(BUILD) ./cmd/declpac
fmt: fmt:
@gofmt -w cmd pkg
fmtcheck:
@if [ -z "$(STAGED_GO)" ]; then exit 0; fi; \ @if [ -z "$(STAGED_GO)" ]; then exit 0; fi; \
unformatted=$$(gofmt -l $(STAGED_GO)); \ unformatted=$$(gofmt -l $(STAGED_GO)); \
if [ -n "$$unformatted" ]; then \ if [ -n "$$unformatted" ]; then \
@@ -15,7 +29,7 @@ vet:
@if [ -z "$(STAGED_GO)" ]; then exit 0; fi; \ @if [ -z "$(STAGED_GO)" ]; then exit 0; fi; \
go vet $(STAGED_PKGS) go vet $(STAGED_PKGS)
check: fmt vet check: fmtcheck vet
githook: githook:
@mkdir -p .git/hooks @mkdir -p .git/hooks
@@ -23,3 +37,10 @@ githook:
@echo 'make check' >> .git/hooks/pre-commit @echo 'make check' >> .git/hooks/pre-commit
@chmod +x .git/hooks/pre-commit @chmod +x .git/hooks/pre-commit
@echo "Installed pre-commit hook: .git/hooks/pre-commit" @echo "Installed pre-commit hook: .git/hooks/pre-commit"
install: build
@mkdir -p $(DESTDIR)$(PREFIX)/bin
@install -m 0755 $(BUILD) $(DESTDIR)$(PREFIX)/bin/$(BIN)
uninstall:
@rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN)
+27 -76
View File
@@ -5,8 +5,8 @@ Declarative package manager for Arch Linux that syncs your system with a declare
## Features ## Features
- Declarative state management — define your desired package list in files or stdin - 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 (with `--prune`)
- Explicit package tracking — marks declared packages as explicit - Smart orphan cleanup — removes packages no longer needed (with `--prune`)
- AUR support — builds and installs AUR packages automatically - AUR support — builds and installs AUR packages automatically
- Machine-readable output — perfect for scripting - Machine-readable output — perfect for scripting
@@ -27,22 +27,22 @@ sudo mv declpac /usr/local/bin/
- pacman - pacman
- makepkg (for AUR support) - makepkg (for AUR support)
- git (for AUR support) - git (for AUR support)
- Root privileges - sudo/doas (root privileges)
## Usage ## Usage
```bash ```bash
# Single state file # Single state file
sudo declpac --state packages.txt declpac --state packages.txt
# Multiple state files # Multiple state files
sudo declpac --state base.txt --state apps.txt declpac --state base.txt --state apps.txt
# From stdin # From stdin
cat packages.txt | sudo declpac cat packages.txt | declpac
# Preview changes without applying # Preview changes without applying
sudo declpac --dry-run --state packages.txt declpac --dry-run --state packages.txt
``` ```
### State File Format ### State File Format
@@ -57,12 +57,20 @@ docker
# this is a comment # 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 ### Options
| Flag | Alias | Description | | 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 | | `--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 | | `--help` | `-h` | Show help message |
## How It Works ## How It Works
@@ -72,8 +80,8 @@ docker
3. **Categorize** — Check if packages are in official repos or AUR 3. **Categorize** — Check if packages are in official repos or AUR
4. **Sync** — Install/update packages via pacman 4. **Sync** — Install/update packages via pacman
5. **Build** — Build and install AUR packages via makepkg 5. **Build** — Build and install AUR packages via makepkg
6. **Mark** — Mark declared packages as explicit, all others as dependencies 6. **Mark** (with `--prune`) — Mark declared packages as explicit, all others as dependencies
7. **Cleanup** — Remove orphaned packages 7. **Cleanup** (with `--prune`) — Remove orphaned packages
### Database Freshness ### Database Freshness
@@ -81,70 +89,11 @@ If the pacman database is older than 24 hours, it is automatically refreshed.
### Logging ### 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)
## Output
```
# Packages installed/removed
Installed 5 packages, removed 2 packages
# No changes needed
Installed 0 packages, removed 0 packages
# Dry-run preview
Installed 3 packages, removed 1 packages
Would install: vim, git, docker
Would remove: python2
# Error
error: package not found: <package-name>
```
### Exit Codes
| Code | Meaning |
|------|---------|
| 0 | Success |
| 1 | Error |
## Examples
### Minimal System
```bash
echo -e "base\nbase-devel\nlinux-headers\nvim\ngit\ncurl" > ~/.config/declpac/minimal.txt
sudo declpac --state ~/.config/declpac/minimal.txt
```
### Development Environment
```bash
# development.txt
go
nodejs
python
rust
docker
sudo declpac --state development.txt
```
### Dry-Run
```bash
sudo declpac --dry-run --state packages.txt
```
## Troubleshooting ## Troubleshooting
### Permission denied
Use sudo:
```bash
sudo declpac --state packages.txt
```
### Package not found ### Package not found
Check if the package exists: Check if the package exists:
@@ -166,12 +115,14 @@ declpac/
│ └── main.go # Entry point │ └── main.go # Entry point
├── pkg/ ├── pkg/
│ ├── input/ # State file/stdin reading │ ├── input/ # State file/stdin reading
│ ├── merge/ # Package merging │ ├── fetch/ # Package resolution
│ ├── fetch/ # Package resolution (pacman/AUR) │ ├── aur/ # AUR support
│ │ └── alpm/ # ALPM support
│ ├── pacman/ # Pacman operations │ ├── pacman/ # Pacman operations
│ ├── validation/ # Database freshness check │ ├── read/ # Read packages
├── output/ # Output formatting │ └── sync/ # Sync packages
── state/ # Logging ── log/ # Logging
│ └── output/ # Output formatting
└── README.md └── README.md
``` ```
+38 -18
View File
@@ -8,18 +8,20 @@ import (
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
"github.com/Riyyi/declpac/pkg/auth"
"github.com/Riyyi/declpac/pkg/input" "github.com/Riyyi/declpac/pkg/input"
"github.com/Riyyi/declpac/pkg/merge" "github.com/Riyyi/declpac/pkg/log"
"github.com/Riyyi/declpac/pkg/output" "github.com/Riyyi/declpac/pkg/output"
"github.com/Riyyi/declpac/pkg/pacman" "github.com/Riyyi/declpac/pkg/pacman"
"github.com/Riyyi/declpac/pkg/state" "github.com/Riyyi/declpac/pkg/pacman/read"
"github.com/Riyyi/declpac/pkg/validation"
) )
type Config struct { type Config struct {
StateFiles []string StateFiles []string
NoConfirm bool NoCheck bool
DryRun bool DryRun bool
Prune bool
Verbose bool
} }
func main() { func main() {
@@ -35,13 +37,30 @@ func main() {
Usage: "State file(s) to read package list from", Usage: "State file(s) to read package list from",
Destination: &cfg.StateFiles, Destination: &cfg.StateFiles,
}, },
&cli.BoolFlag{
Name: "nocheck",
Usage: "Skip safety check",
Destination: &cfg.NoCheck,
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "dry-run", Name: "dry-run",
Usage: "Simulate the sync without making changes", Usage: "Simulate the sync without making changes",
Destination: &cfg.DryRun, Destination: &cfg.DryRun,
}, },
&cli.BoolFlag{
Name: "prune",
Usage: "Mark packages and cleanup orphans",
Destination: &cfg.Prune,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Enable verbose output",
Destination: &cfg.Verbose,
},
}, },
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
log.Verbose = cfg.Verbose
return run(cfg) return run(cfg)
}, },
} }
@@ -53,46 +72,47 @@ func main() {
func run(cfg *Config) error { func run(cfg *Config) error {
start := time.Now() start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] run: starting...\n") log.Debug("run: starting...")
packages, err := input.ReadPackages(cfg.StateFiles) packages, err := input.ReadPackages(cfg.StateFiles)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err return err
} }
fmt.Fprintf(os.Stderr, "[debug] run: packages read (%.2fs)\n", time.Since(start).Seconds()) 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 { if cfg.DryRun {
result, err := pacman.DryRun(merged) result, err := read.DryRun(merged)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err return err
} }
fmt.Println(output.Format(result)) fmt.Println(output.Format(result))
fmt.Fprintf(os.Stderr, "[debug] run: dry-run done (%.2fs)\n", time.Since(start).Seconds()) log.Debug("run: dry-run done (%.2fs)", time.Since(start).Seconds())
return nil return nil
} }
if err := state.OpenLog(); err != nil { auth.Start()
if err := log.OpenLog(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err return err
} }
defer state.Close() defer log.Close()
if err := validation.CheckDBFreshness(); err != nil { result, err := pacman.Sync(merged, cfg.NoCheck, cfg.Prune)
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err
}
result, err := pacman.Sync(merged)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err return err
} }
fmt.Println(output.Format(result)) fmt.Println(output.Format(result))
fmt.Fprintf(os.Stderr, "[debug] run: sync done (%.2fs)\n", time.Since(start).Seconds()) log.Debug("run: sync done (%.2fs)", time.Since(start).Seconds())
return nil return nil
} }
@@ -19,47 +19,27 @@
| Pattern | Functions | How | | Pattern | Functions | How |
|---------|------------|-----| |---------|------------|-----|
| Streaming | MarkAllAsDeps, MarkAsExplicit, InstallAUR | `io.MultiWriter` to tee to both terminal and log | | Captured | All state-modifying functions | capture output, write to log with single timestamp at start, write to terminal |
| Captured | SyncPackages, CleanupOrphans | capture with `CombinedOutput()`, write to log, write to terminal |
### One Timestamp Per Tool Call
Instead of streaming with MultiWriter (multiple timestamps), each state-modifying function:
1. Writes timestamp + operation name to log
2. Runs command, captures output
3. Writes captured output to log
4. Writes output to terminal
This ensures exactly 1 timestamp print per tool call.
### Error Handling ### Error Handling
- Write error to log BEFORE returning from function - Write error to log BEFORE returning from function
- Print error to stderr so user sees it - Print error to stderr so user sees it
### Dependencies ### Dependencies
- Add to imports: `io`, `os`, `path/filepath` - Add to imports: `os`, `path/filepath`
### Structure
```go
// pkg/state/state.go
var logFile *os.File
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
return nil
}
func GetLogWriter() io.Writer {
return logFile
}
func Close() error {
if logFile == nil {
return nil
}
return logFile.Close()
}
```
### Flow in Sync() or main entrypoint ### Flow in Sync() or main entrypoint
1. Call `OpenLog()` at program start, defer close 1. Call `OpenLog()` at program start, defer close
2. Each state-modifying function uses `state.GetLogWriter()` via MultiWriter 2. Each state-modifying function calls `state.Write()` with timestamp prefix
### Wire into main.go ### Wire into main.go
- Open log at start of `run()` - Open log at start of `run()`
@@ -7,7 +7,7 @@ debugging and audit.
## What Changes ## What Changes
- Add state directory initialization creating `~/.local/state/declpac` if not exists - Add state directory initialization creating `~/.local/state/declpac` if not exists
- Open/manage a single log file at `$XDG_STATE_HOME/declpac` (e.g., `~/.local/state/declpac/declpac`) - Open/manage a single log file at `/var/log/declpac.log`
- Instrument all state-modifying exec calls in `pkg/pacman/pacman.go` to tee or append output to this file - Instrument all state-modifying exec calls in `pkg/pacman/pacman.go` to tee or append output to this file
- Skip debug messages (internal timing logs) - Skip debug messages (internal timing logs)
- Capture and write errors before returning - Capture and write errors before returning
@@ -18,15 +18,11 @@ In `cmd/declpac/main.go` `run()`:
Modify `pkg/pacman/pacman.go`: Modify `pkg/pacman/pacman.go`:
All state-modifying functions use `state.Write()` instead of `state.GetLogWriter().Write()`: Each state-modifying function writes timestamp ONCE at start, then captures output:
- Write `timestamp - operation name` to log
``` - Run command, capture output
// OLD - Write captured output to log
state.GetLogWriter().Write(output) - Write output to terminal
// NEW
state.Write(output) // auto-prepends timestamp
```
**Functions updated:** **Functions updated:**
- `SyncPackages()` - write output with timestamp - `SyncPackages()` - write output with timestamp
@@ -47,7 +47,7 @@
- [x] 6.1 Get list of currently installed packages before sync - [x] 6.1 Get list of currently installed packages before sync
- [x] 6.2 Mark declared state packages as explicitly installed via pacman -D --explicit - [x] 6.2 Mark declared state packages as explicitly installed via pacman -D --explicit
- [x] 6.3 Run pacman sync operation (5.x series) - [x] 6.3 Run pacman sync operation (5.x series)
- [x] 6.4 Run pacman -Rsu to remove orphaned packages - [x] 6.4 Run pacman -Rns to remove orphaned packages
- [x] 6.5 Capture and report number of packages removed - [x] 6.5 Capture and report number of packages removed
- [x] 6.6 Handle case where no orphans exist (no packages removed) - [x] 6.6 Handle case where no orphans exist (no packages removed)
@@ -18,4 +18,4 @@ The program fails to find packages that exist in official repositories (like `cm
## Impact ## Impact
- `pkg/pacman/pacman.go`: Modify `New()` function to register sync DBs after getting them from the handle - `pkg/fetch/fetch.go`: Modify `New()` function to register sync DBs after getting them from the handle
+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()
}
}
+148
View File
@@ -0,0 +1,148 @@
package alpm
import (
"fmt"
"time"
"github.com/Jguer/dyalpm"
"github.com/Riyyi/declpac/pkg/log"
)
var (
Root = "/"
PacmanState = "/var/lib/pacman"
)
type Handle struct {
handle dyalpm.Handle
localDB dyalpm.Database
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...")
handle, err := dyalpm.Initialize(Root, PacmanState)
if err != nil {
return nil, fmt.Errorf("failed to initialize alpm: %w", err)
}
localDB, err := handle.LocalDB()
if err != nil {
handle.Release()
return nil, fmt.Errorf("failed to get local database: %w", err)
}
syncDBs, err := handle.SyncDBs()
if err != nil {
handle.Release()
return nil, fmt.Errorf("failed to get sync databases: %w", err)
}
if len(syncDBs) == 0 {
syncDBs, err = registerSyncDBs(handle)
if err != nil {
handle.Release()
return nil, fmt.Errorf("failed to register sync databases: %w", err)
}
}
log.Debug("alpm.New: done (%.2fs)", time.Since(start).Seconds())
return &Handle{
handle: handle,
localDB: localDB,
syncDBs: syncDBs,
}, nil
}
// -----------------------------------------
// private
func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) {
log.Debug("registerSyncDBs: starting...")
repos := []string{"core", "extra", "multilib"}
var dbs []dyalpm.Database
for _, repo := range repos {
db, err := handle.RegisterSyncDB(repo, 0)
if err != nil {
continue
}
count := 0
db.PkgCache().ForEach(func(pkg dyalpm.Package) error {
count++
return nil
})
if count > 0 {
dbs = append(dbs, db)
}
}
log.Debug("registerSyncDBs: done (%d dbs)", len(dbs))
return dbs, nil
}
+100
View File
@@ -0,0 +1,100 @@
package aur
import (
"encoding/json"
"io"
"net/http"
"net/url"
"time"
"github.com/Riyyi/declpac/pkg/log"
)
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"`
Depends []string `json:"Depends"`
MakeDepends []string `json:"MakeDepends"`
}
func (p Package) AllDepends() []string {
return append(p.Depends, p.MakeDepends...)
}
type Response struct {
Results []Package `json:"results"`
}
type Client struct {
cache map[string]Package
}
func New() *Client {
return &Client{
cache: make(map[string]Package),
}
}
func (c *Client) Fetch(packages []string) (map[string]Package, error) {
start := time.Now()
log.Debug("aur.Fetch: starting...")
result := make(map[string]Package)
if len(packages) == 0 {
return result, nil
}
var uncached []string
for _, pkg := range packages {
if _, ok := c.cache[pkg]; !ok {
uncached = append(uncached, pkg)
}
}
if len(uncached) == 0 {
log.Debug("aur.Fetch: done (cached) (%.2fs)", time.Since(start).Seconds())
for _, pkg := range packages {
result[pkg] = c.cache[pkg]
}
return result, nil
}
v := url.Values{}
for _, pkg := range packages {
v.Add("arg[]", pkg)
}
resp, err := http.Get(AURInfoURL + "&" + v.Encode())
if err != nil {
return result, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return result, err
}
var aurResp Response
if err := json.Unmarshal(body, &aurResp); err != nil {
return result, err
}
for _, r := range aurResp.Results {
c.cache[r.Name] = r
result[r.Name] = r
}
log.Debug("aur.Fetch: done (%.2fs)", time.Since(start).Seconds())
return result, nil
}
func (c *Client) Get(name string) (Package, bool) {
pkg, ok := c.cache[name]
return pkg, ok
}
+47 -234
View File
@@ -1,104 +1,54 @@
package fetch package fetch
import ( import (
"encoding/json"
"fmt" "fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time" "time"
"github.com/Jguer/dyalpm" "github.com/Riyyi/declpac/pkg/fetch/alpm"
"github.com/Riyyi/declpac/pkg/fetch/aur"
"github.com/Riyyi/declpac/pkg/log"
) )
var (
Root = "/"
LockFile = "/var/lib/pacman/db.lock"
AURInfoURL = "https://aur.archlinux.org/rpc?v=5&type=info"
)
type Fetcher struct {
aurCache map[string]AURPackage
handle dyalpm.Handle
localDB dyalpm.Database
syncDBs []dyalpm.Database
}
type PackageInfo struct { type PackageInfo struct {
Name string Name string
InAUR bool InAUR bool
Exists bool Exists bool
Installed bool Installed bool
AURInfo *AURPackage Provided string
syncPkg dyalpm.Package AURInfo *aur.Package
} }
type AURResponse struct { type Fetcher struct {
Results []AURPackage `json:"results"` alpmHandle *alpm.Handle
aurClient *aur.Client
} }
type AURPackage struct { // -----------------------------------------
Name string `json:"Name"` // constructor
PackageBase string `json:"PackageBase"`
Version string `json:"Version"`
URL string `json:"URL"`
}
func New() (*Fetcher, error) { func New() (*Fetcher, error) {
start := time.Now() start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] Fetcher New: starting...\n") log.Debug("fetch.Fetcher New: starting...")
handle, err := dyalpm.Initialize(Root, "/var/lib/pacman") alpmHandle, err := alpm.New()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to initialize alpm: %w", err) return nil, err
} }
localDB, err := handle.LocalDB() aurClient := aur.New()
if err != nil {
handle.Release()
return nil, fmt.Errorf("failed to get local database: %w", err)
}
syncDBs, err := handle.SyncDBs() log.Debug("fetch.Fetcher New: done (%.2fs)", time.Since(start).Seconds())
if err != nil {
handle.Release()
return nil, fmt.Errorf("failed to get sync databases: %w", err)
}
if len(syncDBs) == 0 {
syncDBs, err = registerSyncDBs(handle)
if err != nil {
handle.Release()
return nil, fmt.Errorf("failed to register sync databases: %w", err)
}
}
fmt.Fprintf(os.Stderr, "[debug] Fetcher New: done (%.2fs)\n", time.Since(start).Seconds())
return &Fetcher{ return &Fetcher{
aurCache: make(map[string]AURPackage), alpmHandle: alpmHandle,
handle: handle, aurClient: aurClient,
localDB: localDB,
syncDBs: syncDBs,
}, nil }, nil
} }
func (f *Fetcher) Close() error { // -----------------------------------------
if f.handle != nil { // public
f.handle.Release()
}
return nil
}
func (f *Fetcher) GetAURPackage(name string) (AURPackage, bool) {
pkg, ok := f.aurCache[name]
return pkg, ok
}
func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) { func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) {
localPkgs, err := f.buildLocalPkgMap() localPkgs, err := f.alpmHandle.LocalPackages()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -109,106 +59,47 @@ func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) {
return result, nil return result, nil
} }
func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) { func (f *Fetcher) Close() error {
fmt.Fprintf(os.Stderr, "[debug] registerSyncDBs: starting...\n") return f.alpmHandle.Release()
repos := []string{"core", "extra", "multilib"}
var dbs []dyalpm.Database
for _, repo := range repos {
db, err := handle.RegisterSyncDB(repo, 0)
if err != nil {
continue
}
count := 0
db.PkgCache().ForEach(func(pkg dyalpm.Package) error {
count++
return nil
})
if count > 0 {
dbs = append(dbs, db)
}
}
fmt.Fprintf(os.Stderr, "[debug] registerSyncDBs: done (%d dbs)\n", len(dbs))
return dbs, nil
} }
func (f *Fetcher) buildLocalPkgMap() (map[string]dyalpm.Package, error) { func (f *Fetcher) FetchAur(packages []string) (map[string]aur.Package, error) {
start := time.Now() return f.aurClient.Fetch(packages)
fmt.Fprintf(os.Stderr, "[debug] buildLocalPkgMap: starting...\n")
localPkgs := make(map[string]dyalpm.Package)
err := f.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)
}
fmt.Fprintf(os.Stderr, "[debug] buildLocalPkgMap: done (%.2fs)\n", time.Since(start).Seconds())
return localPkgs, nil
} }
func (f *Fetcher) checkSyncDBs(pkgNames []string) (map[string]dyalpm.Package, error) { func (f *Fetcher) FindProvidingPackage(depName string) (string, bool) {
start := time.Now() return f.alpmHandle.FindProvidingPackage(depName)
fmt.Fprintf(os.Stderr, "[debug] checkSyncDBs: starting...\n") }
syncPkgs := make(map[string]dyalpm.Package) func (f *Fetcher) GetAURPackage(name string) (aur.Package, bool) {
pkgSet := make(map[string]bool) return f.aurClient.Get(name)
for _, name := range pkgNames {
pkgSet[name] = true
}
for _, db := range f.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)
}
}
fmt.Fprintf(os.Stderr, "[debug] checkSyncDBs: done (%.2fs)\n", time.Since(start).Seconds())
return syncPkgs, nil
} }
func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) { func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
start := time.Now() start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] Resolve: starting...\n") log.Debug("fetch.Resolve: starting...")
result := make(map[string]*PackageInfo) result := make(map[string]*PackageInfo)
for _, pkg := range packages { for _, pkg := range packages {
result[pkg] = &PackageInfo{Name: pkg, Exists: false} result[pkg] = &PackageInfo{Name: pkg, Exists: false}
} }
syncPkgs, err := f.checkSyncDBs(packages) syncPkgs, err := f.alpmHandle.SyncPackages(packages)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Fprintf(os.Stderr, "[debug] Resolve: sync db check done (%.2fs)\n", time.Since(start).Seconds()) log.Debug("fetch.Resolve: sync db check done (%.2fs)", time.Since(start).Seconds())
for pkg, syncPkg := range syncPkgs { for pkg := range syncPkgs {
result[pkg].Exists = true result[pkg].Exists = true
result[pkg].InAUR = false result[pkg].InAUR = false
result[pkg].syncPkg = syncPkg
} }
localPkgs, err := f.buildLocalPkgMap() localPkgs, err := f.alpmHandle.LocalPackages()
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Fprintf(os.Stderr, "[debug] Resolve: local pkgs built (%.2fs)\n", time.Since(start).Seconds()) log.Debug("fetch.Resolve: local pkgs built (%.2fs)", time.Since(start).Seconds())
for pkg := range localPkgs { for pkg := range localPkgs {
if info, ok := result[pkg]; ok { if info, ok := result[pkg]; ok {
@@ -224,7 +115,9 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
} }
if len(notInSync) > 0 { if len(notInSync) > 0 {
f.ensureAURCache(notInSync) if _, err := f.aurClient.Fetch(notInSync); err != nil {
log.Debug("fetch.Resolve: aur fetch error: %v", err)
}
for _, pkg := range packages { for _, pkg := range packages {
info := result[pkg] info := result[pkg]
@@ -232,12 +125,19 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
continue continue
} }
if aurInfo, ok := f.aurCache[pkg]; ok { if aurInfo, ok := f.aurClient.Get(pkg); ok {
info.InAUR = true info.InAUR = true
info.AURInfo = &aurInfo info.AURInfo = &aurInfo
continue 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) return nil, fmt.Errorf("package not found: %s", pkg)
} }
} }
@@ -249,93 +149,6 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
} }
} }
fmt.Fprintf(os.Stderr, "[debug] Resolve: done (%.2fs)\n", time.Since(start).Seconds()) log.Debug("fetch.Resolve: done (%.2fs)", time.Since(start).Seconds())
return result, nil return result, nil
} }
func (f *Fetcher) ensureAURCache(packages []string) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] ensureAURCache: starting...\n")
if len(packages) == 0 {
return
}
var uncached []string
for _, pkg := range packages {
if _, ok := f.aurCache[pkg]; !ok {
uncached = append(uncached, pkg)
}
}
if len(uncached) == 0 {
fmt.Fprintf(os.Stderr, "[debug] ensureAURCache: done (%.2fs)\n", time.Since(start).Seconds())
return
}
_, err := f.fetchAURInfo(uncached)
if err != nil {
fmt.Fprintf(os.Stderr, "[debug] ensureAURCache: fetch error: %v\n", err)
}
fmt.Fprintf(os.Stderr, "[debug] ensureAURCache: done (%.2fs)\n", time.Since(start).Seconds())
}
func (f *Fetcher) fetchAURInfo(packages []string) (map[string]AURPackage, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] fetchAURInfo: starting...\n")
result := make(map[string]AURPackage)
if len(packages) == 0 {
return result, nil
}
v := url.Values{}
for _, pkg := range packages {
v.Add("arg[]", pkg)
}
resp, err := http.Get(AURInfoURL + "&" + v.Encode())
if err != nil {
return result, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return result, err
}
var aurResp AURResponse
if err := json.Unmarshal(body, &aurResp); err != nil {
return result, err
}
for _, r := range aurResp.Results {
f.aurCache[r.Name] = r
result[r.Name] = r
}
fmt.Fprintf(os.Stderr, "[debug] fetchAURInfo: done (%.2fs)\n", time.Since(start).Seconds())
return result, nil
}
func (f *Fetcher) ListOrphans() ([]string, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] ListOrphans: starting...\n")
cmd := exec.Command("pacman", "-Qdtq")
orphans, err := cmd.Output()
if err != nil {
return nil, nil
}
list := strings.TrimSpace(string(orphans))
if list == "" {
fmt.Fprintf(os.Stderr, "[debug] ListOrphans: done (%.2fs)\n", time.Since(start).Seconds())
return nil, nil
}
fmt.Fprintf(os.Stderr, "[debug] ListOrphans: done (%.2fs)\n", time.Since(start).Seconds())
return strings.Split(list, "\n"), nil
}
+54 -9
View File
@@ -2,15 +2,43 @@ package input
import ( import (
"bufio" "bufio"
"errors"
"os" "os"
"path/filepath"
"strings" "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) { func ReadPackages(stateFiles []string) (map[string]bool, error) {
packages := make(map[string]bool) packages := make(map[string]bool)
for _, file := range stateFiles { 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 return nil, err
} }
} }
@@ -22,6 +50,31 @@ func ReadPackages(stateFiles []string) (map[string]bool, error) {
return packages, nil 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 { func readStateFile(path string, packages map[string]bool) error {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
@@ -60,11 +113,3 @@ func readStdin(packages map[string]bool) error {
return scanner.Err() 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
}
+74
View File
@@ -0,0 +1,74 @@
package log
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/Riyyi/declpac/pkg/lib"
)
var logFile *os.File
var Verbose bool
// -----------------------------------------
// public
func Close() error {
if logFile == nil {
return nil
}
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")
header := fmt.Sprintf("\n--- %s ---\n", ts)
logFile.Write([]byte(header))
}
-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 ToRemove []string
} }
// -----------------------------------------
// public
func Format(r *Result) string { func Format(r *Result) string {
var b strings.Builder 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 { if len(r.ToInstall) > 0 {
b.WriteString("\nWould install: ") b.WriteString("\nwould install: ")
b.WriteString(strings.Join(r.ToInstall, ", ")) b.WriteString(strings.Join(r.ToInstall, ", "))
} }
if len(r.ToRemove) > 0 { if len(r.ToRemove) > 0 {
b.WriteString("\nWould remove: ") b.WriteString("\nwould remove: ")
b.WriteString(strings.Join(r.ToRemove, ", ")) b.WriteString(strings.Join(r.ToRemove, ", "))
} }
return b.String() return b.String()
+119 -277
View File
@@ -2,146 +2,133 @@ package pacman
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "slices"
"path/filepath"
"regexp"
"strings"
"time" "time"
"github.com/Riyyi/declpac/pkg/fetch" "github.com/Riyyi/declpac/pkg/fetch"
"github.com/Riyyi/declpac/pkg/log"
"github.com/Riyyi/declpac/pkg/output" "github.com/Riyyi/declpac/pkg/output"
"github.com/Riyyi/declpac/pkg/state" "github.com/Riyyi/declpac/pkg/pacman/read"
"github.com/Riyyi/declpac/pkg/validation" "github.com/Riyyi/declpac/pkg/pacman/sync"
) )
func MarkAllAsDeps() error { func Sync(packages []string, noCheck bool, prune bool) (*output.Result, error) {
start := time.Now() start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] MarkAllAsDeps: starting...\n") log.Debug("Sync: starting...")
listCmd := exec.Command("pacman", "-Qq") explicitList, err := read.ExplicitList()
output, err := listCmd.Output()
if err != nil {
return fmt.Errorf("failed to list packages: %w", err)
}
packages := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(packages) == 0 || packages[0] == "" {
fmt.Fprintf(os.Stderr, "[debug] MarkAllAsDeps: no packages to mark (%.2fs)\n", time.Since(start).Seconds())
return nil
}
args := append([]string{"-D", "--asdeps"}, packages...)
cmd := exec.Command("pacman", args...)
state.Write([]byte("MarkAllAsDeps...\n"))
cmd.Stdout = io.MultiWriter(os.Stdout, state.GetLogWriter())
cmd.Stderr = io.MultiWriter(os.Stderr, state.GetLogWriter())
err = cmd.Run()
if err != nil {
state.Write([]byte(fmt.Sprintf("error: %v\n", err)))
}
fmt.Fprintf(os.Stderr, "[debug] MarkAllAsDeps: done (%.2fs)\n", time.Since(start).Seconds())
return err
}
func MarkAsExplicit(packages []string) error {
if len(packages) == 0 {
return nil
}
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] MarkAsExplicit: starting...\n")
args := append([]string{"-D", "--asexplicit"}, packages...)
cmd := exec.Command("pacman", args...)
state.Write([]byte("MarkAsExplicit...\n"))
cmd.Stdout = io.MultiWriter(os.Stdout, state.GetLogWriter())
cmd.Stderr = io.MultiWriter(os.Stderr, state.GetLogWriter())
err := cmd.Run()
if err != nil {
state.Write([]byte(fmt.Sprintf("error: %v\n", err)))
}
fmt.Fprintf(os.Stderr, "[debug] MarkAsExplicit: done (%.2fs)\n", time.Since(start).Seconds())
return err
}
func Sync(packages []string) (*output.Result, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] Sync: starting...\n")
before, err := getInstalledCount()
if err != nil { if err != nil {
return nil, err return nil, err
} }
explicitCount := len(explicitList)
if err := validation.CheckDBFreshness(); err != nil { 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 return nil, err
} }
fmt.Fprintf(os.Stderr, "[debug] Sync: database fresh (%.2fs)\n", time.Since(start).Seconds()) before := len(list)
fresh, err := read.DBFreshness()
if err != nil {
return nil, err
}
if !fresh {
if err := sync.RefreshDB(log.GetLogWriter()); err != nil {
return nil, err
}
}
log.Debug("Sync: database fresh (%.2fs)", time.Since(start).Seconds())
f, err := fetch.New() f, err := fetch.New()
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
fmt.Fprintf(os.Stderr, "[debug] Sync: initialized fetcher (%.2fs)\n", time.Since(start).Seconds()) log.Debug("Sync: initialized fetcher (%.2fs)", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] Sync: categorizing packages...\n") log.Debug("Sync: categorizing packages...")
pacmanPkgs, aurPkgs, err := categorizePackages(f, packages) pacmanPkgs, aurPkgs, err := categorizePackages(f, packages)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Fprintf(os.Stderr, "[debug] Sync: packages categorized (%.2fs)\n", time.Since(start).Seconds()) log.Debug("Sync: packages categorized (%.2fs)", time.Since(start).Seconds())
if len(pacmanPkgs) > 0 { if len(pacmanPkgs) > 0 {
fmt.Fprintf(os.Stderr, "[debug] Sync: syncing %d pacman packages...\n", len(pacmanPkgs)) log.Debug("Sync: syncing %d pacman packages...", len(pacmanPkgs))
_, err = SyncPackages(pacmanPkgs) if err := sync.SyncPackages(pacmanPkgs, log.GetLogWriter()); err != nil {
if err != nil {
return nil, err return nil, err
} }
fmt.Fprintf(os.Stderr, "[debug] Sync: pacman packages synced (%.2fs)\n", time.Since(start).Seconds()) log.Debug("Sync: pacman packages synced (%.2fs)", time.Since(start).Seconds())
} }
for _, pkg := range aurPkgs { for _, pkg := range aurPkgs {
fmt.Fprintf(os.Stderr, "[debug] Sync: installing AUR package %s...\n", pkg) if slices.Contains(list, pkg) {
if err := InstallAUR(f, pkg); err != nil { continue
}
log.Debug("Sync: installing AUR package %s...", pkg)
aurInfo, ok := f.GetAURPackage(pkg)
if !ok {
return nil, fmt.Errorf("AUR package not found in cache: %s", pkg)
}
if err := sync.InstallAUR(f, pkg, aurInfo.PackageBase, false, log.GetLogWriter()); err != nil {
return nil, err return nil, err
} }
fmt.Fprintf(os.Stderr, "[debug] Sync: AUR package %s installed (%.2fs)\n", pkg, time.Since(start).Seconds()) log.Debug("Sync: AUR package %s installed (%.2fs)", pkg, time.Since(start).Seconds())
} }
fmt.Fprintf(os.Stderr, "[debug] Sync: marking all as deps...\n") var removed int
if err := MarkAllAsDeps(); err != nil { if prune {
fmt.Fprintf(os.Stderr, "warning: could not mark all as deps: %v\n", err) log.Debug("Sync: running prune sanity check...")
if err := pruneSanityCheck(packages); err != nil {
return nil, err
} }
fmt.Fprintf(os.Stderr, "[debug] Sync: all marked as deps (%.2fs)\n", time.Since(start).Seconds()) log.Debug("Sync: prune sanity check passed (%.2fs)", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] Sync: marking state packages as explicit...\n") log.Debug("Sync: marking all as deps...")
if err := MarkAsExplicit(packages); err != nil { if err := markAllAsDeps(); err != nil {
return nil, err
}
log.Debug("Sync: all marked as deps (%.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) fmt.Fprintf(os.Stderr, "warning: could not mark state packages as explicit: %v\n", err)
} }
fmt.Fprintf(os.Stderr, "[debug] Sync: state packages marked as explicit (%.2fs)\n", time.Since(start).Seconds()) log.Debug("Sync: state packages marked as explicit (%.2fs)", time.Since(start).Seconds())
removed, err := CleanupOrphans() removed, err = cleanupOrphans()
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
list, _ = read.List()
if err != nil {
return nil, err
}
after := len(list)
after, _ := getInstalledCount()
installedCount := max(after-before, 0) installedCount := max(after-before, 0)
fmt.Fprintf(os.Stderr, "[debug] Sync: done (%.2fs)\n", time.Since(start).Seconds()) log.Debug("Sync: done (%.2fs)", time.Since(start).Seconds())
return &output.Result{ return &output.Result{
Installed: installedCount, Installed: installedCount,
Removed: removed, Removed: removed,
}, nil }, nil
} }
// -----------------------------------------
// private
func categorizePackages(f *fetch.Fetcher, packages []string) (pacmanPkgs, aurPkgs []string, err error) { func categorizePackages(f *fetch.Fetcher, packages []string) (pacmanPkgs, aurPkgs []string, err error) {
start := time.Now() start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] categorizePackages: starting...\n") log.Debug("categorizePackages: starting...")
resolved, err := f.Resolve(packages) resolved, err := f.Resolve(packages)
if err != nil { if err != nil {
@@ -161,215 +148,70 @@ func categorizePackages(f *fetch.Fetcher, packages []string) (pacmanPkgs, aurPkg
} }
} }
fmt.Fprintf(os.Stderr, "[debug] categorizePackages: done (%.2fs)\n", time.Since(start).Seconds()) log.Debug("categorizePackages: done (%.2fs)", time.Since(start).Seconds())
return pacmanPkgs, aurPkgs, nil return pacmanPkgs, aurPkgs, nil
} }
func InstallAUR(f *fetch.Fetcher, pkgName string) error { func cleanupOrphans() (int, error) {
start := time.Now() start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: starting...\n") log.Debug("cleanupOrphans: starting...")
aurInfo, ok := f.GetAURPackage(pkgName) orphans, err := read.ListOrphans()
if !ok {
return fmt.Errorf("AUR package not found in cache: %s", pkgName)
}
sudoUser := os.Getenv("SUDO_USER")
if sudoUser == "" {
sudoUser = os.Getenv("USER")
if sudoUser == "" {
sudoUser = "root"
}
}
tmpDir := "/tmp/declpac-aur-" + pkgName
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)
cloneURL := "https://aur.archlinux.org/" + aurInfo.PackageBase + ".git"
state.Write([]byte("Cloning " + cloneURL + "\n"))
cloneCmd := exec.Command("su", "-", sudoUser, "-c", "git clone "+cloneURL+" "+tmpDir)
cloneCmd.Stdout = io.MultiWriter(os.Stdout, state.GetLogWriter())
cloneCmd.Stderr = io.MultiWriter(os.Stderr, state.GetLogWriter())
if err := cloneCmd.Run(); err != nil {
errMsg := fmt.Sprintf("failed to clone AUR repo: %v\n", err)
state.Write([]byte("error: " + errMsg))
return fmt.Errorf("failed to clone AUR repo: %w", err)
}
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: cloned (%.2fs)\n", time.Since(start).Seconds())
state.Write([]byte("Building package...\n"))
makepkgCmd := exec.Command("su", "-", sudoUser, "-c", "cd "+tmpDir+" && makepkg -s --noconfirm")
makepkgCmd.Stdout = io.MultiWriter(os.Stdout, state.GetLogWriter())
makepkgCmd.Stderr = io.MultiWriter(os.Stderr, state.GetLogWriter())
if err := makepkgCmd.Run(); err != nil {
errMsg := fmt.Sprintf("makepkg failed to build AUR package: %v\n", err)
state.Write([]byte("error: " + errMsg))
return fmt.Errorf("makepkg failed to build AUR package: %w", err)
}
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: built (%.2fs)\n", time.Since(start).Seconds())
pkgFile, err := findPKGFile(tmpDir)
if err != nil { if err != nil {
return fmt.Errorf("failed to find built package: %w", err) log.Write([]byte(fmt.Sprintf("error: %v\n", err)))
return 0, err
} }
state.Write([]byte("Installing package...\n")) removed, err := sync.RemoveOrphans(orphans, log.GetLogWriter())
installCmd := exec.Command("pacman", "-U", "--noconfirm", pkgFile) if err != nil {
installCmd.Stdout = io.MultiWriter(os.Stdout, state.GetLogWriter()) log.Write([]byte(fmt.Sprintf("error: %v\n", err)))
installCmd.Stderr = io.MultiWriter(os.Stderr, state.GetLogWriter()) return 0, err
if err := installCmd.Run(); err != nil {
errMsg := fmt.Sprintf("failed to install package: %v\n", err)
state.Write([]byte("error: " + errMsg))
return fmt.Errorf("failed to install package: %w", err)
} }
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: built (%.2fs)\n", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: done (%.2fs)\n", time.Since(start).Seconds()) 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 return nil
} }
func findPKGFile(dir string) (string, error) { // pruneSanityCheck checks if the installation of all state packages succeeded,
entries, err := os.ReadDir(dir) // before attempting to do package marking and orphan cleanup.
if err != nil { func pruneSanityCheck(statePackages []string) error {
return "", err
}
for _, entry := range entries {
name := entry.Name()
if strings.HasSuffix(name, ".pkg.tar.zst") || strings.HasSuffix(name, ".pkg.tar.gz") {
return filepath.Join(dir, name), nil
}
}
return "", fmt.Errorf("no package file found in %s", dir)
}
func getInstalledCount() (int, error) {
start := time.Now() start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] getInstalledCount: starting...\n") log.Debug("pruneSanityCheck: starting...")
cmd := exec.Command("pacman", "-Qq") localPackages, err := read.List()
output, err := cmd.Output()
if err != nil { if err != nil {
return 0, nil return fmt.Errorf("failed to list local packages: %w", err)
}
count := strings.Count(string(output), "\n") + 1
if strings.TrimSpace(string(output)) == "" {
count = 0
} }
fmt.Fprintf(os.Stderr, "[debug] getInstalledCount: done (%.2fs)\n", time.Since(start).Seconds()) var missing []string
return count, nil for _, pkg := range statePackages {
} if !slices.Contains(localPackages, pkg) {
missing = append(missing, pkg)
func SyncPackages(packages []string) (int, error) { }
start := time.Now() }
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: starting...\n")
if len(missing) > 0 {
args := append([]string{"-S", "--needed"}, packages...) return fmt.Errorf("safety check: missing state packages: %v", missing)
cmd := exec.Command("pacman", args...) }
output, err := cmd.CombinedOutput()
if err != nil { log.Debug("pruneSanityCheck: done (%.2fs)", time.Since(start).Seconds())
errMsg := fmt.Sprintf("pacman sync failed: %s", output) return nil
state.Write([]byte(errMsg))
return 0, fmt.Errorf("pacman sync failed: %s", output)
}
if len(output) > 0 {
state.Write(output)
}
re := regexp.MustCompile(`upgrading (\S+)`)
matches := re.FindAllStringSubmatch(string(output), -1)
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: done (%.2fs)\n", time.Since(start).Seconds())
return len(matches), nil
}
func CleanupOrphans() (int, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] CleanupOrphans: starting...\n")
f, err := fetch.New()
if err != nil {
return 0, err
}
defer f.Close()
orphans, err := f.ListOrphans()
if err != nil || len(orphans) == 0 {
fmt.Fprintf(os.Stderr, "[debug] CleanupOrphans: done (%.2fs)\n", time.Since(start).Seconds())
return 0, nil
}
removeCmd := exec.Command("pacman", "-Rns")
output, err := removeCmd.CombinedOutput()
if err != nil {
errMsg := fmt.Sprintf("%s: %s", err, output)
state.Write([]byte(errMsg))
return 0, fmt.Errorf("%s: %s", err, output)
}
if len(output) > 0 {
state.Write(output)
}
count := len(orphans)
fmt.Fprintf(os.Stderr, "[debug] CleanupOrphans: done (%.2fs)\n", time.Since(start).Seconds())
return count, nil
}
func DryRun(packages []string) (*output.Result, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] DryRun: starting...\n")
f, err := fetch.New()
if err != nil {
return nil, err
}
defer f.Close()
fmt.Fprintf(os.Stderr, "[debug] DryRun: initialized fetcher (%.2fs)\n", time.Since(start).Seconds())
resolved, err := f.Resolve(packages)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] DryRun: packages resolved (%.2fs)\n", time.Since(start).Seconds())
localPkgs, err := f.BuildLocalPkgMap()
if err != nil {
return nil, err
}
var toInstall []string
var aurPkgs []string
for _, pkg := range packages {
info := resolved[pkg]
if info == nil || (!info.Exists && !info.InAUR) {
return nil, fmt.Errorf("package not found: %s", pkg)
}
if info.InAUR {
aurPkgs = append(aurPkgs, pkg)
} else if _, installed := localPkgs[pkg]; !installed {
toInstall = append(toInstall, pkg)
}
}
fmt.Fprintf(os.Stderr, "[debug] DryRun: packages categorized (%.2fs)\n", time.Since(start).Seconds())
orphans, err := f.ListOrphans()
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] DryRun: orphans listed (%.2fs)\n", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] DryRun: done (%.2fs)\n", time.Since(start).Seconds())
return &output.Result{
Installed: len(toInstall) + len(aurPkgs),
Removed: len(orphans),
ToInstall: append(toInstall, aurPkgs...),
ToRemove: orphans,
}, nil
} }
+149
View File
@@ -0,0 +1,149 @@
package read
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/Riyyi/declpac/pkg/fetch"
"github.com/Riyyi/declpac/pkg/log"
"github.com/Riyyi/declpac/pkg/output"
)
var LockFile = "/var/lib/pacman/db.lock"
func DBFreshness() (bool, error) {
info, err := os.Stat(LockFile)
if err != nil {
return false, nil
}
age := time.Since(info.ModTime())
return age <= 24*time.Hour, nil
}
func DryRun(packages []string) (*output.Result, error) {
start := time.Now()
log.Debug("DryRun: starting...")
f, err := fetch.New()
if err != nil {
return nil, err
}
defer f.Close()
log.Debug("DryRun: initialized fetcher (%.2fs)", time.Since(start).Seconds())
resolved, err := f.Resolve(packages)
if err != nil {
return nil, err
}
log.Debug("DryRun: packages resolved (%.2fs)", time.Since(start).Seconds())
var toInstall []string
var aurPkgs []string
for _, pkg := range packages {
info := resolved[pkg]
if info == nil || (!info.Exists && !info.InAUR) {
return nil, fmt.Errorf("package not found: %s", pkg)
}
if info.InAUR && !info.Installed {
aurPkgs = append(aurPkgs, pkg)
} else if !info.Installed {
toInstall = append(toInstall, pkg)
}
}
log.Debug("DryRun: packages categorized (%.2fs)", time.Since(start).Seconds())
orphans, err := ListOrphans()
if err != nil {
return nil, err
}
log.Debug("DryRun: orphans listed (%.2fs)", time.Since(start).Seconds())
pkgSet := make(map[string]bool)
for _, p := range packages {
pkgSet[p] = true
}
var toRemove []string
for _, o := range orphans {
if !pkgSet[o] {
toRemove = append(toRemove, o)
}
}
log.Debug("DryRun: done (%.2fs)", time.Since(start).Seconds())
return &output.Result{
Installed: len(toInstall) + len(aurPkgs),
Removed: len(toRemove),
ToInstall: append(toInstall, aurPkgs...),
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
}
+318
View File
@@ -0,0 +1,318 @@
package sync
import (
"fmt"
"io"
"os"
"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"
)
type Result struct {
Installed int
Removed int
}
// -----------------------------------------
// public
func InstallAUR(f *fetch.Fetcher, pkgName string, packageBase string, asDeps bool, logWriter io.Writer) error {
start := time.Now()
log.Debug("InstallAUR: starting...")
if logWriter == nil {
logWriter = os.Stderr
}
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("failed to find built package: %w", err)
}
if err := installBuiltPackage(pkgFile, logWriter); err != nil {
return err
}
log.Debug("InstallAUR: done (%.2fs)", time.Since(start).Seconds())
return nil
}
func MarkAs(packages []string, flag string, logWriter io.Writer) error {
if len(packages) == 0 {
return nil
}
start := time.Now()
flagName := map[string]string{"deps": "asdeps", "explicit": "asexplicit"}[flag]
log.Debug("MarkAs(%s): starting...", flag)
if logWriter == nil {
logWriter = os.Stderr
}
args := append([]string{"-D", "--" + flagName}, packages...)
cmd := auth.Command("pacman", args...)
cmd.Stdout = logWriter
cmd.Stderr = logWriter
err := cmd.Run()
if err != nil {
return fmt.Errorf("mark as %s failed: %w", flag, err)
}
log.Debug("MarkAs(%s): done (%.2fs)", flag, 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
}
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...")
if logWriter == nil {
logWriter = os.Stderr
}
if len(orphans) == 0 {
log.Debug("RemoveOrphans: done (no orphans) (%.2fs)", time.Since(start).Seconds())
return 0, nil
}
args := make([]string, 0, 3+len(orphans))
args = append(args, "pacman", "-Rns", "--noconfirm")
args = append(args, orphans...)
removeCmd := auth.Command(args[0], args[1:]...)
removeCmd.Stdout = logWriter
removeCmd.Stderr = logWriter
err := removeCmd.Run()
if err != nil {
return 0, fmt.Errorf("remove orphans failed: %w", err)
}
count := len(orphans)
log.Debug("RemoveOrphans: done (%d) (%.2fs)", count, time.Since(start).Seconds())
return count, nil
}
func SyncPackages(packages []string, logWriter io.Writer) error {
start := time.Now()
log.Debug("SyncPackages: starting...")
if logWriter == nil {
logWriter = os.Stderr
}
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)
}
log.Debug("SyncPackages: done (%.2fs)", time.Since(start).Seconds())
return nil
}
// -----------------------------------------
// private
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)
}
return nil
}
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")
}
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)
}
return nil
}
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") {
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
}
-43
View File
@@ -1,43 +0,0 @@
package state
import (
"fmt"
"io"
"os"
"path/filepath"
"time"
)
var logFile *os.File
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
return nil
}
func GetLogWriter() io.Writer {
return logFile
}
func Write(msg []byte) {
PrependWithTimestamp(logFile, msg)
}
func Close() error {
if logFile == nil {
return nil
}
return logFile.Close()
}
func PrependWithTimestamp(w io.Writer, msg []byte) {
ts := time.Now().Format("2006-01-02 15:04:05")
header := fmt.Sprintf("\n--- %s ---\n", ts)
w.Write([]byte(header))
w.Write(msg)
}
-33
View File
@@ -1,33 +0,0 @@
package validation
import (
"fmt"
"os"
"os/exec"
"time"
)
var LockFile = "/var/lib/pacman/db.lock"
func CheckDBFreshness() error {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] CheckDBFreshness: starting...\n")
info, err := os.Stat(LockFile)
if err != nil {
return nil
}
age := time.Since(info.ModTime())
if age > 24*time.Hour {
cmd := exec.Command("pacman", "-Syy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to refresh pacman database: %w", err)
}
}
fmt.Fprintf(os.Stderr, "[debug] CheckDBFreshness: done (%.2fs)\n", time.Since(start).Seconds())
return nil
}
+6
View File
@@ -5,6 +5,12 @@
"source": "JuliusBrussee/caveman", "source": "JuliusBrussee/caveman",
"sourceType": "github", "sourceType": "github",
"computedHash": "a818cdc41dcfaa50dd891c5cb5e5705968338de02e7e37949ca56e8c30ad4176" "computedHash": "a818cdc41dcfaa50dd891c5cb5e5705968338de02e7e37949ca56e8c30ad4176"
},
"grill-me": {
"source": "mattpocock/skills",
"sourceType": "github",
"skillPath": "skills/productivity/grill-me/SKILL.md",
"computedHash": "784f0dbb7403b0f00324bce9a112f715342777a0daee7bbb7385f9c6f0a170ea"
} }
} }
} }