瀏覽代碼

Disable colored output when "--json" flag is used, so as to produce valid JSON ouput (#276).

Dan Helfman 5 年之前
父節點
當前提交
5afe0e3d63
共有 3 個文件被更改,包括 12 次插入5 次删除
  1. 1 0
      NEWS
  2. 7 1
      borgmatic/commands/borgmatic.py
  3. 4 4
      docs/how-to/set-up-backups.md

+ 1 - 0
NEWS

@@ -1,4 +1,5 @@
 1.4.22.dev0
 1.4.22.dev0
+ * #276: Disable colored output when "--json" flag is used, so as to produce valid JSON ouput.
  * In "borgmatic --help", don't expand $HOME in listing of default "--config" paths.
  * In "borgmatic --help", don't expand $HOME in listing of default "--config" paths.
 
 
 1.4.21
 1.4.21

+ 7 - 1
borgmatic/commands/borgmatic.py

@@ -587,7 +587,13 @@ def main():  # pragma: no cover
     config_filenames = tuple(collect.collect_config_filenames(global_arguments.config_paths))
     config_filenames = tuple(collect.collect_config_filenames(global_arguments.config_paths))
     configs, parse_logs = load_configurations(config_filenames, global_arguments.overrides)
     configs, parse_logs = load_configurations(config_filenames, global_arguments.overrides)
 
 
-    colorama.init(autoreset=True, strip=not should_do_markup(global_arguments.no_color, configs))
+    any_json_flags = any(
+        getattr(sub_arguments, 'json', False) for sub_arguments in arguments.values()
+    )
+    colorama.init(
+        autoreset=True,
+        strip=not should_do_markup(global_arguments.no_color or any_json_flags, configs),
+    )
     try:
     try:
         configure_logging(
         configure_logging(
             verbosity_to_log_level(global_arguments.verbosity),
             verbosity_to_log_level(global_arguments.verbosity),

+ 4 - 4
docs/how-to/set-up-backups.md

@@ -213,10 +213,10 @@ borgmatic to run.
 ## Colored output
 ## Colored output
 
 
 Borgmatic produces colored terminal output by default. It is disabled when a
 Borgmatic produces colored terminal output by default. It is disabled when a
-non-interactive terminal is detected (like a cron job). Otherwise, you can
-disable it by passing the `--no-color` flag, setting the environment variable
-`PY_COLORS=False`, or setting the `color` option to `false` in the `output`
-section of configuration.
+non-interactive terminal is detected (like a cron job), or when you use the
+`--json` flag. Otherwise, you can disable it by passing the `--no-color` flag,
+setting the environment variable `PY_COLORS=False`, or setting the `color`
+option to `false` in the `output` section of configuration.
 
 
 ## Troubleshooting
 ## Troubleshooting