From cc04421f5c61e709e1ddfaa7920be6fd4d6f5978 Mon Sep 17 00:00:00 2001 From: Jan Delgado Date: Fri, 9 Feb 2018 00:30:24 +0100 Subject: [PATCH] new NO_COLOR environment variable to disable color output (equally to --no-color) --- README.md | 13 +++++++++---- app/main/command_line.go | 14 +++++++------- app/main/command_line_test.go | 19 +++++++++++++++++-- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 369330c..44a2766 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Swiss army knife for RabbitMQ. Tap/Pub/Sub messages, create/delete/bind queues and exchanges, inspect broker. -## Contents +## Contents @@ -20,6 +20,7 @@ and exchanges, inspect broker. * [Environment variables](#environment-variables) * [Default RabbitMQ broker](#default-rabbitmq-broker) * [Default RabbitMQ management API endpoint](#default-rabbitmq-management-api-endpoint) + * [Disable color output](#disable-color-output) * [Examples](#examples) * [Broker info](#broker-info) * [Wire-tapping messages](#wire-tapping-messages) @@ -116,7 +117,7 @@ Options: single JSON file. JSON body is base64 encoded. Otherwise metadata and body (as-is) are saved separately. -k, --insecure allow insecure TLS connections (no certificate check). - -n, --no-color don't colorize output. + -n, --no-color don't colorize output (also environment variable NO_COLOR) -r, --routingkey KEY routing key to use in publish mode. --saveto DIR also save messages and metadata to DIR. --show-default include default exchange in output info command. @@ -175,6 +176,10 @@ $ rabtap info ... ``` +#### Disable color output + +Set environment variable `NO_COLOR` to disable color output. + ### Examples The following examples expect a RabbitMQ broker running on localhost:5672 and @@ -258,7 +263,7 @@ Will consume messages from queue `somequeue` and print out messages in JSON format (`-j`). Example assumes that `RABTAP_AMQPURI` environment variable is set. -Note that unlike `tap`, `sub` will consume messages that are in effect +Note that unlike `tap`, `sub` will consume messages that are in effect removed from the specified queue. #### Poor mans shovel @@ -327,7 +332,7 @@ $ make build-all ## Test data generator A simple [test data generator tool](app/testgen/README.md) for manual tests is -included in the `app/testgen` directory. +included in the `app/testgen` directory. ## Author diff --git a/app/main/command_line.go b/app/main/command_line.go index f716b4a..336f9f0 100644 --- a/app/main/command_line.go +++ b/app/main/command_line.go @@ -51,23 +51,23 @@ Options: be read. QUEUE name of a queue. -a, --autodelete create auto delete exchange/queue. - --api APIURI connect to given API server. If APIURI is omitted, + --api APIURI connect to given API server. If APIURI is omitted, the environment variable RABTAP_APIURI will be used. -b, --bindingkey KEY binding key to use in bind queue command. --consumers include consumers in output of info command. -d, --durable create durable exchange/queue. -h, --help print this help. - -j, --json print/save/publish message metadata and body to a - single JSON file. JSON body is base64 encoded. Otherwise - metadata and body (as-is) are saved separately. + -j, --json print/save/publish message metadata and body to a + single JSON file. JSON body is base64 encoded. Otherwise + metadata and body (as-is) are saved separately. -k, --insecure allow insecure TLS connections (no certificate check). - -n, --no-color don't colorize output. + -n, --no-color don't colorize output (also environment variable NO_COLOR) -r, --routingkey KEY routing key to use in publish mode. --saveto DIR also save messages and metadata to DIR. --show-default include default exchange in output info command. --stats include statistics in output of info command. -t, --type TYPE exchange type [default: fanout]. - --uri URI connect to given AQMP broker. If omitted, the + --uri URI connect to given AQMP broker. If omitted, the environment variable RABTAP_AMQPURI will be used. -v, --verbose enable verbose mode. --version show version information and exit. @@ -157,7 +157,7 @@ func parseCommonArgs(args map[string]interface{}) commonArgs { return commonArgs{ Verbose: args["--verbose"].(bool), InsecureTLS: args["--insecure"].(bool), - NoColor: args["--no-color"].(bool), + NoColor: args["--no-color"].(bool) || (os.Getenv("NO_COLOR") != ""), JSONFormat: args["--json"].(bool)} } diff --git a/app/main/command_line_test.go b/app/main/command_line_test.go index aab9447..b9480d5 100644 --- a/app/main/command_line_test.go +++ b/app/main/command_line_test.go @@ -79,7 +79,7 @@ func TestCliTapWithMultipleUris(t *testing.T) { args, err := ParseCommandLineArgs( []string{"tap", "--uri=broker1", "exchange1:binding1,exchange2:binding2", "tap", "--uri=broker2", "exchange3:binding3,exchange4:binding4", - "--saveto", "savedir", "--no-color"}) + "--saveto", "savedir"}) assert.Nil(t, err) assert.Equal(t, TapCmd, args.Cmd) @@ -97,7 +97,7 @@ func TestCliTapWithMultipleUris(t *testing.T) { assert.Equal(t, "exchange4", args.TapConfig[1].Exchanges[1].Exchange) assert.Equal(t, "binding4", args.TapConfig[1].Exchanges[1].BindingKey) assert.Equal(t, "savedir", *args.SaveDir) - assert.True(t, args.NoColor) + assert.False(t, args.NoColor) assert.False(t, args.Verbose) assert.False(t, args.InsecureTLS) } @@ -353,6 +353,7 @@ func TestCliCreateDurableAutodeleteExchange(t *testing.T) { assert.True(t, args.Durable) assert.True(t, args.Autodelete) } + func TestCliRemoveExchange(t *testing.T) { args, err := ParseCommandLineArgs( []string{"exchange", "rm", "name", "--uri", "uri"}) @@ -362,3 +363,17 @@ func TestCliRemoveExchange(t *testing.T) { assert.Equal(t, "name", args.ExchangeName) assert.Equal(t, "uri", args.AmqpURI) } + +func TestParseNoColorFromEnvironment(t *testing.T) { + const key = "NO_COLOR" + os.Setenv(key, "1") + defer os.Unsetenv(key) + + args, err := ParseCommandLineArgs( + []string{"info", "--api=APIURI"}) + + assert.Nil(t, err) + assert.Equal(t, InfoCmd, args.Cmd) + assert.Equal(t, "APIURI", args.APIURI) + assert.True(t, args.NoColor) +}