aboutsummaryrefslogtreecommitdiff
path: root/internal/utils/cli.go
blob: 46eef78de572f66494ed48b2dee3da77621ab40a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package utils

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func AskUserYesNo(prompt string, defaultVal bool) (bool, error) {
	suffix := "[y/N]"
	if defaultVal {
		suffix = "[Y/n]"
	}

	scanner := bufio.NewScanner(os.Stdin)
	fmt.Printf("%s %s ", prompt, suffix)
	scanner.Scan()
	if err := scanner.Err(); err != nil {
		return false, fmt.Errorf("failed to read confirmation: %w", err)
	}

	input := strings.TrimSpace(scanner.Text())
	if input == "" {
		return defaultVal, nil
	}

	switch strings.ToUpper(input) {
	case "Y", "YE", "YES":
		return true, nil
	default:
		return false, nil
	}
}

func ClearTerm() {
    fmt.Print("\033[H\033[2J")
}