From 9da07a667a769d30be1841b39e29ae5f8e95d3e7 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 5 Oct 2025 11:26:17 -0500 Subject: only keep newest packages --- wit.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/wit.go b/wit.go index 756b774..ebc3fc5 100644 --- a/wit.go +++ b/wit.go @@ -4,6 +4,9 @@ import ( "bufio" "os" "strings" + + "go.wit.com/lib/fhelp" + "go.wit.com/log" ) func (m *Machine) IsInstalled(name string) bool { @@ -52,6 +55,22 @@ func (m *Machine) FindVersion(name string) string { return "" } +func (m *Machine) FindInstalledPackage(check *Package) *Package { + /* + for p := range m.Packages.IterByName() { + if (check.Name == p.Name) && (check.Version == p.Version) { + return p + } + } + */ + for p := range m.Wit.IterByName() { + if (check.Name == p.Name) && (check.Version == p.Version) { + return p + } + } + return nil +} + // looks to see if any package matches a name and version // if version == "", return the first name found func (m *Machine) FindByVersion(name string, version string) *Package { @@ -93,6 +112,29 @@ func (m *Machine) InitWitMirrors() error { return err } +// adds the package if the name isn't there +// if it's newer, remove the old package and use the new one +func (all *Packages) AddIfNewer(p *Package) { + check := all.FindByName(p.Name) + if check == nil { + all.Append(p) + return + } + v1, _ := fhelp.NewDebVersion(check.Version) + v2, _ := fhelp.NewDebVersion(p.Version) + if v1.Equal(v2) { + // log.Info("do nothing", v1, v2) + return + } + if v1.LessThan(v2) { + log.Info("removing", v1, "using", v2) + all.Delete(check) + all.Append(p) + } else { + log.Info("keeping", v1, "got:", v2) + } +} + // breaks up the apt list file into sections // then sends each section to be processed // and added to zoopb.Machine.Wit @@ -108,7 +150,7 @@ func (m *Machine) scanPackageListFile(filename string) error { line := scanner.Text() if line == "" { p := parsePackageInfo(debInfo) - m.Wit.Append(p) + m.Wit.AddIfNewer(p) debInfo = "" } debInfo += line + "\n" -- cgit v1.2.3