-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
cmd_rm.go
69 lines (57 loc) · 1.09 KB
/
cmd_rm.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
package main
import (
"fmt"
"os"
"github.com/urfave/cli/v2"
)
func doRm(c *cli.Context) error {
var (
name = c.Args().First()
dry = c.Bool("dry-run")
w = c.App.Writer
bare = c.Bool("bare")
)
if name == "" {
return fmt.Errorf("repository name is required")
}
u, err := newURL(name, false, true)
if err != nil {
return err
}
localRepo, err := LocalRepositoryFromURL(u, bare)
if err != nil {
return err
}
p := localRepo.FullPath
ok, err := isNotExistOrEmpty(p)
if err != nil {
return err
}
if ok {
return fmt.Errorf("directory %q does not exist", p)
}
if dry {
fmt.Fprintf(w, "Would remove %s\n", p)
return nil
}
ok, err = confirm(fmt.Sprintf("Remove %s?", p))
if err != nil {
return err
}
if !ok {
return fmt.Errorf("aborted")
}
if err := os.RemoveAll(p); err != nil {
return err
}
fmt.Fprintf(w, "Removed %s\n", p)
return nil
}
func confirm(message string) (bool, error) {
fmt.Fprintf(os.Stderr, "%s [y/N]: ", message)
var response string
if _, err := fmt.Scanln(&response); err != nil {
return false, err
}
return response == "y", nil
}