Merge branch 'master' into paths-take-files
This commit is contained in:
commit
c3263d495e
7
app.go
7
app.go
@ -72,6 +72,8 @@ type App struct {
|
|||||||
Authors []*Author
|
Authors []*Author
|
||||||
// Copyright of the binary if any
|
// Copyright of the binary if any
|
||||||
Copyright string
|
Copyright string
|
||||||
|
// Reader reader to write input to (useful for tests)
|
||||||
|
Reader io.Reader
|
||||||
// Writer writer to write output to
|
// Writer writer to write output to
|
||||||
Writer io.Writer
|
Writer io.Writer
|
||||||
// ErrWriter writes error output
|
// ErrWriter writes error output
|
||||||
@ -117,6 +119,7 @@ func NewApp() *App {
|
|||||||
BashComplete: DefaultAppComplete,
|
BashComplete: DefaultAppComplete,
|
||||||
Action: helpCommand.Action,
|
Action: helpCommand.Action,
|
||||||
Compiled: compileTime(),
|
Compiled: compileTime(),
|
||||||
|
Reader: os.Stdin,
|
||||||
Writer: os.Stdout,
|
Writer: os.Stdout,
|
||||||
ErrWriter: os.Stderr,
|
ErrWriter: os.Stderr,
|
||||||
}
|
}
|
||||||
@ -160,6 +163,10 @@ func (a *App) Setup() {
|
|||||||
a.Compiled = compileTime()
|
a.Compiled = compileTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if a.Reader == nil {
|
||||||
|
a.Reader = os.Stdin
|
||||||
|
}
|
||||||
|
|
||||||
if a.Writer == nil {
|
if a.Writer == nil {
|
||||||
a.Writer = os.Stdout
|
a.Writer = os.Stdout
|
||||||
}
|
}
|
||||||
|
38
app_test.go
38
app_test.go
@ -433,6 +433,12 @@ func TestApp_Command(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApp_Setup_defaultsReader(t *testing.T) {
|
||||||
|
app := &App{}
|
||||||
|
app.Setup()
|
||||||
|
expect(t, app.Reader, os.Stdin)
|
||||||
|
}
|
||||||
|
|
||||||
func TestApp_Setup_defaultsWriter(t *testing.T) {
|
func TestApp_Setup_defaultsWriter(t *testing.T) {
|
||||||
app := &App{}
|
app := &App{}
|
||||||
app.Setup()
|
app.Setup()
|
||||||
@ -850,6 +856,15 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApp_DefaultStdin(t *testing.T) {
|
||||||
|
app := &App{}
|
||||||
|
app.Setup()
|
||||||
|
|
||||||
|
if app.Reader != os.Stdin {
|
||||||
|
t.Error("Default input reader not set.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApp_DefaultStdout(t *testing.T) {
|
func TestApp_DefaultStdout(t *testing.T) {
|
||||||
app := &App{}
|
app := &App{}
|
||||||
app.Setup()
|
app.Setup()
|
||||||
@ -859,6 +874,29 @@ func TestApp_DefaultStdout(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApp_SetStdin(t *testing.T) {
|
||||||
|
buf := make([]byte, 12)
|
||||||
|
|
||||||
|
app := &App{
|
||||||
|
Name: "test",
|
||||||
|
Reader: strings.NewReader("Hello World!"),
|
||||||
|
Action: func(c *Context) error {
|
||||||
|
_, err := c.App.Reader.Read(buf)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.Run([]string{"help"})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Run error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(buf) != "Hello World!" {
|
||||||
|
t.Error("App did not read input from desired reader.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApp_SetStdout(t *testing.T) {
|
func TestApp_SetStdout(t *testing.T) {
|
||||||
var w bytes.Buffer
|
var w bytes.Buffer
|
||||||
|
|
||||||
|
@ -425,15 +425,17 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "lang, l",
|
Name: "lang",
|
||||||
Value: "english",
|
Aliases: []string{"l"},
|
||||||
Usage: "Language for the greeting",
|
Value: "english",
|
||||||
},
|
Usage: "Language for the greeting",
|
||||||
&cli.StringFlag{
|
},
|
||||||
Name: "config, c",
|
&cli.StringFlag{
|
||||||
Usage: "Load configuration from `FILE`",
|
Name: "config",
|
||||||
},
|
Aliases: []string{"c"},
|
||||||
|
Usage: "Load configuration from `FILE`",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
{
|
{
|
||||||
@ -570,7 +572,8 @@ func main() {
|
|||||||
|
|
||||||
app.Flags = []cli.Flag {
|
app.Flags = []cli.Flag {
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "password, p",
|
Name: "password",
|
||||||
|
Aliases: []string{"p"},
|
||||||
Usage: "password for the mysql database",
|
Usage: "password for the mysql database",
|
||||||
FilePath: "/etc/mysql/password",
|
FilePath: "/etc/mysql/password",
|
||||||
},
|
},
|
||||||
@ -1309,7 +1312,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cli.HelpFlag = &cli.BoolFlag{
|
cli.HelpFlag = &cli.BoolFlag{
|
||||||
Name: "haaaaalp", Aliases: []string{"halp"},
|
Name: "haaaaalp",
|
||||||
|
Aliases: []string{"halp"},
|
||||||
Usage: "HALP",
|
Usage: "HALP",
|
||||||
EnvVars: []string{"SHOW_HALP", "HALPPLZ"},
|
EnvVars: []string{"SHOW_HALP", "HALPPLZ"},
|
||||||
}
|
}
|
||||||
@ -1344,7 +1348,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cli.VersionFlag = &cli.BoolFlag{
|
cli.VersionFlag = &cli.BoolFlag{
|
||||||
Name: "print-version", Aliases: []string{"V"},
|
Name: "print-version",
|
||||||
|
Aliases: []string{"V"},
|
||||||
Usage: "print only the version",
|
Usage: "print only the version",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user