File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ package commands
2+
3+ import (
4+ "bufio"
5+ "fmt"
6+ "os"
7+ "strings"
8+
9+ "github.com/spf13/cobra"
10+ )
11+
12+ // Exit returns a command to exit the console application.
13+ // The command will prompt the user to confirm quitting.
14+ func Exit () * cobra.Command {
15+ exitCmd := & cobra.Command {
16+ Use : "exit" ,
17+ Short : "Exit the console application" ,
18+ GroupID : "core" ,
19+ Run : func (_ * cobra.Command , _ []string ) {
20+ exitCtrlD ()
21+ },
22+ }
23+
24+ return exitCmd
25+ }
26+
27+ // exitCtrlD is a custom interrupt handler to use when the shell
28+ // readline receives an io.EOF error, which is returned with CtrlD.
29+ func exitCtrlD () {
30+ reader := bufio .NewReader (os .Stdin )
31+
32+ fmt .Print ("Confirm exit (Y/y): " )
33+
34+ text , _ := reader .ReadString ('\n' )
35+ answer := strings .TrimSpace (text )
36+
37+ if (answer == "Y" ) || (answer == "y" ) {
38+ os .Exit (0 )
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments