From 747c6b10762e18bbda24fbb876ac2e88f29dc70b Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Wed, 3 Jun 2020 21:45:04 +0200 Subject: [PATCH 1/7] Respect NO_COLOR environment variable --- cdist/argparse.py | 23 +++++++++-------------- cdist/config.py | 1 + cdist/configuration.py | 28 +++++++++++++++++++--------- cdist/core/manifest.py | 2 +- cdist/emulator.py | 4 ++-- cdist/log.py | 19 ++++++++++--------- cdist/test/configuration/__init__.py | 2 +- configuration/cdist.cfg.skeleton | 6 ++++-- 8 files changed, 47 insertions(+), 38 deletions(-) diff --git a/cdist/argparse.py b/cdist/argparse.py index c30e2030..0782654f 100644 --- a/cdist/argparse.py +++ b/cdist/argparse.py @@ -89,13 +89,6 @@ def check_lower_bounded_int(value, lower_bound, name): return val -def colored_output_type(val): - boolean_states = cdist.configuration.ColoredOutputOption.BOOLEAN_STATES - if val not in boolean_states.keys(): - raise argparse.ArgumentError() - return boolean_states[val] - - def get_parsers(): global parser @@ -140,7 +133,7 @@ def get_parsers(): 'It can be a boolean or "auto" (default) which enables this ' 'feature if stdout is a tty and disables it otherwise.', action='store', dest='colored_output', required=False, - type=colored_output_type) + choices=cdist.configuration.ColoredOutputOption.CHOICES) parser['beta'] = argparse.ArgumentParser(add_help=False) parser['beta'].add_argument( @@ -501,7 +494,12 @@ def handle_loglevel(args): if hasattr(args, 'quiet') and args.quiet: args.verbose = _verbosity_level_off - logging.root.setLevel(_verbosity_level[args.verbose]) + logging.getLogger().setLevel(_verbosity_level[args.verbose]) + + +def handle_log_colors(args): + if cdist.configuration.ColoredOutputOption.translate(args.colored_output): + cdist.log.DefaultLog.USE_COLORS = True def parse_and_configure(argv, singleton=True): @@ -515,16 +513,13 @@ def parse_and_configure(argv, singleton=True): raise cdist.Error(str(e)) # Loglevels are handled globally in here handle_loglevel(args) + handle_log_colors(args) log = logging.getLogger("cdist") - config = cfg.get_config() - if config.get('GLOBAL', {}).get('colored_output', False): - cdist.log.ColorFormatter.USE_COLORS = True - log.verbose("version %s" % cdist.VERSION) log.trace('command line args: {}'.format(cfg.command_line_args)) - log.trace('configuration: {}'.format(config)) + log.trace('configuration: {}'.format(cfg.get_config())) log.trace('configured args: {}'.format(args)) check_beta(vars(args)) diff --git a/cdist/config.py b/cdist/config.py index 97cc1da6..b2d72f05 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -203,6 +203,7 @@ class Config(object): cdist.log.setupParallelLogging() elif args.timestamp: cdist.log.setupTimestampingLogging() + log = logging.getLogger("config") # No new child process if only one host at a time. diff --git a/cdist/configuration.py b/cdist/configuration.py index 6f07c27f..c0fbc063 100644 --- a/cdist/configuration.py +++ b/cdist/configuration.py @@ -48,7 +48,6 @@ _VERBOSITY_VALUES = ( _ARCHIVING_VALUES = ( 'tar', 'tgz', 'tbz2', 'txz', 'none', ) -_COLORED_OUTPUT_DEFAULT = sys.stdout.isatty() class OptionBase: @@ -249,8 +248,22 @@ class LogLevelOption(OptionBase): class ColoredOutputOption(BooleanOption): - BOOLEAN_STATES = dict(configparser.ConfigParser.BOOLEAN_STATES, - auto=_COLORED_OUTPUT_DEFAULT) + CHOICES = tuple(configparser.ConfigParser.BOOLEAN_STATES) + ('auto',) + DEFAULT = 'auto' + + def get_converter(self): + return self.translate + + @staticmethod + def translate(val): + if 'NO_COLOR' in os.environ: + return False + elif isinstance(val, bool): + return val + elif val == 'auto': + return sys.stdout.isatty() + else: + return configparser.ConfigParser.BOOLEAN_STATES[val] _ARG_OPTION_MAPPING = { @@ -337,12 +350,10 @@ class Configuration(metaclass=Singleton): } ARG_OPTION_MAPPING = _ARG_OPTION_MAPPING - ADJUST_ARG_OPTION_MAPPING = { - _ARG_OPTION_MAPPING[key]: key for key in _ARG_OPTION_MAPPING - } + ADJUST_ARG_OPTION_MAPPING = {v: k for k, v in _ARG_OPTION_MAPPING.items()} REQUIRED_DEFAULT_CONFIG_VALUES = { 'GLOBAL': { - 'colored_output': _COLORED_OUTPUT_DEFAULT, + 'colored_output': 'auto', 'verbosity': 0, }, } @@ -495,8 +506,7 @@ class Configuration(metaclass=Singleton): newconfig = self._read_config_file(config_file) self._update_config_dict(config, newconfig) # command line config file - if (self.args and 'config_file' in self.args and - self.args['config_file']): + if (self.args and self.args.get('config_file', None)): newconfig = self._read_config_file(self.args['config_file']) self._update_config_dict(config, newconfig) # command line diff --git a/cdist/core/manifest.py b/cdist/core/manifest.py index 32520e49..8b833ff2 100644 --- a/cdist/core/manifest.py +++ b/cdist/core/manifest.py @@ -119,7 +119,7 @@ class Manifest(object): '__cdist_log_level': util.log_level_env_var_val(self.log), '__cdist_log_level_name': util.log_level_name_env_var_val( self.log), - '__cdist_colored_log': str(cdist.log.ColorFormatter.USE_COLORS), + '__cdist_colored_log': str(self.log.USE_COLORS).lower(), } if dry_run: diff --git a/cdist/emulator.py b/cdist/emulator.py index 87c9fe12..4eaf2c93 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -129,8 +129,8 @@ class Emulator(object): # if invalid __cdist_log_level value logging.root.setLevel(logging.WARNING) - colored_log = self.env.get('__cdist_colored_log', 'False') - cdist.log.ColorFormatter.USE_COLORS = colored_log == 'True' + colored_log = self.env.get('__cdist_colored_log', 'false') + cdist.log.ColorFormatter.USE_COLORS = colored_log == 'true' self.log = logging.getLogger(self.target_host[0]) diff --git a/cdist/log.py b/cdist/log.py index 5f2d8f53..19efebdb 100644 --- a/cdist/log.py +++ b/cdist/log.py @@ -51,7 +51,6 @@ logging.trace = _trace class ColorFormatter(logging.Formatter): - USE_COLORS = False RESET = '\033[0m' COLOR_MAP = { 'ERROR': '\033[0;31m', @@ -62,20 +61,19 @@ class ColorFormatter(logging.Formatter): 'TRACE': '\033[0;37m', } - def __init__(self, msg): - super().__init__(msg) + def __init__(self, fmt): + super().__init__(fmt=fmt) def format(self, record): msg = super().format(record) - if self.USE_COLORS: - color = self.COLOR_MAP.get(record.levelname) - if color: - msg = color + msg + self.RESET + color = self.COLOR_MAP.get(record.levelname) + if color: + msg = color + msg + self.RESET return msg class DefaultLog(logging.Logger): - + USE_COLORS = False FORMAT = '%(levelname)s: %(message)s' class StdoutFilter(logging.Filter): @@ -90,7 +88,10 @@ class DefaultLog(logging.Logger): super().__init__(name) self.propagate = False - formatter = ColorFormatter(self.FORMAT) + if self.USE_COLORS: + formatter = ColorFormatter(self.FORMAT) + else: + formatter = logging.Formatter(self.FORMAT) self.addFilter(self) diff --git a/cdist/test/configuration/__init__.py b/cdist/test/configuration/__init__.py index 07a73bda..5305b6d3 100644 --- a/cdist/test/configuration/__init__.py +++ b/cdist/test/configuration/__init__.py @@ -33,7 +33,7 @@ import sys my_dir = op.abspath(op.dirname(__file__)) fixtures = op.join(my_dir, 'fixtures') interpolation_config_file = op.join(fixtures, "interpolation-test.cfg") -colored_output_default = sys.stdout.isatty() +colored_output_default = 'auto' def newConfigParser(): diff --git a/configuration/cdist.cfg.skeleton b/configuration/cdist.cfg.skeleton index f2a09064..0730201d 100644 --- a/configuration/cdist.cfg.skeleton +++ b/configuration/cdist.cfg.skeleton @@ -17,6 +17,8 @@ # Use a colored output for different log levels. # It can be a boolean or 'auto' (default) which enables this feature if # stdout is a tty and disables it otherwise. +# Colored output is always disabled if the NO_COLOR environment variable is +# defined (https://no-color.org/). # colored_output = auto # # conf_dir @@ -51,7 +53,7 @@ # # out_path # Directory to save cdist output in. -# out_path = +# out_path = # # parallel # Process hosts in parallel. If -1 then the default, number of CPU's in @@ -77,6 +79,6 @@ # remote_shell = /bin/sh # # verbosity -# Set verbosity level. Valid values are: +# Set verbosity level. Valid values are: # ERROR, WARNING, INFO, VERBOSE, DEBUG, TRACE and OFF. # verbosity = INFO From cdb0d2be413114a237a0f56c948cc5cd6cfc2b0b Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Wed, 3 Jun 2020 23:21:50 +0200 Subject: [PATCH 2/7] Patch tests --- cdist/configuration.py | 4 ++++ cdist/test/configuration/__init__.py | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cdist/configuration.py b/cdist/configuration.py index c0fbc063..f68a5e09 100644 --- a/cdist/configuration.py +++ b/cdist/configuration.py @@ -266,6 +266,10 @@ class ColoredOutputOption(BooleanOption): return configparser.ConfigParser.BOOLEAN_STATES[val] +ColoredOutputOption.DEFAULT = ColoredOutputOption.translate( + ColoredOutputOption.DEFAULT) + + _ARG_OPTION_MAPPING = { 'beta': 'beta', 'cache_path_pattern': 'cache_path_pattern', diff --git a/cdist/test/configuration/__init__.py b/cdist/test/configuration/__init__.py index 5305b6d3..3fd24ca5 100644 --- a/cdist/test/configuration/__init__.py +++ b/cdist/test/configuration/__init__.py @@ -187,7 +187,8 @@ class ConfigurationTestCase(test.CdistTestCase): 'remote_shell': '/bin/sh', 'inventory_dir': None, 'cache_path_pattern': None, - 'colored_output': colored_output_default, + 'colored_output': cc.ColoredOutputOption.translate( + colored_output_default), 'conf_dir': None, 'init_manifest': None, 'out_path': None, @@ -587,7 +588,8 @@ class ConfigurationTestCase(test.CdistTestCase): 'remote_shell': '/usr/bin/sh', 'inventory_dir': None, 'cache_path_pattern': None, - 'colored_output': colored_output_default, + 'colored_output': cc.ColoredOutputOption.translate( + colored_output_default), 'conf_dir': [ '/opt/cdist/conf', '/usr/local/share/cdist/conf', @@ -674,7 +676,8 @@ class ConfigurationTestCase(test.CdistTestCase): 'remote_shell': '/usr/bin/sh', 'inventory_dir': '/var/db/cdist/inventory', 'cache_path_pattern': None, - 'colored_output': colored_output_default, + 'colored_output': cc.ColoredOutputOption.translate( + colored_output_default), 'conf_dir': [ '/opt/cdist/conf', '/usr/local/share/cdist/conf', From 89e48734bf13446024e9009bce7e77cbd4255840 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Fri, 5 Jun 2020 12:22:49 +0200 Subject: [PATCH 3/7] Let config file and command line override NO_COLOR envvar --- cdist/configuration.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cdist/configuration.py b/cdist/configuration.py index f68a5e09..a12940b0 100644 --- a/cdist/configuration.py +++ b/cdist/configuration.py @@ -256,14 +256,12 @@ class ColoredOutputOption(BooleanOption): @staticmethod def translate(val): - if 'NO_COLOR' in os.environ: - return False - elif isinstance(val, bool): + if isinstance(val, bool): return val - elif val == 'auto': - return sys.stdout.isatty() - else: + elif val in configparser.ConfigParser.BOOLEAN_STATES: return configparser.ConfigParser.BOOLEAN_STATES[val] + elif val == 'auto': + return 'NO_COLOR' not in os.environ and sys.stdout.isatty() ColoredOutputOption.DEFAULT = ColoredOutputOption.translate( From 790c6efae9e379cb140e59469f07d2c3732f536c Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Fri, 5 Jun 2020 13:56:30 +0200 Subject: [PATCH 4/7] Update colored output documentation --- docs/src/cdist-reference.rst.sh | 38 +++++++++++++++++++++++++++------ docs/src/man1/cdist.rst | 35 +++++++++++++++++------------- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/docs/src/cdist-reference.rst.sh b/docs/src/cdist-reference.rst.sh index 3b997f63..a841862e 100755 --- a/docs/src/cdist-reference.rst.sh +++ b/docs/src/cdist-reference.rst.sh @@ -34,7 +34,7 @@ dest="$__cdist_abs_mydir/$filename" cd "$__cdist_abs_mydir" exec > "$dest" -cat << eof +cat << eof Reference ========= Variable, path and type reference for cdist @@ -51,7 +51,7 @@ eof done ) -cat << eof +cat << eof Paths ----- @@ -187,13 +187,13 @@ usable within a object directory: files This directory is reserved for user data and will not be used - by cdist at any time. It can be used freely by the type + by cdist at any time. It can be used freely by the type (for instance to store template results). changed This empty file exists in an object directory, if the object has code to be executed (either remote or local). stdin - This file exists and contains data, if data was provided on stdin + This file exists and contains data, if data was provided on stdin when the type was called. @@ -222,65 +222,89 @@ __cdist_log_level, __cdist_log_level_name | TRACE | 5 | +----------------+-----------------+ + Available for: initial manifest, explorer, type manifest, type explorer, + type gencode. +__cdist_colored_log + whether or not cdist's log has colors enabled. + Is set to the string ``true`` if cdist's output is using colors, + otherwise the variable contains the string ``false``. + Available for: initial manifest, explorer, type manifest, type explorer, type gencode. __cdist_dry_run Is set only when doing dry run (``-n`` flag). + Available for: initial manifest, explorer, type manifest, type explorer, type gencode. __explorer Directory that contains all global explorers. + Available for: initial manifest, explorer, type explorer, shell. __files Directory that contains content from the "files" subdirectories from the configuration directories. + Available for: initial manifest, type manifest, type gencode, shell. __manifest Directory that contains the initial manifest. + Available for: initial manifest, type manifest, shell. __global Directory that contains generic output like explorer. + Available for: initial manifest, type manifest, type gencode, shell. __messages_in File to read messages from. + Available for: initial manifest, type manifest, type gencode. __messages_out File to write messages. + Available for: initial manifest, type manifest, type gencode. __object Directory that contains the current object. + Available for: type manifest, type explorer, type gencode and code scripts. __object_id The type unique object id. + Available for: type manifest, type explorer, type gencode and code scripts. - Note: The leading and the trailing "/" will always be stripped (caused by - the filesystem database and ensured by the core). - Note: Double slashes ("//") will not be fixed and result in an error. + + | Note: The leading and the trailing "/" will always be stripped (caused by + the filesystem database and ensured by the core). + | Note: Double slashes ("//") will not be fixed and result in an error. __object_name The full qualified name of the current object. + Available for: type manifest, type explorer, type gencode. __target_host The host we are deploying to. This is primary variable. It's content is literally the one user passed in. + Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell. __target_hostname The hostname of host we are deploying to. This variable is derived from **__target_host** (using **socket.getaddrinfo(__target_host)** and then **socket.gethostbyaddr()**). + Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell. __target_fqdn The fully qualified domain name of the host we are deploying to. This variable is derived from **__target_host** (using **socket.getfqdn()**). + Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell. __target_host_tags Comma separated list of target host tags. + Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell. __type Path to the current type. + Available for: type manifest, type gencode. __type_explorer Directory that contains the type explorers. + Available for: type explorer. Environment variables (for writing) diff --git a/docs/src/man1/cdist.rst b/docs/src/man1/cdist.rst index 4c34c4b7..4def97b5 100644 --- a/docs/src/man1/cdist.rst +++ b/docs/src/man1/cdist.rst @@ -112,9 +112,13 @@ All commands accept the following options: Show the help screen. **--colors COLORED_OUTPUT** - Use a colored output for different log levels.It can - be a boolean or "auto" (default) which enables this - feature if stdout is a tty and disables it otherwise. + Colorize cdist's output. If enabled, cdist will use different colors for + different log levels. + COLORED_OUTPUT recognizes the boolean values 'yes'/'no', 'on'/'off', + 'true'/'false', '1'/'0', and 'auto' (the default). + + If the value is 'auto', colored output is enabled if stdout is a TTY + unless the NO_COLOR (https://no-color.org/) environment variable is defined. **-l LOGLEVEL, --log-level LOGLEVEL** Set the specified verbosity level. The levels, in @@ -168,7 +172,7 @@ Install command is currently in beta. **-b, --beta** Enable beta functionality. - + **-C CACHE_PATH_PATTERN, --cache-path-pattern CACHE_PATH_PATTERN** Specify custom cache path pattern. If it is not set then default hostdir is used. For more info on format see @@ -191,7 +195,7 @@ Install command is currently in beta. **-I INVENTORY_DIR, --inventory INVENTORY_DIR** Use specified custom inventory directory. Inventory - directory is set up by the following rules: if cdist + directory is set up by the following rules: if cdist configuration resolves this value then specified directory is used, if HOME env var is set then ~/.cdit/inventory is used, otherwise distribution @@ -306,7 +310,7 @@ Add host(s) to inventory database. **-I INVENTORY_DIR, --inventory INVENTORY_DIR** Use specified custom inventory directory. Inventory - directory is set up by the following rules: if cdist + directory is set up by the following rules: if cdist configuration resolves this value then specified directory is used, if HOME env var is set then ~/.cdit/inventory is used, otherwise distribution @@ -336,7 +340,7 @@ Add tag(s) to inventory database. **-I INVENTORY_DIR, --inventory INVENTORY_DIR** Use specified custom inventory directory. Inventory - directory is set up by the following rules: if cdist + directory is set up by the following rules: if cdist configuration resolves this value then specified directory is used, if HOME env var is set then ~/.cdit/inventory is used, otherwise distribution @@ -379,7 +383,7 @@ Delete host(s) from inventory database. **-I INVENTORY_DIR, --inventory INVENTORY_DIR** Use specified custom inventory directory. Inventory - directory is set up by the following rules: if cdist + directory is set up by the following rules: if cdist configuration resolves this value then specified directory is used, if HOME env var is set then ~/.cdit/inventory is used, otherwise distribution @@ -413,7 +417,7 @@ Delete tag(s) from inventory database. **-I INVENTORY_DIR, --inventory INVENTORY_DIR** Use specified custom inventory directory. Inventory - directory is set up by the following rules: if cdist + directory is set up by the following rules: if cdist configuration resolves this value then specified directory is used, if HOME env var is set then ~/.cdit/inventory is used, otherwise distribution @@ -460,7 +464,7 @@ List inventory database. **-I INVENTORY_DIR, --inventory INVENTORY_DIR** Use specified custom inventory directory. Inventory - directory is set up by the following rules: if cdist + directory is set up by the following rules: if cdist configuration resolves this value then specified directory is used, if HOME env var is set then ~/.cdit/inventory is used, otherwise distribution @@ -685,6 +689,9 @@ The possible keywords and their meanings are as follows: :strong:`cache_path_pattern` Specify cache path pattern. +:strong:`colored_output` + Colorize cdist's output. cf. the :code:`--colors` option. + :strong:`conf_dir` List of configuration directories separated with the character conventionally used by the operating system to separate search path components (as in PATH), @@ -738,7 +745,7 @@ The possible keywords and their meanings are as follows: in the format: YYYYMMDDHHMMSS.us. :strong:`verbosity` - Set verbosity level. Valid values are: + Set verbosity level. Valid values are: 'ERROR', 'WARNING', 'INFO', 'VERBOSE', 'DEBUG', 'TRACE' and 'OFF'. @@ -770,7 +777,7 @@ cdist/preos NOTES ----- cdist detects if host is specified by IPv6 address. If so then remote_copy -command is executed with host address enclosed in square brackets +command is executed with host address enclosed in square brackets (see :strong:`scp`\ (1)). EXAMPLES @@ -906,9 +913,7 @@ CDIST_CACHE_PATH_PATTERN Custom cache path pattern. CDIST_COLORED_OUTPUT - Use a colored output for different log levels. - It can be a boolean or 'auto' (default) which enables this feature if - stdout is a tty and disables it otherwise. + Colorize cdist's output. cf. the :code:`--colors` option. CDIST_CONFIG_FILE Custom configuration file. From 7a570f8692a727662a6eda2c13f2b578b55864e4 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Fri, 5 Jun 2020 13:59:17 +0200 Subject: [PATCH 5/7] [cdist.cfg.skeleton] Update colored_output documentation based on cdist(1) --- configuration/cdist.cfg.skeleton | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/configuration/cdist.cfg.skeleton b/configuration/cdist.cfg.skeleton index 0730201d..9c66b51b 100644 --- a/configuration/cdist.cfg.skeleton +++ b/configuration/cdist.cfg.skeleton @@ -14,11 +14,12 @@ # cache_path_pattern = %h # # colored_output -# Use a colored output for different log levels. -# It can be a boolean or 'auto' (default) which enables this feature if -# stdout is a tty and disables it otherwise. -# Colored output is always disabled if the NO_COLOR environment variable is -# defined (https://no-color.org/). +# Colorize cdist's output. If enabled, cdist will use different colors for +# different log levels. +# Recognized values are 'yes'/'no', 'on'/'off', 'true'/'false', '1'/'0', +# and 'auto' +# If the value is 'auto', colored output is enabled if stdout is a TTY +# unless the NO_COLOR (https://no-color.org/) environment variable is defined. # colored_output = auto # # conf_dir From 23e66e08fab14f254c426c88271f461fe8499ba4 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sat, 6 Jun 2020 13:39:29 +0200 Subject: [PATCH 6/7] Restrict colored_output value to always/never/auto. --- cdist/argparse.py | 7 +++---- cdist/configuration.py | 8 +++++--- configuration/cdist.cfg.skeleton | 7 +++---- docs/src/cdist-reference.rst.sh | 6 +++--- docs/src/man1/cdist.rst | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cdist/argparse.py b/cdist/argparse.py index 0782654f..85dba246 100644 --- a/cdist/argparse.py +++ b/cdist/argparse.py @@ -128,10 +128,9 @@ def get_parsers(): parser['colored_output'] = argparse.ArgumentParser(add_help=False) parser['colored_output'].add_argument( - '--colors', - help='Use a colored output for different log levels.' - 'It can be a boolean or "auto" (default) which enables this ' - 'feature if stdout is a tty and disables it otherwise.', + '--colors', metavar='WHEN', + help="Colorize cdist's output based on log level; " + "WHEN is 'always', 'never', or 'auto'.", action='store', dest='colored_output', required=False, choices=cdist.configuration.ColoredOutputOption.CHOICES) diff --git a/cdist/configuration.py b/cdist/configuration.py index a12940b0..4ba43a7a 100644 --- a/cdist/configuration.py +++ b/cdist/configuration.py @@ -248,7 +248,7 @@ class LogLevelOption(OptionBase): class ColoredOutputOption(BooleanOption): - CHOICES = tuple(configparser.ConfigParser.BOOLEAN_STATES) + ('auto',) + CHOICES = ('always', 'never', 'auto') DEFAULT = 'auto' def get_converter(self): @@ -258,8 +258,10 @@ class ColoredOutputOption(BooleanOption): def translate(val): if isinstance(val, bool): return val - elif val in configparser.ConfigParser.BOOLEAN_STATES: - return configparser.ConfigParser.BOOLEAN_STATES[val] + elif val == 'always': + return True + elif val == 'never': + return False elif val == 'auto': return 'NO_COLOR' not in os.environ and sys.stdout.isatty() diff --git a/configuration/cdist.cfg.skeleton b/configuration/cdist.cfg.skeleton index 9c66b51b..14861592 100644 --- a/configuration/cdist.cfg.skeleton +++ b/configuration/cdist.cfg.skeleton @@ -16,10 +16,9 @@ # colored_output # Colorize cdist's output. If enabled, cdist will use different colors for # different log levels. -# Recognized values are 'yes'/'no', 'on'/'off', 'true'/'false', '1'/'0', -# and 'auto' -# If the value is 'auto', colored output is enabled if stdout is a TTY -# unless the NO_COLOR (https://no-color.org/) environment variable is defined. +# Recognized values are 'always', 'never', and 'auto'. +# If the value is 'auto', colors are enabled if stdout is a TTY unless +# the NO_COLOR (https://no-color.org/) environment variable is defined. # colored_output = auto # # conf_dir diff --git a/docs/src/cdist-reference.rst.sh b/docs/src/cdist-reference.rst.sh index a841862e..c0ac2c3e 100755 --- a/docs/src/cdist-reference.rst.sh +++ b/docs/src/cdist-reference.rst.sh @@ -369,9 +369,9 @@ CDIST_BETA Enable beta functionalities. CDIST_COLORED_OUTPUT - Use a colored output for different log levels. - It can be a boolean or 'auto' (default) which enables this feature if - stdout is a tty and disables it otherwise. + Colorize cdist's output. If enabled, cdist will use different colors for + different log levels. + Recognized values are 'always', 'never', and 'auto' (the default). CDIST_CACHE_PATH_PATTERN Custom cache path pattern. diff --git a/docs/src/man1/cdist.rst b/docs/src/man1/cdist.rst index 4def97b5..760ff8f6 100644 --- a/docs/src/man1/cdist.rst +++ b/docs/src/man1/cdist.rst @@ -111,11 +111,11 @@ All commands accept the following options: **-h, --help** Show the help screen. -**--colors COLORED_OUTPUT** +**--colors WHEN** Colorize cdist's output. If enabled, cdist will use different colors for different log levels. - COLORED_OUTPUT recognizes the boolean values 'yes'/'no', 'on'/'off', - 'true'/'false', '1'/'0', and 'auto' (the default). + COLORED_OUTPUT recognizes the values 'always', 'never', + and 'auto' (the default). If the value is 'auto', colored output is enabled if stdout is a TTY unless the NO_COLOR (https://no-color.org/) environment variable is defined. From 89ebd7a4f79bef894cb3f31931571cf577778b26 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 7 Jun 2020 16:45:48 +0200 Subject: [PATCH 7/7] cdist man page: update --colors metavar name --- docs/src/man1/cdist.rst | 62 ++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/docs/src/man1/cdist.rst b/docs/src/man1/cdist.rst index 760ff8f6..aae98538 100644 --- a/docs/src/man1/cdist.rst +++ b/docs/src/man1/cdist.rst @@ -15,21 +15,19 @@ SYNOPSIS cdist banner [-h] [-l LOGLEVEL] [-q] [-v] - cdist config [-h] [-l LOGLEVEL] [-q] [-v] [-b] - [--colors COLORED_OUTPUT] [-g CONFIG_FILE] [-4] [-6] - [-C CACHE_PATH_PATTERN] [-c CONF_DIR] [-i MANIFEST] - [-j [JOBS]] [-n] [-o OUT_PATH] [-P] + cdist config [-h] [-l LOGLEVEL] [-q] [-v] [-b] [--colors WHEN] + [-g CONFIG_FILE] [-4] [-6] [-C CACHE_PATH_PATTERN] + [-c CONF_DIR] [-i MANIFEST] [-j [JOBS]] [-n] [-o OUT_PATH] [-P] [-R [{tar,tgz,tbz2,txz}]] [-r REMOTE_OUT_PATH] [--remote-copy REMOTE_COPY] [--remote-exec REMOTE_EXEC] [-S] [-I INVENTORY_DIR] [-A] [-a] [-f HOSTFILE] [-p [HOST_MAX]] [-s] [-t] [host [host ...]] - cdist install [-h] [-l LOGLEVEL] [-q] [-v] [-b] - [--colors COLORED_OUTPUT] [-g CONFIG_FILE] [-4] [-6] - [-C CACHE_PATH_PATTERN] [-c CONF_DIR] [-i MANIFEST] - [-j [JOBS]] [-n] [-o OUT_PATH] [-P] - [-R [{tar,tgz,tbz2,txz}]] [-r REMOTE_OUT_PATH] + cdist install [-h] [-l LOGLEVEL] [-q] [-v] [-b] [--colors WHEN] + [-g CONFIG_FILE] [-4] [-6] [-C CACHE_PATH_PATTERN] + [-c CONF_DIR] [-i MANIFEST] [-j [JOBS]] [-n] [-o OUT_PATH] + [-P] [-R [{tar,tgz,tbz2,txz}]] [-r REMOTE_OUT_PATH] [--remote-copy REMOTE_COPY] [--remote-exec REMOTE_EXEC] [-S] [-I INVENTORY_DIR] [-A] [-a] [-f HOSTFILE] [-p [HOST_MAX]] [-s] [-t] @@ -37,35 +35,31 @@ SYNOPSIS cdist inventory [-h] {add-host,add-tag,del-host,del-tag,list} ... - cdist inventory add-host [-h] [-l LOGLEVEL] [-q] [-v] [-b] - [--colors COLORED_OUTPUT] [-g CONFIG_FILE] - [-I INVENTORY_DIR] [-f HOSTFILE] + cdist inventory add-host [-h] [-l LOGLEVEL] [-q] [-v] [-b] [--colors WHEN] + [-g CONFIG_FILE] [-I INVENTORY_DIR] [-f HOSTFILE] [host [host ...]] - cdist inventory add-tag [-h] [-l LOGLEVEL] [-q] [-v] [-b] - [--colors COLORED_OUTPUT] [-g CONFIG_FILE] - [-I INVENTORY_DIR] [-f HOSTFILE] [-T TAGFILE] - [-t TAGLIST] - [host [host ...]] - - cdist inventory del-host [-h] [-l LOGLEVEL] [-q] [-v] [-b] - [--colors COLORED_OUTPUT] [-g CONFIG_FILE] - [-I INVENTORY_DIR] [-a] [-f HOSTFILE] - [host [host ...]] - - cdist inventory del-tag [-h] [-l LOGLEVEL] [-q] [-v] [-b] - [--colors COLORED_OUTPUT] [-g CONFIG_FILE] - [-I INVENTORY_DIR] [-a] [-f HOSTFILE] + cdist inventory add-tag [-h] [-l LOGLEVEL] [-q] [-v] [-b] [--colors WHEN] + [-g CONFIG_FILE] [-I INVENTORY_DIR] [-f HOSTFILE] [-T TAGFILE] [-t TAGLIST] [host [host ...]] - cdist inventory list [-h] [-l LOGLEVEL] [-q] [-v] [-b] - [--colors COLORED_OUTPUT] [-g CONFIG_FILE] - [-I INVENTORY_DIR] [-a] [-f HOSTFILE] [-H] [-t] + cdist inventory del-host [-h] [-l LOGLEVEL] [-q] [-v] [-b] [--colors WHEN] + [-g CONFIG_FILE] [-I INVENTORY_DIR] [-a] + [-f HOSTFILE] + [host [host ...]] + + cdist inventory del-tag [-h] [-l LOGLEVEL] [-q] [-v] [-b] [--colors WHEN] + [-g CONFIG_FILE] [-I INVENTORY_DIR] [-a] + [-f HOSTFILE] [-T TAGFILE] [-t TAGLIST] + [host [host ...]] + + cdist inventory list [-h] [-l LOGLEVEL] [-q] [-v] [-b] [--colors WHEN] + [-g CONFIG_FILE] [-I INVENTORY_DIR] [-a] [-f HOSTFILE] + [-H] [-t] [host [host ...]] - cdist preos [-h] [-l LOGLEVEL] [-q] [-v] [-c CONF_DIR] [-g CONFIG_FILE] - [-L] + cdist preos [-h] [-l LOGLEVEL] [-q] [-v] [-c CONF_DIR] [-g CONFIG_FILE] [-L] [preos] ... cdist preos [preos-options] debian [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B] @@ -89,8 +83,7 @@ SYNOPSIS [-S SCRIPT] [-s SUITE] [-y REMOTE_COPY] target_dir - cdist shell [-h] [-l LOGLEVEL] [-q] [-v] [--colors COLORED_OUTPUT] - [-s SHELL] + cdist shell [-h] [-l LOGLEVEL] [-q] [-v] [--colors WHEN] [-s SHELL] cdist info [-h] [-a] [-c CONF_DIR] [-e] [-F] [-f] [-g CONFIG_FILE] [-t] [pattern] @@ -114,8 +107,7 @@ All commands accept the following options: **--colors WHEN** Colorize cdist's output. If enabled, cdist will use different colors for different log levels. - COLORED_OUTPUT recognizes the values 'always', 'never', - and 'auto' (the default). + WHEN recognizes the values 'always', 'never', and 'auto' (the default). If the value is 'auto', colored output is enabled if stdout is a TTY unless the NO_COLOR (https://no-color.org/) environment variable is defined.