Skip to content

Commit c31585c

Browse files
committed
prompt on destructive ops by default
1 parent e11e14d commit c31585c

8 files changed

+106
-0
lines changed

cmd/cloudImageDelete.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/spf13/cobra"
2223

@@ -29,6 +30,15 @@ var cloudImageDeleteCmd = &cobra.Command{
2930
Long: `Delete a Cloud Image`,
3031
Run: func(cmd *cobra.Command, args []string) {
3132
imageIdFlag, _ := cmd.Flags().GetInt64("image_id")
33+
forceFlag, _ := cmd.Flags().GetBool("force")
34+
35+
// if force flag wasn't passed
36+
if !forceFlag {
37+
// exit if user didn't consent
38+
if proceed := dialoagDesctructiveConfirmProceed(); !proceed {
39+
os.Exit(0)
40+
}
41+
}
3242

3343
apiArgs := map[string]interface{}{"id": imageIdFlag}
3444

@@ -47,5 +57,6 @@ func init() {
4757

4858
cloudImageDeleteCmd.Flags().Int64("image_id", -1,
4959
"id number of the image (see 'cloud image list')")
60+
cloudImageDeleteCmd.Flags().Bool("force", false, "bypass dialog confirmation")
5061
cloudImageDeleteCmd.MarkFlagRequired("image_id")
5162
}

cmd/cloudPrivateParentDelete.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/spf13/cobra"
2223

@@ -34,6 +35,15 @@ Parents you have total control of how many instances can live on the Private Par
3435
as well as how many resources each Cloud Server gets.`,
3536
Run: func(cmd *cobra.Command, args []string) {
3637
nameFlag, _ := cmd.Flags().GetString("name")
38+
forceFlag, _ := cmd.Flags().GetBool("force")
39+
40+
// if force flag wasn't passed
41+
if !forceFlag {
42+
// exit if user didn't consent
43+
if proceed := dialoagDesctructiveConfirmProceed(); !proceed {
44+
os.Exit(0)
45+
}
46+
}
3747

3848
// if passed a private-parent flag, derive its uniq_id
3949
var privateParentUniqId string
@@ -60,6 +70,7 @@ func init() {
6070
cloudPrivateParentCmd.AddCommand(cloudPrivateParentDeleteCmd)
6171

6272
cloudPrivateParentDeleteCmd.Flags().String("name", "", "name or uniq_id of the Private Parent")
73+
cloudPrivateParentDeleteCmd.Flags().Bool("force", false, "bypass dialog confirmation")
6374

6475
cloudPrivateParentDeleteCmd.MarkFlagRequired("name")
6576
}

cmd/cloudServerDestroy.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/spf13/cobra"
2223

@@ -37,6 +38,15 @@ server.`,
3738
Run: func(cmd *cobra.Command, args []string) {
3839
commentFlag, _ := cmd.Flags().GetString("comment")
3940
reasonFlag, _ := cmd.Flags().GetString("reason")
41+
forceFlag, _ := cmd.Flags().GetBool("force")
42+
43+
// if force flag wasn't passed
44+
if !forceFlag {
45+
// exit if user didn't consent
46+
if proceed := dialoagDesctructiveConfirmProceed(); !proceed {
47+
os.Exit(0)
48+
}
49+
}
4050

4151
for _, uniqId := range cloudServerDestroyCmdUniqIdFlag {
4252

@@ -78,6 +88,7 @@ func init() {
7888
"comment related to the cancellation")
7989
cloudServerDestroyCmd.Flags().String("reason", "",
8090
"reason for the cancellation (optional)")
91+
cloudServerDestroyCmd.Flags().Bool("force", false, "bypass dialog confirmation")
8192

8293
cloudServerDestroyCmd.MarkFlagRequired("uniq_id")
8394
}

cmd/cloudStorageBlockVolumeDelete.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/spf13/cobra"
2223

@@ -34,6 +35,15 @@ Once attached, volumes appear as normal block devices, and can be used as such.
3435
`,
3536
Run: func(cmd *cobra.Command, args []string) {
3637
uniqIdFlag, _ := cmd.Flags().GetString("uniq_id")
38+
forceFlag, _ := cmd.Flags().GetBool("force")
39+
40+
// if force flag wasn't passed
41+
if !forceFlag {
42+
// exit if user didn't consent
43+
if proceed := dialoagDesctructiveConfirmProceed(); !proceed {
44+
os.Exit(0)
45+
}
46+
}
3747

3848
validateFields := map[interface{}]interface{}{
3949
uniqIdFlag: "UniqId",
@@ -57,6 +67,7 @@ func init() {
5767
cloudStorageBlockVolumeCmd.AddCommand(cloudStorageBlockVolumeDeleteCmd)
5868

5969
cloudStorageBlockVolumeDeleteCmd.Flags().String("uniq_id", "", "uniq_id of Cloud Block Storage volume")
70+
cloudStorageBlockVolumeDeleteCmd.Flags().Bool("force", false, "bypass dialog confirmation")
6071

6172
cloudStorageBlockVolumeDeleteCmd.MarkFlagRequired("uniq_id")
6273
}

cmd/cloudStorageObjectDelete.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/spf13/cobra"
2223

@@ -30,6 +31,15 @@ var cloudStorageObjectDeleteCmd = &cobra.Command{
3031
Long: `Delete an Object Store`,
3132
Run: func(cmd *cobra.Command, args []string) {
3233
uniqIdFlag, _ := cmd.Flags().GetString("uniq_id")
34+
forceFlag, _ := cmd.Flags().GetBool("force")
35+
36+
// if force flag wasn't passed
37+
if !forceFlag {
38+
// exit if user didn't consent
39+
if proceed := dialoagDesctructiveConfirmProceed(); !proceed {
40+
os.Exit(0)
41+
}
42+
}
3343

3444
validateFields := map[interface{}]interface{}{
3545
uniqIdFlag: "UniqId",
@@ -54,6 +64,7 @@ func init() {
5464
cloudStorageObjectCmd.AddCommand(cloudStorageObjectDeleteCmd)
5565
cloudStorageObjectDeleteCmd.Flags().String("uniq_id", "",
5666
"uniq_id of object store to delete (see 'cloud storage object list')")
67+
cloudStorageObjectDeleteCmd.Flags().Bool("force", false, "bypass dialog confirmation")
5768

5869
cloudStorageObjectDeleteCmd.MarkFlagRequired("uniq_id")
5970
}

cmd/cloudStorageObjectDeleteKey.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/spf13/cobra"
2223

@@ -31,6 +32,15 @@ var cloudStorageObjectDeleteKeyCmd = &cobra.Command{
3132
Run: func(cmd *cobra.Command, args []string) {
3233
uniqIdFlag, _ := cmd.Flags().GetString("uniq_id")
3334
accessKeyFlag, _ := cmd.Flags().GetString("access-key")
35+
forceFlag, _ := cmd.Flags().GetBool("force")
36+
37+
// if force flag wasn't passed
38+
if !forceFlag {
39+
// exit if user didn't consent
40+
if proceed := dialoagDesctructiveConfirmProceed(); !proceed {
41+
os.Exit(0)
42+
}
43+
}
3444

3545
validateFields := map[interface{}]interface{}{
3646
uniqIdFlag: "UniqId",
@@ -59,6 +69,7 @@ func init() {
5969
cloudStorageObjectCmd.AddCommand(cloudStorageObjectDeleteKeyCmd)
6070
cloudStorageObjectDeleteKeyCmd.Flags().String("uniq_id", "", "uniq_id of Object Store")
6171
cloudStorageObjectDeleteKeyCmd.Flags().String("access-key", "", "the access key to remove from the Object Store")
72+
cloudStorageObjectDeleteKeyCmd.Flags().Bool("force", false, "bypass dialog confirmation")
6273

6374
cloudStorageObjectDeleteKeyCmd.MarkFlagRequired("uniq_id")
6475
cloudStorageObjectDeleteKeyCmd.MarkFlagRequired("access-key")

cmd/networkIpPoolDelete.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/spf13/cobra"
2223

@@ -33,6 +34,15 @@ An IP Pool is a range of nonintersecting, reusable IP addresses reserved to
3334
your account.`,
3435
Run: func(cmd *cobra.Command, args []string) {
3536
uniqIdFlag, _ := cmd.Flags().GetString("uniq_id")
37+
forceFlag, _ := cmd.Flags().GetBool("force")
38+
39+
// if force flag wasn't passed
40+
if !forceFlag {
41+
// exit if user didn't consent
42+
if proceed := dialoagDesctructiveConfirmProceed(); !proceed {
43+
os.Exit(0)
44+
}
45+
}
3646

3747
validateFields := map[interface{}]interface{}{
3848
uniqIdFlag: "UniqId",
@@ -58,6 +68,7 @@ func init() {
5868
networkIpPoolCmd.AddCommand(networkIpPoolDeleteCmd)
5969

6070
networkIpPoolDeleteCmd.Flags().String("uniq_id", "", "uniq_id of IP Pool")
71+
networkIpPoolDeleteCmd.Flags().Bool("force", false, "bypass dialog confirmation")
6172

6273
networkIpPoolDeleteCmd.MarkFlagRequired("uniq_id")
6374
}

cmd/root.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19+
"bufio"
1920
"fmt"
2021
"os"
2122
"strings"
@@ -26,6 +27,7 @@ import (
2627

2728
"github.com/liquidweb/liquidweb-cli/instance"
2829
"github.com/liquidweb/liquidweb-cli/types/api"
30+
"github.com/liquidweb/liquidweb-cli/utils"
2931
)
3032

3133
var cfgFile string
@@ -155,3 +157,30 @@ func derivePrivateParentUniqId(name string) (string, error) {
155157

156158
return privateParentUniqId, nil
157159
}
160+
161+
func dialoagDesctructiveConfirmProceed() (proceed bool) {
162+
163+
var haveConfirmationAnswer bool
164+
utils.PrintTeal("Tip: Avoid future confirmations by passing --force\n\n")
165+
166+
for !haveConfirmationAnswer {
167+
utils.PrintRed("This is a destructive operation. Continue (yes/[no])?: ")
168+
scanner := bufio.NewScanner(os.Stdin)
169+
scanner.Scan()
170+
answer := scanner.Text()
171+
172+
if answer != "" && answer != "yes" && answer != "no" {
173+
utils.PrintYellow("invalid input.\n")
174+
continue
175+
}
176+
177+
haveConfirmationAnswer = true
178+
if answer == "no" || answer == "" {
179+
proceed = false
180+
} else if answer == "yes" {
181+
proceed = true
182+
}
183+
}
184+
185+
return
186+
}

0 commit comments

Comments
 (0)