-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
91 lines (75 loc) · 1.92 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
var skipFoundCheck bool
var quiet bool
var dryrun bool
var help = `rmvcsdir - remove version control directories
The version control directories for Git, SVN, Bzr, and Mercurial are recursively
removed from the filesystem. For example:
$ rmvcsdir ./vendor
Deleting: ./vendor/github.com/Masterminds/semver/.git
Deleting: ./vendor/github.com/Masterminds/vcs/.git
Deleting: ./vendor/github.com/codegangsta/cli/.git
Deleting: ./vendor/gopkg.in/yaml.v2/.git
One or more directories can be passed in. For example:
$ rmvcsdir ~/Code/myproj ~/Code/myproj2/vendor
Options:`
func init() {
flag.BoolVar(&skipFoundCheck, "skip-check", false, "Skip checking if locations exist.")
flag.BoolVar(&quiet, "quiet", false, "Do not display output unless an error occurs.")
flag.BoolVar(&dryrun, "dryrun", false, "Display locations to delete without deleting them.")
}
func main() {
flag.Parse()
args := flag.Args()
// If nothing was passed in display help
if len(args) == 0 {
fmt.Println(help)
flag.PrintDefaults()
return
}
if !skipFoundCheck {
// Make sure the passed in locations exist before proceeding.
found := true
for _, dir := range args {
_, err := os.Stat(dir)
if err != nil {
fmt.Println("Unable to find directory:", dir)
found = false
}
}
if !found {
os.Exit(1)
}
}
for _, dir := range args {
err := filepath.Walk(dir, handler)
if err != nil {
fmt.Printf("Error walking %s: %s", dir, err)
os.Exit(2)
}
}
}
func handler(path string, info os.FileInfo, err error) error {
name := info.Name()
if name == ".git" || name == ".bzr" || name == ".svn" || name == ".hg" {
if _, err := os.Stat(path); err == nil {
if info.IsDir() {
if !quiet || dryrun {
fmt.Println("Deleting:", path)
}
if !dryrun {
err := os.RemoveAll(path)
return err
}
return nil
}
}
}
return nil
}