Documentation ¶
Overview ¶
Package cli provides utilities for CLI programs.
Index ¶
- Variables
- func AccessFile(filename string) (os.FileInfo, error)
- func ClearTerminal() error
- func NewTTYReader() (*os.File, error)
- func NewTTYWriter() (*os.File, error)
- func ParseFileList(args []string, ignoreInvalid bool) ([]string, error)
- func PromptOverride(filename string) error
- type Input
- type Output
Constants ¶
This section is empty.
Variables ¶
var (
ErrAbortedByUser = errors.New("aborted by user")
)
Functions ¶
func AccessFile ¶
Access a file (not a directory), return the file info and error. Filename "-" is considered as stdin, and AccessFile will return (nil, nil) in this case.
func ClearTerminal ¶
func ClearTerminal() error
ClearTerminal clears the terminal screen and move cursor to top-left corner.
This function assumes stdout is a tty but it does not check it. You must check that (by calling either github.com/mattn/go-isatty.IsTerminal or golang.org/x/crypto/ssh/terminal.IsTerminal on os.Stdout) on your own before actually calling ClearTerminal.
Unlike other common simple solutions that just calls "cls.exe" or "clear(1)", it calls Console API (Windows) or writes VT100 escape characters (POSIX) directly.
func NewTTYReader ¶
NewTTYReader allocates an extra tty that can be used to read.
NewTTYReader and NewTTYWriter are useful for prompting when stdin/stdout is redirected. Since it returns a *os.File, you can also call golang.org/x/crypto/ssh/terminal.ReadPassword on it! An example:
package main import ( "crypto/subtle" "fmt" "io" "log" "os" "ekyu.moe/util/cli" "golang.org/x/crypto/ssh/terminal" ) func main() { r, err := cli.NewTTYReader() if err != nil { log.Fatal(err) } defer r.Close() w, err := cli.NewTTYWriter() if err != nil { log.Fatal(err) } defer w.Close() // isatty check fmt.Fprintln( w, terminal.IsTerminal(int(os.Stdin.Fd())), terminal.IsTerminal(int(os.Stdout.Fd())), ) fmt.Fprintln( w, terminal.IsTerminal(int(r.Fd())), terminal.IsTerminal(int(w.Fd())), ) // Basic prompt fmt.Fprint(w, "Would you like to continue? [y/N] ") var ans string if fmt.Fscanln(r, &ans); ans != "y" { fmt.Fprintln(w, "You chose to abort.") os.Exit(1) } fmt.Fprintln(w, "You chose to continue.") // Simple passphrase prompt fmt.Fprint(w, "Enter the passphrase: ") passphrase, err := terminal.ReadPassword(int(r.Fd())) fmt.Fprintln(w) if err != nil { log.Fatal(err) } // Of course in real usage you must do hash/kdf if subtle.ConstantTimeCompare(passphrase, []byte("a secret")) == 0 { fmt.Fprintln(w, "Wrong passphrase.") os.Exit(1) } // Do something like encrypt/decrypt or encode/decode through stdin and // stdout. io.Copy(os.Stdout, os.Stdin) }
Run it:
$ go run main.go <<< "raw data blablabla" | cat false false true true Would you like to continue? [y/N] y You chose to continue. Enter the passphrase: raw data blablabla
func NewTTYWriter ¶
NewTTYReader allocates an extra tty that can be used to write.
func ParseFileList ¶
Parse a slice of globs to file name list, considering "-" as stdin/stdout. The only possible returned error is ErrBadPattern for path/filepath.Glob(). When an error is encountered, the currently parsed filenames will be returned. If ignoreInvalid is true, error will always be nil.
func PromptOverride ¶
Check if a file already exists, if true, prompt for overriding. On err == nil, either there is no filename conflict or the user permitted overriding, or the filename is "-", which is considered as stdout.
Types ¶
type Input ¶
Input is a wrapper for os.File for input, and the underlying file may be stdin.
func AccessOpenFile ¶
Same as AccessFile, but also open the file. If filename is "-", read from stdin, and file info will be nil in this case.
func AccessOpenFileBuffered ¶
Same as AccessOpenFile, but buffers stdin to a temp file when filename is "-", and returns the *Input and os.FileInfo based on the temp file. It will resume the stdin after reading an EOF from it, instead of closing stdin.
type Output ¶
Output is a wrapper for os.File for output, and the underlying file may be stdout.
func PromptOverrideCreate ¶
Short cut for PromptOverrideOpen with os.Create.
func PromptOverrideOpen ¶
Same as PromptOverride, but also open the file with flag and perm passed to os.OpenFile. When filename is "-", write to stdout.