Skip to content

Commit 77fba72

Browse files
committed
Fixes docker-archive-public#2349 - rm get user confirmation before proceeding further
* adds new flag `-y` prompting for user confirmation before removal * Modified existing integration tests to work with the fix docker-archive-public#2349. * Added tests for checking user confirmation, updated the test cases use sub-shell with `|` * Updated the reference docs for rm sub-command incorporated changes by @dgageot, @jeanlaurent and @nathanleclaire Signed-off-by: Anil Belur <askb23@gmail.com>
1 parent 650bd33 commit 77fba72

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

commands/commands.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ var Commands = []cli.Command{
239239
Name: "force, f",
240240
Usage: "Remove local configuration even if machine cannot be removed",
241241
},
242+
cli.BoolFlag{
243+
Name: "y",
244+
Usage: "Assumes automatic yes to proceed with remove, without prompting further user confirmation",
245+
},
242246
},
243247
Name: "rm",
244248
Usage: "Remove a machine",

commands/rm.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ func cmdRm(c CommandLine, api libmachine.API) error {
1414
}
1515

1616
force := c.Bool("force")
17+
confirm := c.Bool("y")
1718

1819
for _, hostName := range c.Args() {
1920
h, err := api.Load(hostName)
2021
if err != nil {
2122
return fmt.Errorf("Error removing host %q: %s", hostName, err)
2223
}
2324

25+
if !confirm && !force {
26+
userinput, err := confirmInput(fmt.Sprintf("Do you really want to remove %q?", hostName))
27+
if !userinput {
28+
return err
29+
}
30+
}
31+
2432
if err := h.Driver.Remove(); err != nil {
2533
if !force {
2634
log.Errorf("Provider error removing machine %q: %s", hostName, err)

commands/rm_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func TestCmdRmMissingMachineName(t *testing.T) {
2323
func TestCmdRm(t *testing.T) {
2424
commandLine := &commandstest.FakeCommandLine{
2525
CliArgs: []string{"machineToRemove1", "machineToRemove2"},
26+
LocalFlags: &commandstest.FakeFlagger{
27+
Data: map[string]interface{}{
28+
"y": true,
29+
},
30+
},
2631
}
2732
api := &libmachinetest.FakeAPI{
2833
Hosts: []*host.Host{

docs/get-started-cloud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ the last section. If we look at `docker-machine ls`, we'll see it is now the
8282
To remove a host and all of its containers and images, use `docker-machine rm`:
8383

8484
$ docker-machine rm dev staging
85+
Do you really want to remove "dev"? (y/n): y
86+
Successfully removed dev
87+
Do you really want to remove "staging"? (y/n): y
88+
Successfully removed staging
89+
8590
$ docker-machine ls
8691
NAME ACTIVE DRIVER STATE URL
8792

docs/reference/rm.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ on the cloud provider or virtualization management platform.
1818
NAME ACTIVE DRIVER STATE URL
1919
foo0 - virtualbox Running tcp://192.168.99.105:2376
2020
foo1 - virtualbox Running tcp://192.168.99.106:2376
21+
2122
$ docker-machine rm foo1
23+
Do you really want to remove "foo1"? (y/n): y
24+
Successfully removed foo1
25+
2226
$ docker-machine ls
2327
NAME ACTIVE DRIVER STATE URL
2428
foo0 - virtualbox Running tcp://192.168.99.105:2376

test/integration/cli/create-rm.bats

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,53 @@ load ${BASE_TEST_DIR}/helpers.bash
6464
}
6565

6666
@test "none: rm with no name fails 'machine rm'" {
67-
run machine rm
67+
run machine rm -y
6868
last=$(expr ${#lines[@]} - 1)
6969
[ "$status" -eq 1 ]
7070
[[ ${lines[$last]} == "Error: Expected to get one or more machine names as arguments" ]]
7171
}
7272

7373
@test "none: rm non existent machine fails 'machine rm ∞'" {
74-
run machine rm ∞
74+
run machine rm ∞ -y
7575
[ "$status" -eq 1 ]
7676
[[ ${lines[0]} == "Error removing host \"\": Host does not exist: \"\"" ]]
7777
}
7878

7979
@test "none: rm is successful 'machine rm 0'" {
80-
run machine rm 0
80+
run machine rm 0 -y
8181
[ "$status" -eq 0 ]
8282
}
8383

84+
@test "none: rm ask user confirmation when -y is not provided 'echo y | machine rm ba'" {
85+
run machine create -d none --url none ba
86+
[ "$status" -eq 0 ]
87+
run bash -c "echo y | machine rm ba"
88+
[ "$status" -eq 0 ]
89+
}
90+
91+
@test "none: rm deny user confirmation when -y is not provided 'echo n | machine rm ab'" {
92+
run machine create -d none --url none ab
93+
[ "$status" -eq 0 ]
94+
run bash -c "echo n | machine rm ab"
95+
[ "$status" -eq 0 ]
96+
}
97+
98+
@test "none: rm never prompt user confirmation with -f is provided 'echo n | machine rm -f ab'" {
99+
run machine create -d none --url none c
100+
[ "$status" -eq 0 ]
101+
run bash -c "machine rm -f c"
102+
[ "$status" -eq 0 ]
103+
[[ ${lines[0]} == "Successfully removed c" ]]
104+
}
105+
84106
# Should be replaced by the test below
85107
@test "none: rm is successful 'machine rm a'" {
86-
run machine rm a
108+
run machine rm a -y
87109
[ "$status" -eq 0 ]
88110
}
89111

90112
@test "none: rm is case insensitive 'machine rm A'" {
91113
skip
92-
run machine rm A
114+
run machine rm A -y
93115
[ "$status" -eq 0 ]
94116
}

test/integration/cli/ls.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ setup () {
99
}
1010

1111
teardown () {
12-
machine rm $(machine ls -q)
12+
machine rm -y $(machine ls -q)
1313
echo_to_log
1414
}
1515

test/integration/run-bats.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function quiet_run () {
1515

1616
function cleanup_machines() {
1717
if [[ $(machine ls -q | wc -l) -ne 0 ]]; then
18-
quiet_run machine rm -f $(machine ls -q)
18+
quiet_run machine rm -y -f $(machine ls -q)
1919
fi
2020
}
2121

0 commit comments

Comments
 (0)