From: Douglas Bagnall Date: Tue, 6 Jul 2021 22:43:59 +0000 (+1200) Subject: samba-tool: respect NO_COLOR env variable and --color options X-Git-Tag: talloc-2.4.0~1188 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=eefc030458674b5bb5b8338f7a3b9c1df48ad8ef;p=thirdparty%2Fsamba.git samba-tool: respect NO_COLOR env variable and --color options This allows the NO_COLOR environment variable and --color=never to work for samba-tool commands that use this method. So far that means some parts of drs showrepl. Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett Reviewed-by: Joseph Sutton --- diff --git a/python/samba/netcmd/__init__.py b/python/samba/netcmd/__init__.py index bb70f72449e..eeb28964142 100644 --- a/python/samba/netcmd/__init__.py +++ b/python/samba/netcmd/__init__.py @@ -204,22 +204,11 @@ class Command(object): "constants" in the colour module to be either real colours or empty strings. """ - requested = requested.lower() - if requested == 'no': - colour.switch_colour_off() - - elif requested == 'yes': - colour.switch_colour_on() - - elif requested == 'auto': - if (hasattr(self.outf, 'isatty') and self.outf.isatty()): - colour.switch_colour_on() - else: - colour.switch_colour_off() - - else: - raise CommandError("Unknown --color option: %s " - "please choose from yes, no, auto") + try: + colour.colour_if_wanted(self.outf, requested) + except ValueError as e: + raise CommandError(f"Unknown --color option: {requested} " + "please choose from always|never|auto") class SuperCommand(Command):