Skip to content

Commit 9cb729f

Browse files
committed
Add an exit command
1 parent 8b6c1b4 commit 9cb729f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

commands/exit.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)