From b2a36fa18cf312b67d3158ea40a93450292b1f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 1 May 2015 12:59:12 +0200 Subject: [PATCH] Split up command line interface into cmd/fdroidcl --- .gitignore | 4 ++-- main.go => cmd/fdroidcl/main.go | 32 ++++++++++++++++++-------------- index.go | 22 +++++++++++++--------- 3 files changed, 33 insertions(+), 25 deletions(-) rename main.go => cmd/fdroidcl/main.go (74%) diff --git a/.gitignore b/.gitignore index 46e4abc..f217302 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -# Output binary -fdroidcl +# Binaries +cmd/fdroidcl/fdroidcl # Indexes *.xml diff --git a/main.go b/cmd/fdroidcl/main.go similarity index 74% rename from main.go rename to cmd/fdroidcl/main.go index 64853a5..1d95ef2 100644 --- a/main.go +++ b/cmd/fdroidcl/main.go @@ -9,6 +9,8 @@ import ( "os" "sort" "strings" + + "github.com/mvdan/fdroidcl" ) func appMatches(fields []string, terms []string) bool { @@ -24,7 +26,7 @@ func appMatches(fields []string, terms []string) bool { return false } -func filterAppsSearch(apps *map[string]App, terms []string) { +func filterAppsSearch(apps *map[string]fdroidcl.App, terms []string) { for _, term := range terms { term = strings.ToLower(term) } @@ -41,13 +43,13 @@ func filterAppsSearch(apps *map[string]App, terms []string) { } } -type appList []App +type appList []fdroidcl.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 sortedApps(apps map[string]App) []App { +func sortedApps(apps map[string]fdroidcl.App) []fdroidcl.App { list := make(appList, 0, len(apps)) for appID := range apps { list = append(list, apps[appID]) @@ -66,13 +68,15 @@ func init() { p("Usage: fdroidcl [-h] [-r ] []") p() p("Available commands:") - p(" update Update the index") - p(" list List all available apps") - p(" search Search available apps") - p(" show Show detailed info of an app") + p(" update Update the index") + p(" list List all available apps") + p(" search Search available apps") + p(" show Show detailed info of an app") } } +const repoName = "index" + func main() { flag.Parse() if flag.NArg() == 0 { @@ -85,27 +89,27 @@ func main() { switch cmd { case "update": - updateIndex() + fdroidcl.UpdateIndex(repoName, *repoURL) case "list": - apps := loadApps() + apps := fdroidcl.LoadApps(repoName) for _, app := range sortedApps(apps) { - app.writeShort(os.Stdout) + app.WriteShort(os.Stdout) } case "search": - apps := loadApps() + apps := fdroidcl.LoadApps(repoName) filterAppsSearch(&apps, args) for _, app := range sortedApps(apps) { - app.writeShort(os.Stdout) + app.WriteShort(os.Stdout) } case "show": - apps := loadApps() + apps := fdroidcl.LoadApps(repoName) for _, appID := range args { app, e := apps[appID] if !e { fmt.Fprintf(os.Stderr, "Could not find app with ID '%s'", appID) os.Exit(1) } - app.writeDetailed(os.Stdout) + app.WriteDetailed(os.Stdout) } default: fmt.Fprintf(os.Stderr, "Unrecognised command '%s'\n\n", cmd) diff --git a/index.go b/index.go index 3ab0c90..f038742 100644 --- a/index.go +++ b/index.go @@ -1,7 +1,7 @@ /* Copyright (c) 2015, Daniel Martí */ /* See LICENSE for licensing information */ -package main +package fdroidcl import ( "archive/zip" @@ -169,12 +169,12 @@ func (app *App) prepareData() { app.calcCurApk() } -func (app *App) writeShort(w io.Writer) { +func (app *App) WriteShort(w io.Writer) { fmt.Fprintf(w, "%s | %s %s\n", app.ID, app.Name, app.CurApk.VName) fmt.Fprintf(w, " %s\n", app.Summary) } -func (app *App) writeDetailed(w io.Writer) { +func (app *App) WriteDetailed(w io.Writer) { p := func(title string, format string, args ...interface{}) { if format == "" { fmt.Fprintln(w, title) @@ -269,12 +269,15 @@ func downloadEtag(url, path string) error { return nil } -const indexName = "index.jar" +func indexPath(repoName string) string { + return repoName + ".jar" +} -func updateIndex() { - url := fmt.Sprintf("%s/%s", *repoURL, indexName) +func UpdateIndex(repoName, repoURL string) { + path := indexPath(repoName) + url := fmt.Sprintf("%s/%s", repoURL, path) log.Printf("Downloading %s", url) - err := downloadEtag(url, indexName) + err := downloadEtag(url, path) if err == ErrNotModified { log.Printf("Index is already up to date") } else if err != nil { @@ -282,8 +285,9 @@ func updateIndex() { } } -func loadApps() map[string]App { - r, err := zip.OpenReader(indexName) +func LoadApps(repoName string) map[string]App { + path := indexPath(repoName) + r, err := zip.OpenReader(path) if err != nil { log.Fatal(err) }