From faf959420554c021c6351a7111d4fa17288ec4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 19 Jul 2015 21:43:54 -0700 Subject: [PATCH] Start using apps from multiple repos --- cmd/fdroidcl/search.go | 3 +-- cmd/fdroidcl/show.go | 3 +-- cmd/fdroidcl/update.go | 60 +++++++++++++++++++++++++++++------------- index.go | 20 +++++++------- 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/cmd/fdroidcl/search.go b/cmd/fdroidcl/search.go index 720c510..73929b8 100644 --- a/cmd/fdroidcl/search.go +++ b/cmd/fdroidcl/search.go @@ -37,8 +37,7 @@ func runSearch(args []string) { if *installed || *updates { device = mustOneDevice() } - index := mustLoadIndex() - apps := filterAppsSearch(index.Apps, args) + apps := filterAppsSearch(mustLoadIndexes(), args) instPkgs := mustInstalled(device) if *installed { apps = filterAppsInstalled(apps, instPkgs) diff --git a/cmd/fdroidcl/show.go b/cmd/fdroidcl/show.go index f0fbe39..4d1796b 100644 --- a/cmd/fdroidcl/show.go +++ b/cmd/fdroidcl/show.go @@ -41,8 +41,7 @@ func appsMap(apps []fdroidcl.App) map[string]*fdroidcl.App { } func findApps(ids []string) []*fdroidcl.App { - index := mustLoadIndex() - apps := appsMap(index.Apps) + apps := appsMap(mustLoadIndexes()) result := make([]*fdroidcl.App, len(ids)) for i, id := range ids { app, e := apps[id] diff --git a/cmd/fdroidcl/update.go b/cmd/fdroidcl/update.go index 3e6aaef..2435107 100644 --- a/cmd/fdroidcl/update.go +++ b/cmd/fdroidcl/update.go @@ -14,6 +14,7 @@ import ( "net/http" "os" "path/filepath" + "sort" "github.com/mvdan/fdroidcl" ) @@ -43,6 +44,27 @@ func (r *repo) updateIndex() error { return nil } +func (r *repo) loadIndex() (*fdroidcl.Index, error) { + p := indexPath(r.ID) + f, err := os.Open(p) + if err != nil { + return nil, fmt.Errorf("could not open index: %v", err) + } + stat, err := f.Stat() + if err != nil { + return nil, fmt.Errorf("could not stat index: %v", err) + } + //pubkey, err := hex.DecodeString(repoPubkey) + //if err != nil { + // return nil, fmt.Errorf("could not decode public key: %v", err) + //} + index, err := fdroidcl.LoadIndexJar(f, stat.Size(), nil) + if err != nil { + return nil, fmt.Errorf("could not load index: %v", err) + } + return index, nil +} + func respEtag(resp *http.Response) string { etags, e := resp.Header["Etag"] if !e || len(etags) == 0 { @@ -106,24 +128,26 @@ func indexPath(name string) string { return filepath.Join(mustConfig(), name+".jar") } -func mustLoadIndex() *fdroidcl.Index { - r := mustOneRepo() - p := indexPath(r.ID) - f, err := os.Open(p) - if err != nil { - log.Fatalf("Could not open index file: %v", err) - } - stat, err := f.Stat() - if err != nil { - log.Fatalf("Could not stat index file: %v", err) +func mustLoadIndexes() []fdroidcl.App { + var m map[string]*fdroidcl.App + for _, r := range config.Repos { + index, err := r.loadIndex() + if err != nil { + log.Fatalf("Error while loading %s: %v", r.ID, err) + } + for _, a := range index.Apps { + _, e := m[a.ID] + if !e { + m[a.ID] = &a + continue + } + // TODO: merge apks + } } - //pubkey, err := hex.DecodeString(repoPubkey) - //if err != nil { - // log.Fatalf("Could not decode public key: %v", err) - //} - index, err := fdroidcl.LoadIndexJar(f, stat.Size(), nil) - if err != nil { - log.Fatalf("Could not load index: %v", err) + apps := make([]fdroidcl.App, 0, len(m)) + for _, a := range m { + apps = append(apps, *a) } - return index + sort.Sort(fdroidcl.AppList(apps)) + return apps } diff --git a/index.go b/index.go index 2d881cc..93d7db3 100644 --- a/index.go +++ b/index.go @@ -208,17 +208,17 @@ func (a *Apk) SrcURL() string { return fmt.Sprintf("%s/%s", a.Repo.URL, a.SrcName) } -type appList []App +type AppList []App -func (al appList) Len() int { return len(al) } -func (al appList) Swap(i, j int) { al[i], al[j] = al[j], al[i] } -func (al appList) Less(i, j int) bool { return al[i].ID < al[j].ID } +func (al AppList) Len() int { return len(al) } +func (al AppList) Swap(i, j int) { al[i], al[j] = al[j], al[i] } +func (al AppList) Less(i, j int) bool { return al[i].ID < al[j].ID } -type apkList []Apk +type ApkList []Apk -func (al apkList) Len() int { return len(al) } -func (al apkList) Swap(i, j int) { al[i], al[j] = al[j], al[i] } -func (al apkList) Less(i, j int) bool { return al[i].VCode > al[j].VCode } +func (al ApkList) Len() int { return len(al) } +func (al ApkList) Swap(i, j int) { al[i], al[j] = al[j], al[i] } +func (al ApkList) Less(i, j int) bool { return al[i].VCode > al[j].VCode } func LoadIndexXml(r io.Reader) (*Index, error) { var index Index @@ -227,11 +227,11 @@ func LoadIndexXml(r io.Reader) (*Index, error) { return nil, err } - sort.Sort(appList(index.Apps)) + sort.Sort(AppList(index.Apps)) for i := range index.Apps { app := &index.Apps[i] - sort.Sort(apkList(app.Apks)) + sort.Sort(ApkList(app.Apks)) for j := range app.Apks { apk := &app.Apks[j] apk.Repo = &index.Repo