Skip to content

Commit

Permalink
fix colored output (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 authored May 3, 2020
1 parent d7c5f24 commit 08b6f64
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
12 changes: 12 additions & 0 deletions app/cli_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func newCLICallCommand(flags *flags, ui cui.UI) *cobra.Command {
" $ evans -r cli call -f in.json --enrich --output json api.Service.Unary # enrich output with JSON format",
}, "\n"),
RunE: runFunc(flags, func(cmd *cobra.Command, cfg *mergedConfig) error {
if cfg.REPL.ColoredOutput {
ui = cui.NewColored(ui)
}

args := cmd.Flags().Args()
if len(args) == 0 {
return errors.New("method is required")
Expand Down Expand Up @@ -69,6 +73,10 @@ list lists method names belong to the service. If not, list lists all services.`
` $ evans -r cli list api.Service # list all methods belong to service "api.Service"`,
}, "\n"),
RunE: runFunc(flags, func(cmd *cobra.Command, cfg *mergedConfig) error {
if cfg.REPL.ColoredOutput {
ui = cui.NewColored(ui)
}

var dsn string
args := cmd.Flags().Args()
if len(args) > 0 {
Expand Down Expand Up @@ -105,6 +113,10 @@ The symbol should be a fully-qualified name. If no symbol is passed, desc shows
` $ evans -r cli desc api.Request # describe the message descriptor of "api.Request"`,
}, "\n"),
RunE: runFunc(flags, func(cmd *cobra.Command, cfg *mergedConfig) error {
if cfg.REPL.ColoredOutput {
ui = cui.NewColored(ui)
}

var fqn string
args := cmd.Flags().Args()
if len(args) > 0 {
Expand Down
15 changes: 14 additions & 1 deletion app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,17 @@ func newCLICommand(flags *flags, ui cui.UI) *cobra.Command {
cmd := &cobra.Command{
Use: "cli",
Short: "CLI mode",
RunE: runFunc(flags, func(cmd *cobra.Command, cfg *mergedConfig) error {
RunE: runFunc(flags, func(cmd *cobra.Command, cfg *mergedConfig) (err error) {
if cfg.REPL.ColoredOutput {
ui = cui.NewColored(ui)
}

defer func() {
if err == nil {
ui.Warn("evans: deprecated usage, please use sub-commands. see `evans -h` for more details.")
}
}()

// For backward-compatibility.
// If the method is specified by passing --call option, use it.
call := cfg.call
Expand Down Expand Up @@ -225,6 +235,9 @@ func newREPLCommand(flags *flags, ui cui.UI) *cobra.Command {
Use: "repl [options ...]",
Short: "REPL mode",
RunE: runFunc(flags, func(_ *cobra.Command, cfg *mergedConfig) error {
if cfg.REPL.ColoredOutput {
ui = cui.NewColored(ui)
}
return runREPLCommand(cfg, ui)
}),
SilenceErrors: true,
Expand Down
48 changes: 30 additions & 18 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/fatih/color"
"github.com/google/go-cmp/cmp"
"github.com/ktr0731/evans/app"
"github.com/ktr0731/evans/cui"
Expand Down Expand Up @@ -64,6 +65,8 @@ func TestE2E_CLI(t *testing.T) {
// But, if it is prefer to show as it is, you can it by specifying
// unflatten to true.
unflatten bool

deprecatedUsage bool
}{
"print usage text to the Writer (common flag)": {
commonFlags: "--help",
Expand Down Expand Up @@ -154,10 +157,11 @@ func TestE2E_CLI(t *testing.T) {
expectedCode: 1,
},
"call unary RPC with an input file by CLI mode (backward-compatibility)": {
commonFlags: "--package api --service Example --proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
expectedOut: `{ "message": "hello, oumae" }`,
commonFlags: "--package api --service Example --proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
},
"call unary RPC with an input file by CLI mode": {
commonFlags: "--proto testdata/test.proto",
Expand All @@ -172,16 +176,18 @@ func TestE2E_CLI(t *testing.T) {
expectedOut: `{ "message": "hello, oumae" }`,
},
"call unary RPC with --call flag (backward-compatibility)": {
commonFlags: "--package api --service Example --proto testdata/test.proto",
cmd: "",
args: "--file testdata/unary_call.in --call Unary",
expectedOut: `{ "message": "hello, oumae" }`,
commonFlags: "--package api --service Example --proto testdata/test.proto",
cmd: "",
args: "--file testdata/unary_call.in --call Unary",
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
},
"call unary RPC without package name because the size of packages is 1 (backward-compatibility)": {
commonFlags: "--service Example --proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
expectedOut: `{ "message": "hello, oumae" }`,
commonFlags: "--service Example --proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
},
"call unary RPC without package and service name because the size of packages and services are 1": {
commonFlags: "--proto testdata/test.proto",
Expand Down Expand Up @@ -270,11 +276,12 @@ func TestE2E_CLI(t *testing.T) {
expectedCode: 1,
},
"call unary RPC by CLI mode with reflection with an input file (backward-compatibility)": {
commonFlags: "--reflection --package api --service Example",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
reflection: true,
expectedOut: `{ "message": "hello, oumae" }`,
commonFlags: "--reflection --package api --service Example",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
reflection: true,
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
},
"call unary RPC by CLI mode with reflection with an input file": {
commonFlags: "--reflection",
Expand Down Expand Up @@ -642,7 +649,12 @@ func TestE2E_CLI(t *testing.T) {
if c.expectedOut != "" && actual != c.expectedOut {
t.Errorf("unexpected output:\n%s", cmp.Diff(c.expectedOut, actual))
}
if eoutBuf.String() != "" {
eout := eoutBuf.String()
if c.deprecatedUsage {
// Trim "deprecated" message.
eout = strings.Replace(eout, color.YellowString("evans: deprecated usage, please use sub-commands. see `evans -h` for more details.")+"\n", "", -1)
}
if eout != "" {
t.Errorf("expected code is 0, but got an error message: '%s'", eoutBuf.String())
}
}
Expand Down

0 comments on commit 08b6f64

Please sign in to comment.