Browse Source

Split up command line interface into cmd/fdroidcl

pull/8/head
Daniel Martí 11 years ago
parent
commit
b2a36fa18c
  1. 4
      .gitignore
  2. 32
      cmd/fdroidcl/main.go
  3. 22
      index.go

4
.gitignore vendored

@ -1,5 +1,5 @@
# Output binary
fdroidcl
# Binaries
cmd/fdroidcl/fdroidcl
# Indexes
*.xml

32
main.go → 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 <repo address>] <command> [<args>]")
p()
p("Available commands:")
p(" update Update the index")
p(" list List all available apps")
p(" search <term...> Search available apps")
p(" show <appid...> Show detailed info of an app")
p(" update Update the index")
p(" list List all available apps")
p(" search <term...> Search available apps")
p(" show <appid...> 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)

22
index.go

@ -1,7 +1,7 @@
/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* 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)
}

Loading…
Cancel
Save