mirror of
https://github.com/tailscale/tailscale.git
synced 2025-06-29 11:38:39 +00:00
util/prompt: make yes/no prompt reusable
Updates #19445 Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
parent
b3e74367d8
commit
9309760263
@ -28,6 +28,7 @@ import (
|
|||||||
"tailscale.com/ipn/ipnstate"
|
"tailscale.com/ipn/ipnstate"
|
||||||
"tailscale.com/tailcfg"
|
"tailscale.com/tailcfg"
|
||||||
"tailscale.com/util/mak"
|
"tailscale.com/util/mak"
|
||||||
|
"tailscale.com/util/prompt"
|
||||||
"tailscale.com/util/slicesx"
|
"tailscale.com/util/slicesx"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
)
|
)
|
||||||
@ -757,7 +758,7 @@ func (e *serveEnv) removeWebServe(sc *ipn.ServeConfig, dnsName string, srvPort u
|
|||||||
|
|
||||||
if len(mounts) > 1 {
|
if len(mounts) > 1 {
|
||||||
msg := fmt.Sprintf("Are you sure you want to delete %d handlers under port %s?", len(mounts), portStr)
|
msg := fmt.Sprintf("Are you sure you want to delete %d handlers under port %s?", len(mounts), portStr)
|
||||||
if !e.yes && !promptYesNo(msg) {
|
if !e.yes && !prompt.YesNo(msg) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,10 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/peterbourgon/ff/v3/ffcli"
|
"github.com/peterbourgon/ff/v3/ffcli"
|
||||||
"tailscale.com/clientupdate"
|
"tailscale.com/clientupdate"
|
||||||
|
"tailscale.com/util/prompt"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
"tailscale.com/version/distro"
|
"tailscale.com/version/distro"
|
||||||
)
|
)
|
||||||
@ -87,19 +87,5 @@ func confirmUpdate(ver string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg := fmt.Sprintf("This will update Tailscale from %v to %v. Continue?", version.Short(), ver)
|
msg := fmt.Sprintf("This will update Tailscale from %v to %v. Continue?", version.Short(), ver)
|
||||||
return promptYesNo(msg)
|
return prompt.YesNo(msg)
|
||||||
}
|
|
||||||
|
|
||||||
// PromptYesNo takes a question and prompts the user to answer the
|
|
||||||
// question with a yes or no. It appends a [y/n] to the message.
|
|
||||||
func promptYesNo(msg string) bool {
|
|
||||||
fmt.Print(msg + " [y/n] ")
|
|
||||||
var resp string
|
|
||||||
fmt.Scanln(&resp)
|
|
||||||
resp = strings.ToLower(resp)
|
|
||||||
switch resp {
|
|
||||||
case "y", "yes", "sure":
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
@ -172,6 +172,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
|
|||||||
tailscale.com/util/multierr from tailscale.com/control/controlhttp+
|
tailscale.com/util/multierr from tailscale.com/control/controlhttp+
|
||||||
tailscale.com/util/must from tailscale.com/clientupdate/distsign+
|
tailscale.com/util/must from tailscale.com/clientupdate/distsign+
|
||||||
tailscale.com/util/nocasemaps from tailscale.com/types/ipproto
|
tailscale.com/util/nocasemaps from tailscale.com/types/ipproto
|
||||||
|
tailscale.com/util/prompt from tailscale.com/cmd/tailscale/cli
|
||||||
tailscale.com/util/quarantine from tailscale.com/cmd/tailscale/cli
|
tailscale.com/util/quarantine from tailscale.com/cmd/tailscale/cli
|
||||||
tailscale.com/util/rands from tailscale.com/tsweb
|
tailscale.com/util/rands from tailscale.com/tsweb
|
||||||
tailscale.com/util/set from tailscale.com/derp+
|
tailscale.com/util/set from tailscale.com/derp+
|
||||||
|
24
util/prompt/prompt.go
Normal file
24
util/prompt/prompt.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
// Package prompt provides a simple way to prompt the user for input.
|
||||||
|
package prompt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// YesNo takes a question and prompts the user to answer the
|
||||||
|
// question with a yes or no. It appends a [y/n] to the message.
|
||||||
|
func YesNo(msg string) bool {
|
||||||
|
fmt.Print(msg + " [y/n] ")
|
||||||
|
var resp string
|
||||||
|
fmt.Scanln(&resp)
|
||||||
|
resp = strings.ToLower(resp)
|
||||||
|
switch resp {
|
||||||
|
case "y", "yes", "sure":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user