Skip to content

Commit 51af369

Browse files
committed
Add Confirm helper
Signed-off-by: apostasie <[email protected]>
1 parent 3aa8f4f commit 51af369

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

cmd/nerdctl/builder.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/docker/go-units"
2626
"github.com/spf13/cobra"
2727

28+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2829
"github.com/containerd/nerdctl/v2/pkg/api/types"
2930
"github.com/containerd/nerdctl/v2/pkg/cmd/builder"
3031
)
@@ -71,23 +72,16 @@ func builderPruneAction(cmd *cobra.Command, _ []string) error {
7172
}
7273

7374
if !options.Force {
74-
var (
75-
confirm string
76-
msg string
77-
)
75+
var msg string
7876

7977
if options.All {
8078
msg = "This will remove all build cache."
8179
} else {
8280
msg = "This will remove any dangling build cache."
8381
}
84-
msg += " Are you sure you want to continue? [y/N] "
8582

86-
fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
87-
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)
88-
89-
if strings.ToLower(confirm) != "y" {
90-
return nil
83+
if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed {
84+
return err
9185
}
9286
}
9387

cmd/nerdctl/compose_rm.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/spf13/cobra"
2424

25+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2526
"github.com/containerd/nerdctl/v2/pkg/clientutil"
2627
"github.com/containerd/nerdctl/v2/pkg/cmd/compose"
2728
"github.com/containerd/nerdctl/v2/pkg/composer"
@@ -51,18 +52,15 @@ func composeRemoveAction(cmd *cobra.Command, args []string) error {
5152
return err
5253
}
5354
if !force {
54-
var confirm string
5555
services := "all"
5656
if len(args) != 0 {
5757
services = strings.Join(args, ",")
5858
}
59+
5960
msg := fmt.Sprintf("This will remove all stopped containers from services: %s.", services)
60-
msg += "\nAre you sure you want to continue? [y/N] "
61-
fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
62-
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)
6361

64-
if strings.ToLower(confirm) != "y" {
65-
return nil
62+
if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed {
63+
return err
6664
}
6765
}
6866

cmd/nerdctl/container_prune.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
package main
1818

1919
import (
20-
"fmt"
21-
"strings"
22-
2320
"github.com/spf13/cobra"
2421

22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2523
"github.com/containerd/nerdctl/v2/pkg/api/types"
2624
"github.com/containerd/nerdctl/v2/pkg/clientutil"
2725
"github.com/containerd/nerdctl/v2/pkg/cmd/container"
@@ -59,15 +57,7 @@ func grantPrunePermission(cmd *cobra.Command) (bool, error) {
5957
}
6058

6159
if !force {
62-
var confirm string
63-
msg := "This will remove all stopped containers."
64-
msg += "\nAre you sure you want to continue? [y/N] "
65-
fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
66-
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)
67-
68-
if strings.ToLower(confirm) != "y" {
69-
return false, nil
70-
}
60+
return helpers.Confirm(cmd, "WARNING! This will remove all stopped containers.")
7161
}
7262
return true, nil
7363
}

cmd/nerdctl/helpers/prompt.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package helpers
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/spf13/cobra"
24+
)
25+
26+
func Confirm(cmd *cobra.Command, message string) (bool, error) {
27+
message += "\nAre you sure you want to continue? [y/N] "
28+
_, err := fmt.Fprint(cmd.OutOrStdout(), message)
29+
if err != nil {
30+
return false, err
31+
}
32+
33+
var confirm string
34+
_, err = fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)
35+
if err != nil {
36+
return false, err
37+
}
38+
return strings.ToLower(confirm) == "y", err
39+
}

cmd/nerdctl/image_prune.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package main
1818

1919
import (
2020
"fmt"
21-
"strings"
2221

2322
"github.com/spf13/cobra"
2423

24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2525
"github.com/containerd/nerdctl/v2/pkg/api/types"
2626
"github.com/containerd/nerdctl/v2/pkg/clientutil"
2727
"github.com/containerd/nerdctl/v2/pkg/cmd/image"
@@ -82,22 +82,15 @@ func imagePruneAction(cmd *cobra.Command, _ []string) error {
8282
}
8383

8484
if !options.Force {
85-
var (
86-
confirm string
87-
msg string
88-
)
85+
var msg string
8986
if !options.All {
9087
msg = "This will remove all dangling images."
9188
} else {
9289
msg = "This will remove all images without at least one container associated to them."
9390
}
94-
msg += "\nAre you sure you want to continue? [y/N] "
9591

96-
fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
97-
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)
98-
99-
if strings.ToLower(confirm) != "y" {
100-
return nil
92+
if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed {
93+
return err
10194
}
10295
}
10396

0 commit comments

Comments
 (0)