Add safety check for package list validation

This commit is contained in:
AI Bot
2026-05-03 14:30:16 +02:00
committed by Riyyi
parent 38d26fd2d8
commit 5ad29767c9
6 changed files with 57 additions and 10 deletions
+12 -1
View File
@@ -13,10 +13,21 @@ import (
"github.com/Riyyi/declpac/pkg/pacman/sync"
)
func Sync(packages []string) (*output.Result, error) {
func Sync(packages []string, noCheck bool) (*output.Result, error) {
start := time.Now()
log.Debug("Sync: starting...")
explicitList, err := read.ExplicitList()
if err != nil {
return nil, err
}
explicitCount := len(explicitList)
if !noCheck && len(packages) < explicitCount/2 {
errMsg := "safety check: state packages (%d) less than half of explicitly installed (%d), override with --nocheck"
return nil, fmt.Errorf(errMsg, len(packages), explicitCount)
}
list, err := read.List()
if err != nil {
return nil, err
+19
View File
@@ -35,6 +35,25 @@ func List() ([]string, error) {
return list, 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 ListOrphans() ([]string, error) {
start := time.Now()
log.Debug("ListOrphans: starting...")