cli

package
v0.0.0-...-b439704 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2017 License: BSD-3-Clause Imports: 7 Imported by: 15

Documentation

Overview

Package cli provides utilities for CLI programs.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAbortedByUser = errors.New("aborted by user")
)

Functions

func AccessFile

func AccessFile(filename string) (os.FileInfo, error)

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

func NewTTYReader() (*os.File, error)

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

func NewTTYWriter() (*os.File, error)

NewTTYReader allocates an extra tty that can be used to write.

func ParseFileList

func ParseFileList(args []string, ignoreInvalid bool) ([]string, error)

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

func PromptOverride(filename string) error

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

type Input struct {
	*os.File
	// contains filtered or unexported fields
}

Input is a wrapper for os.File for input, and the underlying file may be stdin.

func AccessOpenFile

func AccessOpenFile(filename string) (*Input, os.FileInfo, error)

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

func AccessOpenFileBuffered(filename string) (*Input, os.FileInfo, error)

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.

func (*Input) Close

func (i *Input) Close() error

Close the underlying *os.File. If *os.File is os.Stdin, remove the underlying temp file.

func (*Input) IsStdin

func (i *Input) IsStdin() bool

type Output

type Output struct {
	*os.File
	// contains filtered or unexported fields
}

Output is a wrapper for os.File for output, and the underlying file may be stdout.

func PromptOverrideCreate

func PromptOverrideCreate(filename string) (*Output, error)

Short cut for PromptOverrideOpen with os.Create.

func PromptOverrideOpen

func PromptOverrideOpen(filename string, flag int, perm os.FileMode) (*Output, error)

Same as PromptOverride, but also open the file with flag and perm passed to os.OpenFile. When filename is "-", write to stdout.

func (*Output) Close

func (o *Output) Close() error

Close the underlying *os.File. If the *os.File is os.Stdout, it will not actually close the stdout.

func (*Output) IsStdout

func (o *Output) IsStdout() bool

Directories

Path Synopsis
Extra syscalls for Windows
Extra syscalls for Windows

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL