Skip to content
Browse files

cleanup, sanity checks, start work on destPath option

  • Loading branch information...
1 parent b3c2c11 commit 32da2a6be791c189d6096e78c367a1ad440b6047 @falconindy committed Aug 30, 2010
Showing with 21 additions and 7 deletions.
  1. +21 −7 gobble.go
View
28 gobble.go
@@ -24,6 +24,7 @@ type archpkg struct {
var (
help = flag.Bool("h", false, "display this help message")
cachePath = flag.String("c", PKGCACHE, "path to package cache")
+ destPath = flag.String("m", "", "move files to this location")
keep = flag.Int("k", 3, "number of packages to keep")
delete = flag.Bool("d", false, "delete packages over keep limit")
quiet = flag.Bool("q", false, "don't print deletion candidates")
@@ -68,6 +69,12 @@ func parsePkg(file string) *archpkg {
return &archpkg{pkgname, pkgver, ext[0], ext[1]}
}
+func movePkg(pkg *archpkg) os.Error {
+ /* try to rename, first */
+ filename := fmt.Sprintf("%s-%s-%s.%s", pkg.Name, pkg.Version, pkg.Arch, pkg.Ext)
+ return os.Rename(fmt.Sprintf("%s/%s", *cachePath, filename), fmt.Sprintf("%s/%s", *destPath, filename))
+}
+
func addPkgToMap(pkg *archpkg) {
pkgKey := fmt.Sprintf("%s$%s", pkg.Name, pkg.Arch)
if _, ok := pkgList[pkgKey]; !ok {
@@ -136,9 +143,18 @@ func main() {
flag.Usage()
}
- if *delete && os.Getuid() != 0 {
- fmt.Fprintln(os.Stderr, "error: cannot delete as non-root user.")
- os.Exit(1)
+ /* Sanity checks */
+ if *delete && *destPath != "" {
+ fmt.Fprintln(os.Stderr, "error: cannot delete and move in same operation")
+ os.Exit(5)
+ }
+ if (*delete || *destPath != "") && os.Getuid() != 0 {
+ fmt.Fprintln(os.Stderr, "error: cannot delete/move as non-root user")
+ os.Exit(5)
+ }
+ if *destPath != ""; _, err := os.Stat(*destPath); err != nil {
+ fmt.Fprintf(os.Stderr, "error: cannot stat destination: %s\n", err.String())
+ os.Exit(5)
}
d, err := os.Open(*cachePath, os.O_RDONLY, 0666)
@@ -154,9 +170,7 @@ func main() {
os.Exit(1)
}
- if !*delete {
- fmt.Println("Performing dry-run...")
- }
+ if !*delete { fmt.Println("Performing dry-run...") }
found := 0
for _, pkg := range fi {
@@ -175,7 +189,7 @@ func main() {
}
g, p := prune()
- if *delete {
+ if *delete || *destPath != "" {
fmt.Printf("%d files pruned from %d package groups.\n", p, g)
} else {
fmt.Printf("%d files would be pruned from %d package groups.\n", p, g)

0 comments on commit 32da2a6

Please sign in to comment.
Something went wrong with that request. Please try again.