From f07b5a7d0eba0ae1ab94e2e473f34f7e25ce2350 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Tue, 6 Mar 2018 20:35:19 -0800 Subject: [PATCH] Adds simplest Removable check This will also eventually need some satisfiability love. --- installed.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/installed.go b/installed.go index df4a711..a7e5831 100644 --- a/installed.go +++ b/installed.go @@ -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 }