Make available traversal code reusable

This commit is contained in:
Stephen McQuay 2018-03-02 23:23:16 -08:00
parent 3fd56a8276
commit 97de548a26
Signed by untrusted user: sm
GPG Key ID: 4E4B72F479BA3CE5
2 changed files with 27 additions and 17 deletions

View File

@ -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
}

View File

@ -7,7 +7,6 @@ import (
"net/http"
"os"
"path/filepath"
"sort"
"github.com/pkg/errors"
"mcquay.me/fs"
@ -56,23 +55,9 @@ 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]
for m := range db.Traverse() {
fmt.Fprintf(w, "%v\t%v\t%v\n", m.Name, m.Version, m.Remote.String())
}
}
return nil
}