Allow disabling colors via NO_COLOR environment variable

This commit is contained in:
Kevin van Zonneveld 2016-02-16 22:19:40 +01:00
parent 915f858d5e
commit d365e8dcea
5 changed files with 38 additions and 1 deletions

View File

@ -61,6 +61,7 @@ For more information on SemVer, please visit [http://semver.org](http://semver.o
### v1.2.0 (Unreleased)
- Allow disabling colors via `NO_COLOR` environment variable
- Enable errexit, nounset and pipefail at the top
- More refined colors (thanks @arathai)
- Add Changelog

View File

@ -39,6 +39,7 @@ set -o pipefail
# Environment variables and their defaults
LOG_LEVEL="${LOG_LEVEL:-6}" # 7 = debug -> 0 = emergency
NO_COLOR="${NO_COLOR:-}" # true = disable color. otherwise autodetected
# Commandline options. This defines the usage page, and is used to parse cli
# opts & defaults from. The parsing is unforgiving so be precise in your syntax
@ -80,7 +81,7 @@ function _fmt () {
local color="${!colorvar:-$color_error}"
local color_reset="\x1b[0m"
if [[ "${TERM:-}" != "xterm"* ]] || [ -t 1 ]; then
if [ "${NO_COLOR}" = "true" ] || [[ "${TERM:-}" != "xterm"* ]] || [ -t 1 ]; then
# Don't use colors on pipes or non-recognized terminals
color=""; color_reset=""
fi

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,19 @@
B3BP:STDIO_REPLACE_DATETIMES
{datetime} UTC [ debug] cli arg arg_f = () -> {tmpdir}/x
{datetime} UTC [ info] __file: {root}/main.sh
{datetime} UTC [ info] __dir: {root}
{datetime} UTC [ info] __base: main
{datetime} UTC [ info] __os: {os}
{datetime} UTC [ info] arg_f: {tmpdir}/x
{datetime} UTC [ info] arg_d: 0
{datetime} UTC [ info] arg_v: 0
{datetime} UTC [ info] arg_h: 0
{datetime} UTC [ debug] Info useful to developers for debugging the application, not useful during operations.
{datetime} UTC [ info] Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required.
{datetime} UTC [ notice] Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required.
{datetime} UTC [ warning] Warning messages, not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full - each item must be resolved within a given time. This is a debug message
{datetime} UTC [ error] Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time.
{datetime} UTC [ critical] Should be corrected immediately, but indicates failure in a primary system, an example is a loss of a backup ISP connection.
{datetime} UTC [ alert] Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection.
{datetime} UTC [emergency] A "panic" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call.
{datetime} UTC [ info] Cleaning up. Done

View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -o pipefail
set -o errexit
set -o nounset
# set -o xtrace
# Set magic variables for current FILE & DIR
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname $(dirname $(dirname "${__dir}")))" && pwd)"
echo "B3BP:STDIO_REPLACE_DATETIMES"
env LOG_LEVEL=8 NO_COLOR=true bash "${__root}/main.sh" -f /tmp/x