fix: bad line length character caused by printing to stdout

using stderr instead
This commit is contained in:
juancwu 2026-01-12 18:06:02 -05:00
commit e334a472b5
2 changed files with 19 additions and 9 deletions

View file

@ -23,9 +23,19 @@ func startEphemeralAgent(pemData []byte, target string) (string, func(), error)
if err != nil { if err != nil {
if _, ok := err.(*ssh.PassphraseMissingError); ok { if _, ok := err.(*ssh.PassphraseMissingError); ok {
fmt.Printf("\033[1;32mGosh:\033[0m Key for \033[1m%s\033[0m is encrypted. Enter passphrase: ", target) var promptWriter *os.File = os.Stderr
pass, readErr := term.ReadPassword(int(syscall.Stdin)) var inputFd int = int(syscall.Stdin)
fmt.Println()
tty, ttyErr := getTTY()
if ttyErr == nil {
defer tty.Close()
promptWriter = tty
inputFd = int(tty.Fd())
}
fmt.Fprintf(promptWriter, "\033[1;32mGosh:\033[0m Key for \033[1m%s\033[0m is encrypted. Enter passphrase: ", target)
pass, readErr := term.ReadPassword(inputFd)
fmt.Fprintln(promptWriter)
if readErr != nil { if readErr != nil {
return "", nil, fmt.Errorf("failed to read password: %w", err) return "", nil, fmt.Errorf("failed to read password: %w", err)
} }
@ -38,7 +48,7 @@ func startEphemeralAgent(pemData []byte, target string) (string, func(), error)
return "", nil, fmt.Errorf("invalid key format: %w", err) return "", nil, fmt.Errorf("invalid key format: %w", err)
} }
} else { } else {
fmt.Printf("Using unencrypted key for %s\n", target) fmt.Fprintf(os.Stderr, "Using unencrypted key for %s\n", target)
} }
keyring := agent.NewKeyring() keyring := agent.NewKeyring()
@ -93,7 +103,7 @@ func startSSH(storePath string, args []string) {
socketPath, cleanup, err := startEphemeralAgent(pemData, targetName) socketPath, cleanup, err := startEphemeralAgent(pemData, targetName)
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1) os.Exit(1)
} }
defer cleanup() defer cleanup()
@ -110,7 +120,7 @@ func startSSH(storePath string, args []string) {
sshPath, err := exec.LookPath("ssh") sshPath, err := exec.LookPath("ssh")
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1) os.Exit(1)
} }
@ -132,7 +142,7 @@ func startSSH(storePath string, args []string) {
err = sshCmd.Run() err = sshCmd.Run()
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Fprintln(os.Stderr, "Error:", err)
if exitErr, ok := err.(*exec.ExitError); ok { if exitErr, ok := err.(*exec.ExitError); ok {
os.Exit(exitErr.ExitCode()) os.Exit(exitErr.ExitCode())
} }

View file

@ -30,8 +30,8 @@ func getStorePath(customPath string) string {
localDataDir := filepath.Join(home, ".local", "share", "gosh") localDataDir := filepath.Join(home, ".local", "share", "gosh")
err := os.MkdirAll(localDataDir, 0700) err := os.MkdirAll(localDataDir, 0700)
if err != nil { if err != nil {
fmt.Println("Warning: Failed to create local data direction '", localDataDir, "': ", err) fmt.Fprintln(os.Stderr, "Warning: Failed to create local data direction '", localDataDir, "': ", err)
fmt.Println("Warning: Putting database in current working directory.") fmt.Fprintln(os.Stderr, "Warning: Putting database in current working directory.")
return "./keys.json" return "./keys.json"
} }
return filepath.Join(localDataDir, "keys.json") return filepath.Join(localDataDir, "keys.json")