Make available traversal code reusable
This commit is contained in:
parent
3fd56a8276
commit
97de548a26
25
available.go
25
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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user