Adds simplest Removable check

This will also eventually need some satisfiability love.
This commit is contained in:
Stephen McQuay 2018-03-06 20:35:19 -08:00
parent bd06d484c2
commit f07b5a7d0e
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
1 changed files with 42 additions and 1 deletions

View File

@ -2,7 +2,9 @@ package pm
import (
"errors"
"fmt"
"sort"
"strings"
)
// Installed tracks installed packages.
@ -28,5 +30,44 @@ func (i Installed) Traverse() <-chan Meta {
// Removable calculates if the packages requested in "in" can all be removed.
func (i Installed) Removable(names []string) (Metas, error) {
return nil, errors.New("NYI")
inm := map[Name]bool{}
// XXX (sm): here we simply check if the package exists; eventually we'll
// have to check transitive dependencies, and deal with explicitly and
// implicitly installed packages.
found := map[Name]Meta{}
for _, name := range names {
n := Name(name)
inm[n] = true
if m, ok := i[n]; ok {
found[n] = m
}
}
if len(found) > len(inm) {
return nil, errors.New("should not have been able to find more than asked for, but did; internals are inconsistent.")
} else if len(inm) > len(found) {
// user asked for something that isn't installed.
missing := []string{}
for _, name := range names {
if _, ok := found[Name(name)]; !ok {
missing = append(missing, name)
}
}
return nil, fmt.Errorf("packages not installed: %v", strings.Join(missing, ", "))
}
if len(found) != len(inm) {
return nil, fmt.Errorf("escapes logic")
}
// XXX (sm): the ordering here will also eventually depend on transitive
// dependencies.
r := Metas{}
for _, m := range found {
r = append(r, m)
}
return r, nil
}