From 97de548a26fc20a83c2a4b93fa336470cbfb7bd6 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Fri, 2 Mar 2018 23:23:16 -0800 Subject: [PATCH] Make available traversal code reusable --- available.go | 25 +++++++++++++++++++++++++ db/available.go | 19 ++----------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/available.go b/available.go index 7d71bdd..574760a 100644 --- a/available.go +++ b/available.go @@ -2,6 +2,7 @@ package pm import ( "net/url" + "sort" "github.com/pkg/errors" ) @@ -62,3 +63,27 @@ func (a Available) SetRemote(u url.URL) { } } } + +func (a Available) Traverse() <-chan Meta { + r := make(chan Meta) + go func() { + names := Names{} + nvs := map[Name]Versions{} + for n, vers := range a { + names = append(names, n) + for v := range vers { + nvs[n] = append(nvs[n], v) + } + sort.Sort(nvs[n]) + } + sort.Sort(names) + + for _, n := range names { + for _, v := range nvs[n] { + r <- a[n][v] + } + } + close(r) + }() + return r +} diff --git a/db/available.go b/db/available.go index 5ac49ab..d78d194 100644 --- a/db/available.go +++ b/db/available.go @@ -7,7 +7,6 @@ import ( "net/http" "os" "path/filepath" - "sort" "github.com/pkg/errors" "mcquay.me/fs" @@ -56,22 +55,8 @@ func ListAvailable(root string, w io.Writer) error { if err != nil { return errors.Wrap(err, "loading") } - names := pm.Names{} - nvs := map[pm.Name]pm.Versions{} - for n, vers := range db { - names = append(names, n) - for v := range vers { - nvs[n] = append(nvs[n], v) - } - sort.Sort(nvs[n]) - } - sort.Sort(names) - - for _, n := range names { - for _, v := range nvs[n] { - m := db[n][v] - fmt.Fprintf(w, "%v\t%v\t%v\n", m.Name, m.Version, m.Remote.String()) - } + for m := range db.Traverse() { + fmt.Fprintf(w, "%v\t%v\t%v\n", m.Name, m.Version, m.Remote.String()) } return nil }