This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No subcommand flags. Testing stdout line length.
No planned sub command flags. Making Config field items non-pointers since FromCLI() is only called once. Adding --target and --user flags to global options. Polishing off CLI help texts. Testing --help line lengths (80 chars max).
- Loading branch information
Showing
4 changed files
with
110 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// Config defines the application configuration. | ||
type Config struct { | ||
LogFile *string `cli:"log"` | ||
Quiet *bool `cli:"quiet"` | ||
Verbose *bool `cli:"verbose"` | ||
LogFile string | ||
Quiet bool | ||
TargetDir string | ||
Username string | ||
Verbose bool | ||
} | ||
|
||
// FromCLI is passed to cli.App{} in the Action field. It populates the GlobalConfig. | ||
func (c *Config) FromCLI(ctx *cli.Context) error { | ||
if s := ctx.String("log"); c.LogFile == nil { | ||
c.LogFile = &s | ||
} | ||
if b := ctx.Bool("quiet"); c.Quiet == nil { | ||
c.Quiet = &b | ||
c.LogFile = ctx.String("log") | ||
c.Quiet = ctx.Bool("quiet") | ||
c.TargetDir = ctx.String("target") | ||
c.Username = ctx.String("user") | ||
c.Verbose = ctx.Bool("verbose") | ||
|
||
// Set defaults. | ||
if c.TargetDir == "" { | ||
c.TargetDir = "ghbackup" | ||
} | ||
if b := ctx.Bool("verbose"); c.Verbose == nil { | ||
c.Verbose = &b | ||
if c.Username == "" { | ||
c.Username = "TODO" | ||
} | ||
return nil | ||
} | ||
|
||
// Finalize resolves remaining nil pointers to empty *values. | ||
func (c *Config) Finalize() { | ||
emptyBool := false | ||
emptyString := "" | ||
structValue := reflect.ValueOf(c).Elem() | ||
for i := 0; i < structValue.NumField(); i++ { | ||
field := structValue.Field(i) | ||
if !field.IsNil() { | ||
continue | ||
} | ||
if field.Type() == reflect.TypeOf(&emptyBool) { | ||
field.Set(reflect.ValueOf(&emptyBool)) | ||
} else if field.Type() == reflect.TypeOf(&emptyString) { | ||
field.Set(reflect.ValueOf(&emptyString)) | ||
} | ||
} | ||
} | ||
|
||
// GlobalConfig will hold the config values for the entire application during runtime. | ||
var GlobalConfig Config |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// From http://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string | ||
func withStdoutRedir(args []string) (*string, error) { | ||
var output string | ||
stdout := make(chan string) | ||
|
||
// Replace args. | ||
oldArgs := os.Args | ||
os.Args = args | ||
defer func() { os.Args = oldArgs }() | ||
|
||
// Replace stream. | ||
old := os.Stdout | ||
r, w, err := os.Pipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
os.Stdout = w | ||
defer func() { | ||
w.Close() | ||
os.Stdout = old | ||
out := <-stdout | ||
output = out | ||
}() | ||
|
||
// Start copy. | ||
go func() { | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
stdout <- buf.String() | ||
}() | ||
|
||
// Run the main function. | ||
main() | ||
|
||
return &output, nil | ||
} | ||
|
||
func TestMain_HelpLineLength(t *testing.T) { | ||
assert := require.New(t) | ||
allArgs := [][]string{ | ||
{"githubBackup", "--help"}, | ||
{"githubBackup", "gist", "--help"}, | ||
{"githubBackup", "github", "--help"}, | ||
{"githubBackup", "all", "--help"}, | ||
} | ||
|
||
for _, args := range allArgs { | ||
output, err := withStdoutRedir(args) | ||
assert.NoError(err) | ||
assert.Contains(*output, "githubBackup") | ||
assert.Contains(*output, "USAGE") | ||
for _, line := range strings.Split(*output, "\n") { | ||
truncated := fmt.Sprintf("%.80s", line) | ||
assert.Equal(truncated, line) | ||
} | ||
} | ||
} |