12 var removeArchives *bool
14 func processPath(curPath string) error {
15 info, err := os.Stat(curPath)
17 return fmt.Errorf("Unable to stat %s: %s", curPath, err.Error())
20 entries, err := os.ReadDir(curPath)
22 return fmt.Errorf("Unable to call readdir on %s: %s", curPath, err.Error())
24 for i := range(entries) {
25 err := processPath(filepath.Join(curPath, entries[i].Name()))
31 tarBaseName := tarBaseName(curPath)
32 if tarBaseName != "" {
33 err = applyTarOn(curPath)
37 err = processPath(filepath.Join(filepath.Dir(curPath), tarBaseName))
42 err = os.Remove(curPath)
44 return fmt.Errorf("Unable to remove archive file %s: %s", curPath, err.Error())
46 fmt.Printf("== rm %s\n", curPath)
53 func tarBaseName(curPath string) string {
54 if strings.HasSuffix(curPath, ".tgz") {
55 return filepath.Base(curPath[:len(curPath)-4])
60 func applyTarOn(curPath string) error {
61 fmt.Printf("== decompressing tar file %s\n", curPath)
62 cmd := exec.Command("tar", "xvf", filepath.Base(curPath))
63 cmd.Dir = filepath.Dir(curPath)
64 //cmd.Stdout = os.Stdout
67 return fmt.Errorf("error running tar xvf on %s: %s", curPath, err.Error())
73 removeArchives = flag.Bool("r", false, "Remove archive files after decompressing them")
75 fmt.Printf("recursive_decompress: recursively decompresses a path.\n")
76 fmt.Printf("usage: recursive_decompress [flags] [paths...]\n")
82 if flag.Arg(0) != "" {
85 err := processPath(curPath)
87 fmt.Printf("error: %s\n", err.Error())