]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba-tool: respect NO_COLOR env variable and --color options
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Tue, 6 Jul 2021 22:43:59 +0000 (10:43 +1200)
committerDouglas Bagnall <dbagnall@samba.org>
Tue, 6 Sep 2022 21:12:36 +0000 (21:12 +0000)
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 <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
python/samba/netcmd/__init__.py

index bb70f72449ef116625c4c3e177f0b4c05f155428..eeb28964142ca0b78e813c09f69b7c9c54dffc4b 100644 (file)
@@ -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):