Brijesh's Git Server — network-scan @ main

utils/confirmation.go (view raw)

 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
package utils

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

func ConfirmBeforeRunning(prompt string, fn func()) {
	reader := bufio.NewReader(os.Stdin)

	for {
		fmt.Printf("%s (Y/n): ", prompt)
		response, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println("Error reading input:", err)
			return
		}

		response = strings.TrimSpace(strings.ToLower(response))

		switch response {
		case "y", "yes", "":
			fn()
			return
		case "n", "no":
			fmt.Println("Operation cancelled.")
			return
		default:
			fmt.Println("Invalid response. Please answer with 'y' or 'n'.")
		}
	}
}