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:
+20
-21
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
func Sync(packages []string) (*output.Result, error) {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] Sync: starting...\n")
|
||||
log.Debug("Sync: starting...")
|
||||
|
||||
list, err := read.List()
|
||||
if err != nil {
|
||||
@@ -32,36 +32,35 @@ func Sync(packages []string) (*output.Result, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "[debug] Sync: database fresh (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("Sync: database fresh (%.2fs)", time.Since(start).Seconds())
|
||||
|
||||
f, err := fetch.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
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 {
|
||||
fmt.Fprintf(os.Stderr, "[debug] Sync: syncing %d pacman packages...\n", len(pacmanPkgs))
|
||||
log.Debug("Sync: syncing %d pacman packages...", len(pacmanPkgs))
|
||||
if err := sync.SyncPackages(pacmanPkgs, log.GetLogWriter()); err != nil {
|
||||
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 {
|
||||
if slices.Contains(list, pkg) {
|
||||
fmt.Fprintf(os.Stderr, "[debug] Sync: AUR package %s already installed, skipping...\n", pkg)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "[debug] Sync: installing AUR package %s...\n", pkg)
|
||||
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)
|
||||
@@ -69,18 +68,18 @@ func Sync(packages []string) (*output.Result, error) {
|
||||
if err := sync.InstallAUR(pkg, aurInfo.PackageBase, log.GetLogWriter()); err != nil {
|
||||
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")
|
||||
log.Debug("Sync: marking all as deps...")
|
||||
markAllAsDeps()
|
||||
fmt.Fprintf(os.Stderr, "[debug] Sync: all marked as deps (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("Sync: all marked as deps (%.2fs)", time.Since(start).Seconds())
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] Sync: marking state packages as explicit...\n")
|
||||
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, "[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()
|
||||
if err != nil {
|
||||
@@ -95,7 +94,7 @@ func Sync(packages []string) (*output.Result, error) {
|
||||
|
||||
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{
|
||||
Installed: installedCount,
|
||||
Removed: removed,
|
||||
@@ -104,7 +103,7 @@ func Sync(packages []string) (*output.Result, error) {
|
||||
|
||||
func categorizePackages(f *fetch.Fetcher, packages []string) (pacmanPkgs, aurPkgs []string, err error) {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] categorizePackages: starting...\n")
|
||||
log.Debug("categorizePackages: starting...")
|
||||
|
||||
resolved, err := f.Resolve(packages)
|
||||
if err != nil {
|
||||
@@ -124,13 +123,13 @@ 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
|
||||
}
|
||||
|
||||
func markAllAsDeps() error {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] markAllAsDeps: starting...\n")
|
||||
log.Debug("markAllAsDeps: starting...")
|
||||
|
||||
packages, err := read.List()
|
||||
if err != nil || len(packages) == 0 {
|
||||
@@ -142,13 +141,13 @@ func markAllAsDeps() error {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] markAllAsDeps: done (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("markAllAsDeps: done (%.2fs)", time.Since(start).Seconds())
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanupOrphans() (int, error) {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] cleanupOrphans: starting...\n")
|
||||
log.Debug("cleanupOrphans: starting...")
|
||||
|
||||
orphans, err := read.ListOrphans()
|
||||
if err != nil {
|
||||
@@ -162,6 +161,6 @@ func cleanupOrphans() (int, error) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] cleanupOrphans: done (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("cleanupOrphans: done (%.2fs)", time.Since(start).Seconds())
|
||||
return removed, nil
|
||||
}
|
||||
|
||||
+11
-10
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Riyyi/declpac/pkg/fetch"
|
||||
"github.com/Riyyi/declpac/pkg/log"
|
||||
"github.com/Riyyi/declpac/pkg/output"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,7 @@ var LockFile = "/var/lib/pacman/db.lock"
|
||||
|
||||
func List() ([]string, error) {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] List: starting...\n")
|
||||
log.Debug("List: starting...")
|
||||
|
||||
cmd := exec.Command("pacman", "-Qq")
|
||||
output, err := cmd.Output()
|
||||
@@ -28,13 +29,13 @@ func List() ([]string, error) {
|
||||
list = nil
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] List: done (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("List: done (%.2fs)", time.Since(start).Seconds())
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func ListOrphans() ([]string, error) {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] ListOrphans: starting...\n")
|
||||
log.Debug("ListOrphans: starting...")
|
||||
|
||||
cmd := exec.Command("pacman", "-Qdtq")
|
||||
output, err := cmd.Output()
|
||||
@@ -47,7 +48,7 @@ func ListOrphans() ([]string, error) {
|
||||
orphans = orphans[1:]
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] ListOrphans: done (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("ListOrphans: done (%.2fs)", time.Since(start).Seconds())
|
||||
return orphans, nil
|
||||
}
|
||||
|
||||
@@ -63,20 +64,20 @@ func DBFreshness() (bool, error) {
|
||||
|
||||
func DryRun(packages []string) (*output.Result, error) {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] DryRun: starting...\n")
|
||||
log.Debug("DryRun: starting...")
|
||||
|
||||
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())
|
||||
log.Debug("DryRun: initialized fetcher (%.2fs)", 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())
|
||||
log.Debug("DryRun: packages resolved (%.2fs)", time.Since(start).Seconds())
|
||||
|
||||
var toInstall []string
|
||||
var aurPkgs []string
|
||||
@@ -91,13 +92,13 @@ func DryRun(packages []string) (*output.Result, error) {
|
||||
toInstall = append(toInstall, pkg)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "[debug] DryRun: packages categorized (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("DryRun: packages categorized (%.2fs)", 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())
|
||||
log.Debug("DryRun: orphans listed (%.2fs)", time.Since(start).Seconds())
|
||||
|
||||
pkgSet := make(map[string]bool)
|
||||
for _, p := range packages {
|
||||
@@ -110,7 +111,7 @@ func DryRun(packages []string) (*output.Result, error) {
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] DryRun: done (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("DryRun: done (%.2fs)", time.Since(start).Seconds())
|
||||
return &output.Result{
|
||||
Installed: len(toInstall) + len(aurPkgs),
|
||||
Removed: len(toRemove),
|
||||
|
||||
+15
-13
@@ -7,6 +7,8 @@ import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Riyyi/declpac/pkg/log"
|
||||
)
|
||||
|
||||
type Result struct {
|
||||
@@ -16,7 +18,7 @@ type Result struct {
|
||||
|
||||
func SyncPackages(packages []string, logWriter io.Writer) error {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: starting...\n")
|
||||
log.Debug("SyncPackages: starting...")
|
||||
|
||||
if logWriter == nil {
|
||||
logWriter = os.Stderr
|
||||
@@ -33,13 +35,13 @@ func SyncPackages(packages []string, logWriter io.Writer) error {
|
||||
return fmt.Errorf("pacman sync failed: %w", err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] SyncPackages: done (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("SyncPackages: done (%.2fs)", time.Since(start).Seconds())
|
||||
return nil
|
||||
}
|
||||
|
||||
func RefreshDB(logWriter io.Writer) error {
|
||||
start := time.Now()
|
||||
fmt.Fprintf(os.Stderr, "[debug] RefreshDB: starting...\n")
|
||||
log.Debug("RefreshDB: starting...")
|
||||
|
||||
if logWriter == nil {
|
||||
logWriter = os.Stderr
|
||||
@@ -53,7 +55,7 @@ func RefreshDB(logWriter io.Writer) error {
|
||||
return fmt.Errorf("failed to refresh pacman database: %w", err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] RefreshDB: done (%.2fs)\n", time.Since(start).Seconds())
|
||||
log.Debug("RefreshDB: done (%.2fs)", time.Since(start).Seconds())
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -63,7 +65,7 @@ func MarkAs(packages []string, flag string, logWriter io.Writer) error {
|
||||
}
|
||||
start := time.Now()
|
||||
flagName := map[string]string{"deps": "asdeps", "explicit": "asexplicit"}[flag]
|
||||
fmt.Fprintf(os.Stderr, "[debug] MarkAs(%s): starting...\n", flag)
|
||||
log.Debug("MarkAs(%s): starting...", flag)
|
||||
|
||||
if logWriter == nil {
|
||||
logWriter = os.Stderr
|
||||
@@ -80,20 +82,20 @@ func MarkAs(packages []string, flag string, logWriter io.Writer) error {
|
||||
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())
|
||||
log.Debug("MarkAs(%s): done (%.2fs)", 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")
|
||||
log.Debug("RemoveOrphans: starting...")
|
||||
|
||||
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())
|
||||
log.Debug("RemoveOrphans: done (no orphans) (%.2fs)", time.Since(start).Seconds())
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
@@ -112,13 +114,13 @@ func RemoveOrphans(orphans []string, logWriter io.Writer) (int, error) {
|
||||
|
||||
count := len(orphans)
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[debug] RemoveOrphans: done (%d) (%.2fs)\n", count, time.Since(start).Seconds())
|
||||
log.Debug("RemoveOrphans: done (%d) (%.2fs)", 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")
|
||||
log.Debug("InstallAUR: starting...")
|
||||
|
||||
if logWriter == nil {
|
||||
logWriter = os.Stderr
|
||||
@@ -150,7 +152,7 @@ func InstallAUR(pkgName string, packageBase string, logWriter io.Writer) error {
|
||||
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())
|
||||
log.Debug("InstallAUR: cloned (%.2fs)", time.Since(start).Seconds())
|
||||
|
||||
makepkgCmdStr := "su - " + sudoUser + " -c 'cd " + tmpDir + " && makepkg -s --noconfirm'"
|
||||
fmt.Fprintf(logWriter, "[cmd] %s\n", makepkgCmdStr)
|
||||
@@ -160,7 +162,7 @@ func InstallAUR(pkgName string, packageBase string, logWriter io.Writer) error {
|
||||
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())
|
||||
log.Debug("InstallAUR: built (%.2fs)", time.Since(start).Seconds())
|
||||
|
||||
pkgFile, err := findPKGFile(tmpDir)
|
||||
if err != nil {
|
||||
@@ -175,7 +177,7 @@ func InstallAUR(pkgName string, packageBase string, logWriter io.Writer) error {
|
||||
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())
|
||||
log.Debug("InstallAUR: done (%.2fs)", time.Since(start).Seconds())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user