Files
rainyun_api_go_sdk/cmd/ry/internal/config/interactive.go
T
XingfenD f04bad5f18 feat(cli): add interactive config input and Makefile
- support interactive value input for `config set`
- hide API key input and output
- add Makefile for build, test, vet, coverage, and cleanup
- add golang.org/x/term dependency
- update changelog and gitignore
2026-08-13 13:10:53 +08:00

44 lines
971 B
Go

package config
import (
"bufio"
"fmt"
"os"
"golang.org/x/term"
)
// ReadSecret prompts the user with msg and reads a line from stdin.
// When stdin is a terminal, echo is disabled so the typed value stays
// invisible. When stdin is piped (non-TTY) it falls back to a normal
// read so scripting still works.
func ReadSecret(msg string) (string, error) {
fmt.Fprint(os.Stderr, msg)
var value string
if term.IsTerminal(int(os.Stdin.Fd())) {
b, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
value = string(b)
} else {
sc := bufio.NewScanner(os.Stdin)
if !sc.Scan() {
return "", sc.Err()
}
value = sc.Text()
}
fmt.Fprintln(os.Stderr)
return value, nil
}
// ReadLine prompts the user with msg and reads a normal (echo-enabled) line.
func ReadLine(msg string) (string, error) {
fmt.Fprint(os.Stderr, msg)
sc := bufio.NewScanner(os.Stdin)
if !sc.Scan() {
return "", sc.Err()
}
return sc.Text(), nil
}