From eefc030458674b5bb5b8338f7a3b9c1df48ad8ef Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Wed, 7 Jul 2021 10:43:59 +1200 Subject: [PATCH] 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 --- python/samba/netcmd/__init__.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) 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): -- 2.47.3