go port
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
// Package cli provides result formatting and interactive confirmation that
|
||||
// reproduce the original kisstron.py presult() and trconfirm() behaviour.
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Result is the outcome of an operation, mirroring the original
|
||||
// {success, message, result} dictionary returned by every kisstron function.
|
||||
type Result struct {
|
||||
Success bool
|
||||
Message string
|
||||
Result any
|
||||
}
|
||||
|
||||
var reader = bufio.NewReader(os.Stdin)
|
||||
|
||||
// formatResult renders the presult() output: a "Success:" or "Error:" header,
|
||||
// the message, and a trailing blank line.
|
||||
func formatResult(r Result) string {
|
||||
header := "Error:"
|
||||
if r.Success {
|
||||
header = "Success:"
|
||||
}
|
||||
return fmt.Sprintf("%s\n%s\n\n", header, r.Message)
|
||||
}
|
||||
|
||||
// PrintResult reproduces presult() exactly.
|
||||
func PrintResult(r Result) {
|
||||
fmt.Print(formatResult(r))
|
||||
}
|
||||
|
||||
// ReadLine prints prompt and reads a single line from stdin.
|
||||
func ReadLine(prompt string) string {
|
||||
fmt.Print(prompt)
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil && line == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimRight(line, "\r\n")
|
||||
}
|
||||
|
||||
// Confirm asks the user to type "yes" and aborts with exit code 0 otherwise,
|
||||
// matching trconfirm().
|
||||
func Confirm(prompt string) {
|
||||
if strings.ToLower(ReadLine(prompt)) != "yes" {
|
||||
fmt.Println("Transaction not confirmed, aborting!")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user