diff --git a/cmd/clean.go b/cmd/clean.go index 0af445624..b54882b3e 100644 --- a/cmd/clean.go +++ b/cmd/clean.go @@ -17,6 +17,7 @@ package cmd import ( "github.com/fanux/sealos/install" "github.com/spf13/cobra" + "os" ) // cleanCmd represents the clean command @@ -25,6 +26,10 @@ var cleanCmd = &cobra.Command{ Short: "Simplest way to clean your kubernets HA cluster", Long: `sealos clean`, Run: func(cmd *cobra.Command, args []string) { + if !install.CleanForce { // flase + result := install.Confirm(`clean command will clean all masters and nodes, continue clean (y/n)?`) + if !result { os.Exit(0) } + } deleteNodes := install.ParseIPs(install.NodeIPs) deleteMasters := install.ParseIPs(install.MasterIPs) c := &install.SealConfig{} @@ -40,7 +45,7 @@ func init() { // Here you will define your flags and configuration settings. cleanCmd.Flags().StringSliceVar(&install.NodeIPs, "node", []string{}, "clean node ips.kubernetes multi-nodes ex. 192.168.0.5-192.168.0.5") cleanCmd.Flags().StringSliceVar(&install.MasterIPs, "master", []string{}, "clean master ips.kubernetes multi-nodes ex. 192.168.0.5-192.168.0.5") - + cleanCmd.PersistentFlags().BoolVarP(&install.CleanForce, "force","f", false, "A help for foo") // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: // cleanCmd.PersistentFlags().String("foo", "", "A help for foo") diff --git a/go.sum b/go.sum index c07253978..3092b6c72 100644 --- a/go.sum +++ b/go.sum @@ -42,7 +42,7 @@ github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZ github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= -github.com/fanux/sealgate v0.0.4 h1:vfAQIGIzfQfvcG0qfkPcgXG9xeXu6Z6Bv2d7WjUwh3k= +github.com/fanux/sealgate v0.0.4 h1:mvn4j3hNIdravxL3/eeyuO/z/F2BQDjbTFM3Winx0OQ= github.com/fanux/sealgate v0.0.4/go.mod h1:y3141RLsCSgid4+H0O+7CoAqIHDGB0r0mcR8TYnqvps= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= diff --git a/install/clean.go b/install/clean.go index 0d0d93259..1907a9f6c 100644 --- a/install/clean.go +++ b/install/clean.go @@ -6,6 +6,7 @@ import ( "github.com/wonderivan/logger" "strings" "sync" + ) //BuildClean is diff --git a/install/utils.go b/install/utils.go index 8fa6ce456..8253745cc 100644 --- a/install/utils.go +++ b/install/utils.go @@ -3,6 +3,7 @@ package install import ( "fmt" "github.com/cuisongliu/sshcmd/pkg/filesize" + "github.com/wonderivan/logger" "math/big" "math/rand" "net" @@ -13,8 +14,6 @@ import ( "strings" "sync" "time" - - "github.com/wonderivan/logger" ) const oneMBByte = 1024 * 1024 @@ -238,6 +237,33 @@ func ParseIPs(ips []string) []string { return hosts } +// like y|yes|Y|YES return true +func GetConfirmResult(str string) bool { + return YesRx.MatchString(str) +} + +// send the prompt and get result +func Confirm(prompt string) bool { + var ( + inputStr string + err error + ) + _, err = fmt.Fprint(os.Stdout, prompt) + if err != nil { + logger.Error("fmt.Fprint err", err) + os.Exit(-1) + } + + _, err = fmt.Scanf("%s", inputStr) + if err != nil { + logger.Error("fmt.Scanf err", err) + os.Exit(-1) + } + + return GetConfirmResult(inputStr) +} + + func SliceRemoveStr(ss []string, s string) (result []string) { for _, v := range ss { if v != s { diff --git a/install/vars.go b/install/vars.go index 7a4bcc709..465bf2a77 100644 --- a/install/vars.go +++ b/install/vars.go @@ -1,6 +1,9 @@ package install -import "github.com/cuisongliu/sshcmd/pkg/sshutil" +import ( + "github.com/cuisongliu/sshcmd/pkg/sshutil" + "regexp" +) var ( MasterIPs []string @@ -21,4 +24,8 @@ var ( WithoutCNI bool //network interface name, like "eth.*|en.*" Interface string + + YesRx = regexp.MustCompile("^(?i:y(?:es)?)$") + + CleanForce bool )