Created
January 30, 2017 09:56
-
-
Save sdvcrx/897b29fcbb8adab356ed0c3a8d1c8e09 to your computer and use it in GitHub Desktop.
Cleanup cached pkg files on Archlinux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"regexp" | |
"sort" | |
) | |
func deleteFile(path string) { | |
err := os.Remove(path) | |
if err != nil { | |
log.Println(err) | |
} | |
fmt.Println("Delete " + path) | |
} | |
func main() { | |
dir := "/var/cache/pacman/pkg/" | |
list := make(map[string]int) | |
const RESERVE_VERSION = 2 | |
files := make([]string, 0) | |
raw_files, err := ioutil.ReadDir(dir) | |
if err != nil { | |
fmt.Println(err) | |
} | |
reg, _ := regexp.Compile("(.+?)-[0-9].*-(x86_64|any).pkg.tar.xz") | |
for _, file := range raw_files { | |
if !file.IsDir() { | |
files = append(files, file.Name()) | |
} | |
} | |
sort.Sort(sort.Reverse(sort.StringSlice(files))) | |
for _, file := range files { | |
if reg.MatchString(file) { | |
result := reg.FindStringSubmatch(file) | |
// fmt.Println(result[0]) | |
programName := result[1] | |
if val, ok := list[programName]; ok { | |
if val >= RESERVE_VERSION { | |
go deleteFile(dir + result[0]) | |
} else { | |
list[programName] = val + 1 | |
} | |
} else { | |
list[programName] = 1 | |
} | |
} | |
} | |
fmt.Println("Click any key to continue") | |
var str string | |
fmt.Scanln(&str) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment