This commit is contained in:
Luxferre
2026-09-19 18:26:07 +03:00
parent 8ae332e74d
commit e9826b1ae2
28 changed files with 3417 additions and 1 deletions
+54
View File
@@ -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)
}
}
+39
View File
@@ -0,0 +1,39 @@
package cli
import (
"bufio"
"bytes"
"strings"
"testing"
)
func TestPrintResultSuccess(t *testing.T) {
var buf bytes.Buffer
_ = buf
// PrintResult writes to stdout; capture via the shared writer instead of
// swapping os.Stdout to keep the test hermetic.
got := formatResult(Result{Success: true, Message: "Wallet created"})
want := "Success:\nWallet created\n\n"
if got != want {
t.Fatalf("formatResult = %q, want %q", got, want)
}
}
func TestPrintResultError(t *testing.T) {
got := formatResult(Result{Message: "Invalid private key!"})
want := "Error:\nInvalid private key!\n\n"
if got != want {
t.Fatalf("formatResult = %q, want %q", got, want)
}
}
func TestReadLineTrimsNewline(t *testing.T) {
reader = bufio.NewReader(strings.NewReader("hello\r\n"))
if got := ReadLine(""); got != "hello" {
t.Fatalf("ReadLine = %q, want %q", got, "hello")
}
reader = bufio.NewReader(strings.NewReader("yes\n"))
if got := ReadLine(""); got != "yes" {
t.Fatalf("ReadLine = %q, want %q", got, "yes")
}
}