github.com/blend/go-sdk@v1.20220411.3/sh/password.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package sh 9 10 import ( 11 "fmt" 12 "os" 13 "syscall" 14 15 "golang.org/x/term" 16 ) 17 18 // MustPassword gives a prompt and reads input until newlines without printing the input to screen. 19 // The prompt is written to stdout with `fmt.Print` unchanged. 20 // It panics on error. 21 func MustPassword(prompt string) string { 22 output, err := Password(prompt) 23 if err != nil { 24 panic(err) 25 } 26 return output 27 } 28 29 // Password prints a prompt and reads input until newlines without printing the input to screen. 30 // The prompt is written to stdout with `fmt.Print` unchanged. 31 func Password(prompt string) (string, error) { 32 fmt.Fprint(os.Stdout, prompt) 33 results, err := term.ReadPassword(int(syscall.Stdin)) 34 if err != nil { 35 return "", err 36 } 37 fmt.Fprintln(os.Stdout) 38 return string(results), nil 39 } 40 41 // Passwordf gives a prompt and reads input until newlines without printing the input to screen. 42 // The prompt is written to stdout with `fmt.Printf` unchanged. 43 func Passwordf(format string, args ...interface{}) (string, error) { 44 fmt.Fprintf(os.Stdout, format, args...) 45 results, err := term.ReadPassword(int(syscall.Stdin)) 46 if err != nil { 47 return "", err 48 } 49 fmt.Fprintln(os.Stdout) 50 return string(results), nil 51 }