diff --git a/fdroidcl.go b/index.go similarity index 75% rename from fdroidcl.go rename to index.go index 4e2e9fc..3ab0c90 100644 --- a/fdroidcl.go +++ b/index.go @@ -8,14 +8,12 @@ import ( "bytes" "encoding/xml" "errors" - "flag" "fmt" "io" "io/ioutil" "log" "net/http" "os" - "sort" "strings" ) @@ -320,105 +318,3 @@ func loadApps() map[string]App { } return apps } - -func appMatches(fields []string, terms []string) bool { - for _, field := range fields { - for _, term := range terms { - if !strings.Contains(field, term) { - goto next - } - } - return true - next: - } - return false -} - -func filterAppsSearch(apps *map[string]App, terms []string) { - for _, term := range terms { - term = strings.ToLower(term) - } - for appID, app := range *apps { - fields := []string{ - strings.ToLower(app.ID), - strings.ToLower(app.Name), - strings.ToLower(app.Summary), - strings.ToLower(app.Desc), - } - if !appMatches(fields, terms) { - delete(*apps, appID) - } - } -} - -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 sortedApps(apps map[string]App) []App { - list := make(appList, 0, len(apps)) - for appID := range apps { - list = append(list, apps[appID]) - } - sort.Sort(list) - return list -} - -var repoURL = flag.String("r", "https://f-droid.org/repo", "repository address") - -func init() { - flag.Usage = func() { - p := func(args ...interface{}) { - fmt.Fprintln(os.Stderr, args...) - } - 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") - } -} - -func main() { - flag.Parse() - if flag.NArg() == 0 { - flag.Usage() - os.Exit(2) - } - - cmd := flag.Args()[0] - args := flag.Args()[1:] - - switch cmd { - case "update": - updateIndex() - case "list": - apps := loadApps() - for _, app := range sortedApps(apps) { - app.writeShort(os.Stdout) - } - case "search": - apps := loadApps() - filterAppsSearch(&apps, args) - for _, app := range sortedApps(apps) { - app.writeShort(os.Stdout) - } - case "show": - apps := loadApps() - for _, appID := range args { - app, e := apps[appID] - if !e { - log.Fatalf("Could not find app with ID '%s'", appID) - } - app.writeDetailed(os.Stdout) - } - default: - fmt.Fprintf(os.Stderr, "Unrecognised command '%s'\n\n", cmd) - flag.Usage() - os.Exit(2) - } -} diff --git a/main.go b/main.go new file mode 100644 index 0000000..64853a5 --- /dev/null +++ b/main.go @@ -0,0 +1,115 @@ +/* Copyright (c) 2015, Daniel Martí */ +/* See LICENSE for licensing information */ + +package main + +import ( + "flag" + "fmt" + "os" + "sort" + "strings" +) + +func appMatches(fields []string, terms []string) bool { + for _, field := range fields { + for _, term := range terms { + if !strings.Contains(field, term) { + goto next + } + } + return true + next: + } + return false +} + +func filterAppsSearch(apps *map[string]App, terms []string) { + for _, term := range terms { + term = strings.ToLower(term) + } + for appID, app := range *apps { + fields := []string{ + strings.ToLower(app.ID), + strings.ToLower(app.Name), + strings.ToLower(app.Summary), + strings.ToLower(app.Desc), + } + if !appMatches(fields, terms) { + delete(*apps, appID) + } + } +} + +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 sortedApps(apps map[string]App) []App { + list := make(appList, 0, len(apps)) + for appID := range apps { + list = append(list, apps[appID]) + } + sort.Sort(list) + return list +} + +var repoURL = flag.String("r", "https://f-droid.org/repo", "repository address") + +func init() { + flag.Usage = func() { + p := func(args ...interface{}) { + fmt.Fprintln(os.Stderr, args...) + } + 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") + } +} + +func main() { + flag.Parse() + if flag.NArg() == 0 { + flag.Usage() + os.Exit(2) + } + + cmd := flag.Args()[0] + args := flag.Args()[1:] + + switch cmd { + case "update": + updateIndex() + case "list": + apps := loadApps() + for _, app := range sortedApps(apps) { + app.writeShort(os.Stdout) + } + case "search": + apps := loadApps() + filterAppsSearch(&apps, args) + for _, app := range sortedApps(apps) { + app.writeShort(os.Stdout) + } + case "show": + apps := loadApps() + 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) + } + default: + fmt.Fprintf(os.Stderr, "Unrecognised command '%s'\n\n", cmd) + flag.Usage() + os.Exit(2) + } +}