Compare commits

..

No commits in common. 'a934dfa1f086ac6a79e7dfce58ac26ac4743894d' and '8348cc21df1f8d4355f46972064acd94ddc539a4' have entirely different histories.

  1. 28
      .opencode/commands/opsx-fixup.md
  2. 73
      .opencode/skills/make-commit/SKILL.md
  3. 11
      README.md
  4. 15
      cmd/declpac/main.go
  5. 44
      openspec/changes/archive/2026-04-17-add-operation-logging/design.md
  6. 2
      openspec/changes/archive/2026-04-17-add-operation-logging/proposal.md
  7. 14
      openspec/changes/archive/2026-04-17-add-operation-logging/tasks.md
  8. 2
      openspec/changes/archive/2026-04-17-declpac-cli-tool/tasks.md
  9. 2
      openspec/changes/archive/2026-04-17-fix-sync-dbs-not-loaded/proposal.md
  10. 137
      pkg/fetch/alpm/alpm.go
  11. 94
      pkg/fetch/aur/aur.go
  12. 265
      pkg/fetch/fetch.go
  13. 302
      pkg/pacman/pacman.go
  14. 120
      pkg/pacman/read/read.go
  15. 180
      pkg/pacman/sync/sync.go
  16. 16
      pkg/state/state.go
  17. 33
      pkg/validation/validation.go

28
.opencode/commands/opsx-fixup.md

@ -1,28 +0,0 @@
---
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>"

73
.opencode/skills/make-commit/SKILL.md

@ -7,26 +7,13 @@ description: >
license: GPL-3.0
metadata:
author: riyyi
version: "1.1"
version: "1.0"
---
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**
1. **[REQUIRED] Ask user if this commit is by them or by AI**
@ -38,63 +25,49 @@ Rules:
- "By me" - User made the commit
- "By AI" - AI made the commit
2. **Compose the commit message**
2. **Check for commit message**
**Capitalization rule**: Commit messages should start with a capital letter,
unless it refers to a tool or project that explicitly uses lowercase as its
name (e.g., "go", "npm", "rustc").
If the user did NOT provide a commit message, generate one from staged
changes:
If the user did NOT provide a commit message, generate one from staged changes:
```bash
git --no-pager diff --staged
git diff --staged --stat
```
Write a commit message following the format above.
Create a reasonable commit message based on the changes.
If the user **DID** provide a message, treat it as raw input and apply the
format rules to it.
If thed user DID provide a message, format it into a proper commit message.
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**
3. **Show commit message and confirm**
Use the **question tool** to ask:
> "Is this commit message okay, or would you like to make tweaks?"
> ```
> <message>
> ```
Options:
- "Looks good" - Proceed with this message
- "Make tweaks" - User will provide a new message or describe changes
- "Make tweaks" - User will provide a new message
**If user wants tweaks**: apply the same validation (step 3) to the revised
message before committing.
**If user wants tweaks**: Ask them for the new commit message.
5. **Make the commit**
4. **Make the commit**
For a title-only message:
```bash
git commit -m "<title>"
```
Use the commit message provided by the user.
For a message with a body, pass `-m` twice (git inserts the blank line):
**If by user:**
```bash
git commit -m "<title>" -m "<body>"
git commit -m "<message>"
```
(Uses git config user as both committer and author)
Append `--author="AI Bot <ai@local>"` when the commit is by AI:
**If by AI:**
```bash
git commit -m "<title>" [-m "<body>"] --author="AI Bot <ai@local>"
git commit -m "<message>" --author="AI Bot <ai@local>"
```
(Uses git config for committer, but sets author to AI Bot)
**Output**
- Tell user the commit was made.
- If AI commit, mention that the author was set to "AI Bot <ai@local>".
- Tell user the commit was made
- If AI commit, mention that the author was set to "AI Bot <ai@local>"

11
README.md

@ -167,14 +167,11 @@ declpac/
├── pkg/
│ ├── input/ # State file/stdin reading
│ ├── merge/ # Package merging
│ ├── fetch/ # Package resolution
│ │ ├── aur/ # AUR support
│ │ └── alpm/ # ALPM support
│ ├── fetch/ # Package resolution (pacman/AUR)
│ ├── pacman/ # Pacman operations
│ │ ├── read/ # Read packages
│ │ └── sync/ # Sync packages
│ ├── log/ # Logging
│ └── output/ # Output formatting
│ ├── validation/ # Database freshness check
│ ├── output/ # Output formatting
│ └── state/ # Logging
└── README.md
```

15
cmd/declpac/main.go

@ -9,11 +9,11 @@ import (
"github.com/urfave/cli/v3"
"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"
"github.com/Riyyi/declpac/pkg/state"
"github.com/Riyyi/declpac/pkg/validation"
)
type Config struct {
@ -65,7 +65,7 @@ func run(cfg *Config) error {
merged := merge.Merge(packages)
if cfg.DryRun {
result, err := read.DryRun(merged)
result, err := pacman.DryRun(merged)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err
@ -75,11 +75,16 @@ func run(cfg *Config) error {
return nil
}
if err := log.OpenLog(); err != nil {
if err := state.OpenLog(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err
}
defer state.Close()
if err := validation.CheckDBFreshness(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return err
}
defer log.Close()
result, err := pacman.Sync(merged)
if err != nil {

44
openspec/changes/archive/2026-04-17-add-operation-logging/design.md

@ -19,27 +19,47 @@
| Pattern | Functions | How |
|---------|------------|-----|
| Captured | All state-modifying functions | capture output, write to log with single timestamp at start, 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.
| Streaming | MarkAllAsDeps, MarkAsExplicit, InstallAUR | `io.MultiWriter` to tee to both terminal and log |
| Captured | SyncPackages, CleanupOrphans | capture with `CombinedOutput()`, write to log, write to terminal |
### Error Handling
- Write error to log BEFORE returning from function
- Print error to stderr so user sees it
### Dependencies
- Add to imports: `os`, `path/filepath`
- Add to imports: `io`, `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
1. Call `OpenLog()` at program start, defer close
2. Each state-modifying function calls `state.Write()` with timestamp prefix
2. Each state-modifying function uses `state.GetLogWriter()` via MultiWriter
### Wire into main.go
- Open log at start of `run()`

2
openspec/changes/archive/2026-04-17-add-operation-logging/proposal.md

@ -7,7 +7,7 @@ debugging and audit.
## What Changes
- Add state directory initialization creating `~/.local/state/declpac` if not exists
- Open/manage a single log file at `/var/log/declpac.log`
- Open/manage a single log file at `$XDG_STATE_HOME/declpac` (e.g., `~/.local/state/declpac/declpac`)
- 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)
- Capture and write errors before returning

14
openspec/changes/archive/2026-04-17-add-operation-logging/tasks.md

@ -18,11 +18,15 @@ In `cmd/declpac/main.go` `run()`:
Modify `pkg/pacman/pacman.go`:
Each state-modifying function writes timestamp ONCE at start, then captures output:
- Write `timestamp - operation name` to log
- Run command, capture output
- Write captured output to log
- Write output to terminal
All state-modifying functions use `state.Write()` instead of `state.GetLogWriter().Write()`:
```
// OLD
state.GetLogWriter().Write(output)
// NEW
state.Write(output) // auto-prepends timestamp
```
**Functions updated:**
- `SyncPackages()` - write output with timestamp

2
openspec/changes/archive/2026-04-17-declpac-cli-tool/tasks.md

@ -47,7 +47,7 @@
- [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.3 Run pacman sync operation (5.x series)
- [x] 6.4 Run pacman -Rns to remove orphaned packages
- [x] 6.4 Run pacman -Rsu to remove orphaned packages
- [x] 6.5 Capture and report number of packages removed
- [x] 6.6 Handle case where no orphans exist (no packages removed)

2
openspec/changes/archive/2026-04-17-fix-sync-dbs-not-loaded/proposal.md

@ -18,4 +18,4 @@ The program fails to find packages that exist in official repositories (like `cm
## Impact
- `pkg/fetch/fetch.go`: Modify `New()` function to register sync DBs after getting them from the handle
- `pkg/pacman/pacman.go`: Modify `New()` function to register sync DBs after getting them from the handle

137
pkg/fetch/alpm/alpm.go

@ -1,137 +0,0 @@
package alpm
import (
"fmt"
"os"
"time"
"github.com/Jguer/dyalpm"
)
var (
Root = "/"
PacmanState = "/var/lib/pacman"
)
type Handle struct {
handle dyalpm.Handle
localDB dyalpm.Database
syncDBs []dyalpm.Database
}
func New() (*Handle, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] alpm.New: starting...\n")
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)
}
}
fmt.Fprintf(os.Stderr, "[debug] alpm.New: done (%.2fs)\n", time.Since(start).Seconds())
return &Handle{
handle: handle,
localDB: localDB,
syncDBs: syncDBs,
}, nil
}
func (h *Handle) Release() error {
if h.handle != nil {
h.handle.Release()
}
return nil
}
func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) {
fmt.Fprintf(os.Stderr, "[debug] registerSyncDBs: starting...\n")
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 (h *Handle) LocalPackages() (map[string]dyalpm.Package, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] LocalPackages: starting...\n")
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)
}
fmt.Fprintf(os.Stderr, "[debug] LocalPackages: done (%.2fs)\n", time.Since(start).Seconds())
return localPkgs, nil
}
func (h *Handle) SyncPackages(pkgNames []string) (map[string]dyalpm.Package, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: starting...\n")
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)
}
}
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: done (%.2fs)\n", time.Since(start).Seconds())
return syncPkgs, nil
}

94
pkg/fetch/aur/aur.go

@ -1,94 +0,0 @@
package aur
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
)
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"`
}
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()
fmt.Fprintf(os.Stderr, "[debug] aur.Fetch: starting...\n")
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 {
fmt.Fprintf(os.Stderr, "[debug] aur.Fetch: done (cached) (%.2fs)\n", 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
}
fmt.Fprintf(os.Stderr, "[debug] aur.Fetch: done (%.2fs)\n", time.Since(start).Seconds())
return result, nil
}
func (c *Client) Get(name string) (Package, bool) {
pkg, ok := c.cache[name]
return pkg, ok
}

265
pkg/fetch/fetch.go

@ -1,55 +1,104 @@
package fetch
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
"github.com/Riyyi/declpac/pkg/fetch/alpm"
"github.com/Riyyi/declpac/pkg/fetch/aur"
"github.com/Jguer/dyalpm"
)
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 {
Name string
InAUR bool
Exists bool
Installed bool
AURInfo *aur.Package
AURInfo *AURPackage
syncPkg dyalpm.Package
}
type Fetcher struct {
alpmHandle *alpm.Handle
aurClient *aur.Client
type AURResponse struct {
Results []AURPackage `json:"results"`
}
type AURPackage struct {
Name string `json:"Name"`
PackageBase string `json:"PackageBase"`
Version string `json:"Version"`
URL string `json:"URL"`
}
func New() (*Fetcher, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] fetch.Fetcher New: starting...\n")
fmt.Fprintf(os.Stderr, "[debug] Fetcher New: starting...\n")
alpmHandle, err := alpm.New()
handle, err := dyalpm.Initialize(Root, "/var/lib/pacman")
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to initialize alpm: %w", err)
}
aurClient := aur.New()
localDB, err := handle.LocalDB()
if err != nil {
handle.Release()
return nil, fmt.Errorf("failed to get local database: %w", err)
}
fmt.Fprintf(os.Stderr, "[debug] fetch.Fetcher New: done (%.2fs)\n", time.Since(start).Seconds())
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)
}
}
fmt.Fprintf(os.Stderr, "[debug] Fetcher New: done (%.2fs)\n", time.Since(start).Seconds())
return &Fetcher{
alpmHandle: alpmHandle,
aurClient: aurClient,
aurCache: make(map[string]AURPackage),
handle: handle,
localDB: localDB,
syncDBs: syncDBs,
}, nil
}
func (f *Fetcher) Close() error {
return f.alpmHandle.Release()
if f.handle != nil {
f.handle.Release()
}
return nil
}
func (f *Fetcher) GetAURPackage(name string) (aur.Package, bool) {
return f.aurClient.Get(name)
func (f *Fetcher) GetAURPackage(name string) (AURPackage, bool) {
pkg, ok := f.aurCache[name]
return pkg, ok
}
func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) {
localPkgs, err := f.alpmHandle.LocalPackages()
localPkgs, err := f.buildLocalPkgMap()
if err != nil {
return nil, err
}
@ -60,31 +109,106 @@ func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) {
return result, nil
}
func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) {
fmt.Fprintf(os.Stderr, "[debug] registerSyncDBs: starting...\n")
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) {
start := time.Now()
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) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] checkSyncDBs: starting...\n")
syncPkgs := make(map[string]dyalpm.Package)
pkgSet := make(map[string]bool)
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) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] fetch.Resolve: starting...\n")
fmt.Fprintf(os.Stderr, "[debug] Resolve: starting...\n")
result := make(map[string]*PackageInfo)
for _, pkg := range packages {
result[pkg] = &PackageInfo{Name: pkg, Exists: false}
}
syncPkgs, err := f.alpmHandle.SyncPackages(packages)
syncPkgs, err := f.checkSyncDBs(packages)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] fetch.Resolve: sync db check done (%.2fs)\n", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] Resolve: sync db check done (%.2fs)\n", time.Since(start).Seconds())
for pkg := range syncPkgs {
for pkg, syncPkg := range syncPkgs {
result[pkg].Exists = true
result[pkg].InAUR = false
result[pkg].syncPkg = syncPkg
}
localPkgs, err := f.alpmHandle.LocalPackages()
localPkgs, err := f.buildLocalPkgMap()
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] fetch.Resolve: local pkgs built (%.2fs)\n", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] Resolve: local pkgs built (%.2fs)\n", time.Since(start).Seconds())
for pkg := range localPkgs {
if info, ok := result[pkg]; ok {
@ -100,9 +224,7 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
}
if len(notInSync) > 0 {
if _, err := f.aurClient.Fetch(notInSync); err != nil {
fmt.Fprintf(os.Stderr, "[debug] fetch.Resolve: aur fetch error: %v\n", err)
}
f.ensureAURCache(notInSync)
for _, pkg := range packages {
info := result[pkg]
@ -110,7 +232,7 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
continue
}
if aurInfo, ok := f.aurClient.Get(pkg); ok {
if aurInfo, ok := f.aurCache[pkg]; ok {
info.InAUR = true
info.AURInfo = &aurInfo
continue
@ -127,6 +249,93 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
}
}
fmt.Fprintf(os.Stderr, "[debug] fetch.Resolve: done (%.2fs)\n", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] Resolve: done (%.2fs)\n", time.Since(start).Seconds())
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
}

302
pkg/pacman/pacman.go

@ -3,33 +3,80 @@ package pacman
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/Riyyi/declpac/pkg/fetch"
"github.com/Riyyi/declpac/pkg/log"
"github.com/Riyyi/declpac/pkg/output"
"github.com/Riyyi/declpac/pkg/pacman/read"
"github.com/Riyyi/declpac/pkg/pacman/sync"
"github.com/Riyyi/declpac/pkg/state"
"github.com/Riyyi/declpac/pkg/validation"
)
func Sync(packages []string) (*output.Result, error) {
func MarkAllAsDeps() error {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] Sync: starting...\n")
fmt.Fprintf(os.Stderr, "[debug] MarkAllAsDeps: starting...\n")
list, err := read.List()
listCmd := exec.Command("pacman", "-Qq")
output, err := listCmd.Output()
if err != nil {
return nil, err
return fmt.Errorf("failed to list packages: %w", err)
}
before := len(list)
fresh, err := read.DBFreshness()
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 = state.GetLogWriter()
cmd.Stderr = state.GetLogWriter()
err = cmd.Run()
if err != nil {
return nil, err
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 = state.GetLogWriter()
cmd.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
}
if !fresh {
if err := sync.RefreshDB(log.GetLogWriter()); err != nil {
func Sync(packages []string) (*output.Result, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] Sync: starting...\n")
before, err := getInstalledCount()
if err != nil {
return nil, err
}
if err := validation.CheckDBFreshness(); err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] Sync: database fresh (%.2fs)\n", time.Since(start).Seconds())
@ -49,7 +96,8 @@ func Sync(packages []string) (*output.Result, error) {
if len(pacmanPkgs) > 0 {
fmt.Fprintf(os.Stderr, "[debug] Sync: syncing %d pacman packages...\n", len(pacmanPkgs))
if err := sync.SyncPackages(pacmanPkgs, log.GetLogWriter()); err != nil {
_, err = SyncPackages(pacmanPkgs)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] Sync: pacman packages synced (%.2fs)\n", time.Since(start).Seconds())
@ -57,37 +105,30 @@ func Sync(packages []string) (*output.Result, error) {
for _, pkg := range aurPkgs {
fmt.Fprintf(os.Stderr, "[debug] Sync: installing AUR package %s...\n", pkg)
aurInfo, ok := f.GetAURPackage(pkg)
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 := InstallAUR(f, pkg); err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] Sync: AUR package %s installed (%.2fs)\n", pkg, time.Since(start).Seconds())
}
fmt.Fprintf(os.Stderr, "[debug] Sync: marking all as deps...\n")
markAllAsDeps()
if err := MarkAllAsDeps(); err != nil {
fmt.Fprintf(os.Stderr, "warning: could not mark all as deps: %v\n", err)
}
fmt.Fprintf(os.Stderr, "[debug] Sync: all marked as deps (%.2fs)\n", time.Since(start).Seconds())
fmt.Fprintf(os.Stderr, "[debug] Sync: marking state packages as explicit...\n")
if err := sync.MarkAs(packages, "explicit", log.GetLogWriter()); err != nil {
if err := MarkAsExplicit(packages); err != nil {
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())
removed, err := cleanupOrphans()
if err != nil {
return nil, err
}
list, _ = read.List()
removed, err := CleanupOrphans()
if err != nil {
return nil, err
}
after := len(list)
after, _ := getInstalledCount()
installedCount := max(after-before, 0)
fmt.Fprintf(os.Stderr, "[debug] Sync: done (%.2fs)\n", time.Since(start).Seconds())
@ -123,40 +164,211 @@ func categorizePackages(f *fetch.Fetcher, packages []string) (pacmanPkgs, aurPkg
return pacmanPkgs, aurPkgs, nil
}
func markAllAsDeps() error {
func InstallAUR(f *fetch.Fetcher, pkgName string) error {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] markAllAsDeps: starting...\n")
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: starting...\n")
packages, err := read.List()
if err != nil || len(packages) == 0 {
return fmt.Errorf("failed to list packages: %w", err)
aurInfo, ok := f.GetAURPackage(pkgName)
if !ok {
return fmt.Errorf("AUR package not found in cache: %s", pkgName)
}
if err := sync.MarkAs(packages, "deps", log.GetLogWriter()); err != nil {
log.Write([]byte(fmt.Sprintf("error: %v\n", err)))
return err
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"
cloneCmd := exec.Command("su", "-", sudoUser, "-c", "git clone "+cloneURL+" "+tmpDir)
state.Write([]byte("Cloning " + cloneURL + "\n"))
cloneCmd.Stdout = state.GetLogWriter()
cloneCmd.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())
fmt.Fprintf(os.Stderr, "[debug] markAllAsDeps: done (%.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 = state.GetLogWriter()
makepkgCmd.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 {
return fmt.Errorf("failed to find built package: %w", err)
}
state.Write([]byte("Installing package...\n"))
installCmd := exec.Command("pacman", "-U", "--noconfirm", pkgFile)
installCmd.Stdout = state.GetLogWriter()
installCmd.Stderr = state.GetLogWriter()
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())
return nil
}
func cleanupOrphans() (int, error) {
func findPKGFile(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 filepath.Join(dir, name), nil
}
}
return "", fmt.Errorf("no package file found in %s", dir)
}
func getInstalledCount() (int, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] cleanupOrphans: starting...\n")
fmt.Fprintf(os.Stderr, "[debug] getInstalledCount: starting...\n")
orphans, err := read.ListOrphans()
cmd := exec.Command("pacman", "-Qq")
output, err := cmd.Output()
if err != nil {
log.Write([]byte(fmt.Sprintf("error: %v\n", err)))
return 0, err
return 0, nil
}
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())
return count, nil
}
func SyncPackages(packages []string) (int, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: starting...\n")
args := append([]string{"-S", "--needed"}, packages...)
cmd := exec.Command("pacman", args...)
output, err := cmd.CombinedOutput()
if err != nil {
errMsg := fmt.Sprintf("pacman sync failed: %s", output)
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")
removed, err := sync.RemoveOrphans(orphans, log.GetLogWriter())
f, err := fetch.New()
if err != nil {
log.Write([]byte(fmt.Sprintf("error: %v\n", err)))
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
}
fmt.Fprintf(os.Stderr, "[debug] cleanupOrphans: done (%.2fs)\n", time.Since(start).Seconds())
return removed, 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
}

120
pkg/pacman/read/read.go

@ -1,120 +0,0 @@
package read
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/Riyyi/declpac/pkg/fetch"
"github.com/Riyyi/declpac/pkg/output"
)
var LockFile = "/var/lib/pacman/db.lock"
func List() ([]string, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] List: starting...\n")
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
}
fmt.Fprintf(os.Stderr, "[debug] List: done (%.2fs)\n", time.Since(start).Seconds())
return list, nil
}
func ListOrphans() ([]string, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] ListOrphans: starting...\n")
cmd := exec.Command("pacman", "-Qdtq")
output, err := cmd.Output()
if err != nil {
return nil, err
}
orphans := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(orphans) > 0 && orphans[0] == "" {
orphans = orphans[1:]
}
fmt.Fprintf(os.Stderr, "[debug] ListOrphans: done (%.2fs)\n", time.Since(start).Seconds())
return orphans, nil
}
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()
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())
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)
}
}
fmt.Fprintf(os.Stderr, "[debug] DryRun: packages categorized (%.2fs)\n", time.Since(start).Seconds())
orphans, err := ListOrphans()
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] DryRun: orphans listed (%.2fs)\n", 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)
}
}
fmt.Fprintf(os.Stderr, "[debug] DryRun: done (%.2fs)\n", time.Since(start).Seconds())
return &output.Result{
Installed: len(toInstall) + len(aurPkgs),
Removed: len(toRemove),
ToInstall: append(toInstall, aurPkgs...),
ToRemove: toRemove,
}, nil
}

180
pkg/pacman/sync/sync.go

@ -1,180 +0,0 @@
package sync
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
)
type Result struct {
Installed int
Removed int
}
func SyncPackages(packages []string, logWriter io.Writer) error {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: starting...\n")
if logWriter == nil {
logWriter = os.Stderr
}
args := append([]string{"-S", "--needed"}, packages...)
cmd := exec.Command("pacman", args...)
cmd.Stdout = logWriter
cmd.Stderr = logWriter
err := cmd.Run()
if err != nil {
return fmt.Errorf("pacman sync failed: %w", err)
}
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: done (%.2fs)\n", time.Since(start).Seconds())
return nil
}
func RefreshDB(logWriter io.Writer) error {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] RefreshDB: starting...\n")
if logWriter == nil {
logWriter = os.Stderr
}
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)
}
fmt.Fprintf(os.Stderr, "[debug] RefreshDB: done (%.2fs)\n", 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]
fmt.Fprintf(os.Stderr, "[debug] MarkAs(%s): starting...\n", flag)
if logWriter == nil {
logWriter = os.Stderr
}
args := append([]string{"-D", "--" + flagName}, packages...)
cmd := exec.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)
}
fmt.Fprintf(os.Stderr, "[debug] MarkAs(%s): done (%.2fs)\n", flag, time.Since(start).Seconds())
return nil
}
func RemoveOrphans(orphans []string, logWriter io.Writer) (int, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] RemoveOrphans: starting...\n")
if logWriter == nil {
logWriter = os.Stderr
}
if len(orphans) == 0 {
fmt.Fprintf(os.Stderr, "[debug] RemoveOrphans: done (no orphans) (%.2fs)\n", time.Since(start).Seconds())
return 0, nil
}
args := make([]string, 0, 2+len(orphans))
args = append(args, "pacman", "-Rns")
args = append(args, orphans...)
removeCmd := exec.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)
fmt.Fprintf(os.Stderr, "[debug] RemoveOrphans: done (%d) (%.2fs)\n", count, time.Since(start).Seconds())
return count, nil
}
func InstallAUR(pkgName string, packageBase string, logWriter io.Writer) error {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: starting...\n")
if logWriter == nil {
logWriter = os.Stderr
}
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/" + packageBase + ".git"
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)
}
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: cloned (%.2fs)\n", time.Since(start).Seconds())
makepkgCmd := exec.Command("su", "-", sudoUser, "-c", "cd "+tmpDir+" && makepkg -s --noconfirm")
makepkgCmd.Stdout = logWriter
makepkgCmd.Stderr = logWriter
if err := makepkgCmd.Run(); err != nil {
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 {
return fmt.Errorf("failed to find built package: %w", err)
}
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)
}
fmt.Fprintf(os.Stderr, "[debug] InstallAUR: done (%.2fs)\n", time.Since(start).Seconds())
return nil
}
func findPKGFile(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
}
}
return "", fmt.Errorf("no package file found in %s", dir)
}

16
pkg/log/log.go → pkg/state/state.go

@ -1,4 +1,4 @@
package log
package state
import (
"fmt"
@ -8,12 +8,8 @@ import (
"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)
@ -21,7 +17,6 @@ func OpenLog() error {
return err
}
logFile = f
writeTimestamp()
return nil
}
@ -30,7 +25,7 @@ func GetLogWriter() io.Writer {
}
func Write(msg []byte) {
logFile.Write(msg)
PrependWithTimestamp(logFile, msg)
}
func Close() error {
@ -40,10 +35,9 @@ func Close() error {
return logFile.Close()
}
// -----------------------------------------
func writeTimestamp() {
func PrependWithTimestamp(w io.Writer, msg []byte) {
ts := time.Now().Format("2006-01-02 15:04:05")
header := fmt.Sprintf("\n--- %s ---\n", ts)
logFile.Write([]byte(header))
w.Write([]byte(header))
w.Write(msg)
}

33
pkg/validation/validation.go

@ -0,0 +1,33 @@
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
}
Loading…
Cancel
Save