Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 897bf85

Browse files
teddy-codesNathan Potter
authored and
Nathan Potter
committed
Add --with-data flag to remove project directory on rm
1 parent a529966 commit 897bf85

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

rmcmd.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"os"
7+
"path/filepath"
78
"time"
89

910
"go.coder.com/cli"
@@ -14,8 +15,9 @@ import (
1415
type rmcmd struct {
1516
gf *globalFlags
1617

17-
repoArg string
18-
all bool
18+
repoArg string
19+
all bool
20+
withData bool
1921
}
2022

2123
func (c *rmcmd) Spec() cli.CommandSpec {
@@ -29,22 +31,22 @@ or all of the containers on a system with the -all flag.`,
2931
}
3032

3133
func (c *rmcmd) RegisterFlags(fl *flag.FlagSet) {
32-
fl.BoolVar(&c.all, "all", false, "Remove all sail containers.")
34+
fl.BoolVar(&c.all, "all", false, "Remove all Sail containers.")
35+
fl.BoolVar(&c.withData, "with-data", false, "Remove the cloned repository's directory.")
3336
}
3437

3538
func (c *rmcmd) Run(fl *flag.FlagSet) {
36-
c.gf.ensureDockerDaemon()
37-
3839
c.repoArg = fl.Arg(0)
3940

4041
if c.repoArg == "" && !c.all {
4142
fl.Usage()
4243
os.Exit(1)
4344
}
4445

45-
names := c.getRemovalList()
46+
c.gf.ensureDockerDaemon()
4647

47-
removeContainers(names...)
48+
names := c.getRemovalList()
49+
c.removeContainers(names...)
4850
}
4951

5052
// getRemovalList returns a list of container names that should be removed.
@@ -74,7 +76,7 @@ func (c *rmcmd) getRemovalList() []string {
7476
return names
7577
}
7678

77-
func removeContainers(names ...string) {
79+
func (c *rmcmd) removeContainers(names ...string) {
7880
cli := dockerClient()
7981
defer cli.Close()
8082

@@ -87,7 +89,14 @@ func removeContainers(names ...string) {
8789
flog.Error("failed to remove %s: %v", name, err)
8890
continue
8991
}
90-
92+
if c.withData {
93+
root := c.gf.config().ProjectRoot
94+
path := filepath.Join(root, c.repoArg)
95+
err = os.RemoveAll(path)
96+
if err != nil {
97+
flog.Error("Failed to remove cloned directory: %v", err)
98+
}
99+
}
91100
flog.Info("removed %s", name)
92101
}
93102
}

0 commit comments

Comments
 (0)