From ce3a45a78cc49065ec2c58e88f93f7bff1b4b691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 17 Jul 2015 23:05:45 -0700 Subject: [PATCH] Reorganize app dir code --- cmd/fdroidcl/install.go | 20 +++----------------- cmd/fdroidcl/main.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/cmd/fdroidcl/install.go b/cmd/fdroidcl/install.go index 4a28281..fb07995 100644 --- a/cmd/fdroidcl/install.go +++ b/cmd/fdroidcl/install.go @@ -7,8 +7,6 @@ import ( "fmt" "log" "path/filepath" - - "github.com/mvdan/basedir" ) var cmdInstall = &Command{ @@ -27,11 +25,10 @@ func runInstall(args []string) { device := mustOneDevice() apps := findApps(args) paths := make([]string, len(apps)) - apksDir := apksCacheDir() for i, app := range apps { apk := app.CurApk url := fmt.Sprintf("%s/%s", repoURL, apk.ApkName) - path := filepath.Join(apksDir, apk.ApkName) + path := apkPath(apk.ApkName) if err := downloadEtag(url, path, apk.Hash.Data); err != nil { log.Fatalf("Could not download '%s': %v", app.ID, err) } @@ -48,18 +45,7 @@ func runInstall(args []string) { } } -func apksCacheDir() string { - cache, err := basedir.Cache() - if err != nil { - log.Fatalf("Could not determine cache dir: %v", err) - } - return appSubdir(cache, "apks") -} - func apkPath(apkname string) string { - cache, err := basedir.Cache() - if err != nil { - log.Fatalf("Could not determine cache dir: %v", err) - } - return filepath.Join(appSubdir(cache, "apks"), apkname) + apksDir := subdir(mustCache(), "apks") + return filepath.Join(apksDir, apkname) } diff --git a/cmd/fdroidcl/main.go b/cmd/fdroidcl/main.go index 6badc40..44f3459 100644 --- a/cmd/fdroidcl/main.go +++ b/cmd/fdroidcl/main.go @@ -8,7 +8,10 @@ import ( "fmt" "log" "os" + "path/filepath" "strings" + + "github.com/mvdan/basedir" ) const ( @@ -18,6 +21,36 @@ const ( repoURL = "https://f-droid.org/repo" ) +func subdir(dir, name string) string { + p := filepath.Join(dir, name) + if err := os.MkdirAll(p, 0755); err != nil { + log.Fatalf("Could not create dir '%s': %v", p, err) + } + return p +} + +func mustCache() string { + dir, err := basedir.Cache() + if err != nil { + log.Fatalf("Could not determine cache dir: %v", err) + } + return subdir(dir, cmdName) +} + +func mustConfig() string { + dir, err := basedir.Config() + if err != nil { + log.Fatalf("Could not determine config dir: %v", err) + } + return subdir(dir, cmdName) +} + +var config struct { + Repos map[string]struct { + URL string `json:"url"` + } `json:"repos"` +} + // A Command is an implementation of a go command // like go build or go fix. type Command struct {