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.
This commit is contained in:
AI Bot
2026-04-19 11:06:52 +02:00
committed by Riyyi
parent 1dffd70db2
commit c90d2e8916
8 changed files with 90 additions and 70 deletions
+9 -9
View File
@@ -2,10 +2,10 @@ package alpm
import (
"fmt"
"os"
"time"
"github.com/Jguer/dyalpm"
"github.com/Riyyi/declpac/pkg/log"
)
var (
@@ -21,7 +21,7 @@ type Handle struct {
func New() (*Handle, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] alpm.New: starting...\n")
log.Debug("alpm.New: starting...")
handle, err := dyalpm.Initialize(Root, PacmanState)
if err != nil {
@@ -48,7 +48,7 @@ func New() (*Handle, error) {
}
}
fmt.Fprintf(os.Stderr, "[debug] alpm.New: done (%.2fs)\n", time.Since(start).Seconds())
log.Debug("alpm.New: done (%.2fs)", time.Since(start).Seconds())
return &Handle{
handle: handle,
localDB: localDB,
@@ -64,7 +64,7 @@ func (h *Handle) Release() error {
}
func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) {
fmt.Fprintf(os.Stderr, "[debug] registerSyncDBs: starting...\n")
log.Debug("registerSyncDBs: starting...")
repos := []string{"core", "extra", "multilib"}
var dbs []dyalpm.Database
@@ -86,13 +86,13 @@ func registerSyncDBs(handle dyalpm.Handle) ([]dyalpm.Database, error) {
}
}
fmt.Fprintf(os.Stderr, "[debug] registerSyncDBs: done (%d dbs)\n", len(dbs))
log.Debug("registerSyncDBs: done (%d dbs)", 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")
log.Debug("LocalPackages: starting...")
localPkgs := make(map[string]dyalpm.Package)
@@ -104,13 +104,13 @@ func (h *Handle) LocalPackages() (map[string]dyalpm.Package, error) {
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())
log.Debug("LocalPackages: done (%.2fs)", time.Since(start).Seconds())
return localPkgs, nil
}
func (h *Handle) SyncPackages(pkgNames []string) (map[string]dyalpm.Package, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: starting...\n")
log.Debug("SyncPackages: starting...")
syncPkgs := make(map[string]dyalpm.Package)
pkgSet := make(map[string]bool)
@@ -132,6 +132,6 @@ func (h *Handle) SyncPackages(pkgNames []string) (map[string]dyalpm.Package, err
}
}
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: done (%.2fs)\n", time.Since(start).Seconds())
log.Debug("SyncPackages: done (%.2fs)", time.Since(start).Seconds())
return syncPkgs, nil
}
+5 -5
View File
@@ -2,12 +2,12 @@ package aur
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
"github.com/Riyyi/declpac/pkg/log"
)
var AURInfoURL = "https://aur.archlinux.org/rpc?v=5&type=info"
@@ -35,7 +35,7 @@ func New() *Client {
func (c *Client) Fetch(packages []string) (map[string]Package, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] aur.Fetch: starting...\n")
log.Debug("aur.Fetch: starting...")
result := make(map[string]Package)
@@ -51,7 +51,7 @@ func (c *Client) Fetch(packages []string) (map[string]Package, error) {
}
if len(uncached) == 0 {
fmt.Fprintf(os.Stderr, "[debug] aur.Fetch: done (cached) (%.2fs)\n", time.Since(start).Seconds())
log.Debug("aur.Fetch: done (cached) (%.2fs)", time.Since(start).Seconds())
for _, pkg := range packages {
result[pkg] = c.cache[pkg]
}
@@ -84,7 +84,7 @@ func (c *Client) Fetch(packages []string) (map[string]Package, error) {
result[r.Name] = r
}
fmt.Fprintf(os.Stderr, "[debug] aur.Fetch: done (%.2fs)\n", time.Since(start).Seconds())
log.Debug("aur.Fetch: done (%.2fs)", time.Since(start).Seconds())
return result, nil
}
+8 -8
View File
@@ -2,11 +2,11 @@ package fetch
import (
"fmt"
"os"
"time"
"github.com/Riyyi/declpac/pkg/fetch/alpm"
"github.com/Riyyi/declpac/pkg/fetch/aur"
"github.com/Riyyi/declpac/pkg/log"
)
type PackageInfo struct {
@@ -24,7 +24,7 @@ type Fetcher struct {
func New() (*Fetcher, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] fetch.Fetcher New: starting...\n")
log.Debug("fetch.Fetcher New: starting...")
alpmHandle, err := alpm.New()
if err != nil {
@@ -33,7 +33,7 @@ func New() (*Fetcher, error) {
aurClient := aur.New()
fmt.Fprintf(os.Stderr, "[debug] fetch.Fetcher New: done (%.2fs)\n", time.Since(start).Seconds())
log.Debug("fetch.Fetcher New: done (%.2fs)", time.Since(start).Seconds())
return &Fetcher{
alpmHandle: alpmHandle,
aurClient: aurClient,
@@ -62,7 +62,7 @@ func (f *Fetcher) BuildLocalPkgMap() (map[string]interface{}, error) {
func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
start := time.Now()
fmt.Fprintf(os.Stderr, "[debug] fetch.Resolve: starting...\n")
log.Debug("fetch.Resolve: starting...")
result := make(map[string]*PackageInfo)
for _, pkg := range packages {
@@ -73,7 +73,7 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] fetch.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 := range syncPkgs {
result[pkg].Exists = true
@@ -84,7 +84,7 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "[debug] fetch.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 {
if info, ok := result[pkg]; ok {
@@ -101,7 +101,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)
log.Debug("fetch.Resolve: aur fetch error: %v", err)
}
for _, pkg := range packages {
@@ -127,6 +127,6 @@ func (f *Fetcher) Resolve(packages []string) (map[string]*PackageInfo, error) {
}
}
fmt.Fprintf(os.Stderr, "[debug] fetch.Resolve: done (%.2fs)\n", time.Since(start).Seconds())
log.Debug("fetch.Resolve: done (%.2fs)", time.Since(start).Seconds())
return result, nil
}