-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathclean.go
111 lines (100 loc) · 2 KB
/
clean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2015, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"fmt"
"os"
"path/filepath"
"syscall"
)
var cmdClean = &Command{
UsageLine: "clean",
Short: "Clean index and/or cache",
Long: `
Clean index and/or cache.
The index basically contains information about available APKs to download.
The cache is where all downloaded APKs are cached.
Usage:
$ fdroidcl clean
$ fdroidcl clean index
$ fdroidcl clean cache
`[1:],
}
func init() {
cmdClean.Run = runClean
}
func runClean(args []string) error {
if len(args) > 1 {
return fmt.Errorf("wrong amount of arguments")
}
if len(args) == 1 && (args[0] != "index" && args[0] != "cache") {
return fmt.Errorf("pass either index or cache as parameter, or no parameter at all")
}
if len(args) == 0 || args[0] == "index" {
err := cleanIndex()
if err != nil {
return err
}
}
if len(args) == 0 || args[0] == "cache" {
err := cleanCache()
if err != nil {
return err
}
}
if len(args) == 1 {
fmt.Printf("Cleaned %s.\n", args[0])
} else {
fmt.Println("Cleaned index and cache.")
}
return nil
}
func cleanIndex() error {
cachePath := filepath.Join(mustCache(), "cache-gob")
err := removeFile(cachePath)
if err != nil {
return err
}
err = removeGlob(mustData() + "/*.jar")
if err != nil {
return err
}
err = removeGlob(mustData() + "/*.jar-etag")
if err != nil {
return err
}
return nil
}
func cleanCache() error {
apksDir := subdir(mustCache(), "apks")
err := os.RemoveAll(apksDir)
if err != nil {
return err
}
return nil
}
func removeFile(path string) error {
err := os.Remove(path)
if err != nil {
e, ok := err.(*os.PathError)
if ok && e.Err == syscall.ENOENT {
// The file didn't exist, ignore
} else {
return err
}
}
return nil
}
func removeGlob(glob string) error {
matches, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, value := range matches {
err := removeFile(value)
if err != nil {
return err
}
}
return nil
}