1010import argparse
1111import codecs
1212import contextlib
13+ import io
1314import logging
1415import pathlib
1516import re
@@ -81,7 +82,7 @@ def format_file(self,
8182 if original_backup_file_path :
8283 with codecs .open (original_backup_file_path , 'w' , encoding = chosen_encoding ) as wfp :
8384 wfp .write (original_file_content )
84- self .logger .info ("Previous content saved to '%s'." , original_backup_file_path )
85+ self .logger .info ("Original content saved to '%s'." , original_backup_file_path )
8586
8687 def _load_file_content (self ,
8788 file_path : pathlib .Path ) -> (str , str ):
@@ -295,19 +296,31 @@ def _redirect_stdout_to_stderr():
295296 sys .stdout = old_stdout
296297
297298
298- def _standalone_run (program_arguments ):
299- # todo add logger: logs to stderr
299+ def _aname (action ) -> str :
300+ """Converts argument name to string to be consistent with argparse."""
301+ return argparse ._get_action_name (action )
302+
300303
304+ def _standalone_run (program_arguments ):
301305 arg_parser = argparse .ArgumentParser (description = "Formats nginx configuration files in consistent way." )
302306
303307 arg_parser .add_argument ("-v" , "--verbose" , action = "store_true" , help = "show formatted file names" )
304308
305- backup_xor_print_group = arg_parser .add_mutually_exclusive_group ()
306- print_result_action = backup_xor_print_group .add_argument ("-p" , "--print-result" , action = "store_true" ,
307- help = "prints result to stdout, original file is not changed" )
308- backup_xor_print_group .add_argument ("-b" , "--backup-original" , action = "store_true" ,
309- help = "backup original config file" )
310- arg_parser .add_argument ("config_files" , nargs = '+' , help = "configuration files to format" )
309+ pipe_arg = arg_parser .add_argument ("-" , "--pipe" ,
310+ action = "store_true" ,
311+ help = "reads content from standard input, prints result to stdout" )
312+
313+ pipe_xor_backup_group = arg_parser .add_mutually_exclusive_group ()
314+ print_result_arg = pipe_xor_backup_group .add_argument ("-p" , "--print-result" ,
315+ action = "store_true" ,
316+ help = "prints result to stdout, original file is not changed" )
317+ pipe_xor_backup_group .add_argument ("-b" , "--backup-original" ,
318+ action = "store_true" ,
319+ help = "backup original config file as filename.conf~" )
320+
321+ arg_parser .add_argument ("config_files" ,
322+ nargs = '*' ,
323+ help = "configuration files to format" )
311324
312325 formatter_options_group = arg_parser .add_argument_group ("formatting options" )
313326 formatter_options_group .add_argument ("-i" , "--indent" , action = "store" , default = 4 , type = int ,
@@ -321,17 +334,25 @@ def _standalone_run(program_arguments):
321334 format = '%(levelname)s: %(message)s' )
322335
323336 try :
324- if args .print_result and len (args .config_files ) != 1 :
325- raise Exception ("if %s is enabled, only one file can be passed as input" % argparse ._get_action_name (
326- print_result_action ))
337+ if args .pipe and len (args .config_files ) != 0 :
338+ raise Exception ("if %s is enabled, no file can be passed as input" % _aname (pipe_arg ))
339+ if args .pipe and args .backup_original :
340+ raise Exception ("cannot create backup file when %s is enabled" % _aname (pipe_arg ))
341+ if args .print_result and len (args .config_files ) > 1 :
342+ raise Exception ("if %s is enabled, only one file can be passed as input" % _aname (print_result_arg ))
343+ if len (args .config_files ) == 0 and not args .pipe :
344+ raise Exception ("no input files provided, specify at least one file or use %s" % _aname (pipe_arg ))
327345 except Exception as e :
328346 arg_parser .error (str (e ))
329347
330348 format_options = FormatterOptions ()
331349 format_options .indentation = args .indent
332350 formatter = Formatter (format_options )
333351
334- if args .print_result :
352+ if args .pipe :
353+ original_content = io .TextIOWrapper (sys .stdin .buffer , encoding = 'utf-8' )
354+ print (formatter .format_string (original_content .read ()))
355+ elif args .print_result :
335356 print (formatter .get_formatted_string_from_file (args .config_files [0 ]))
336357 else :
337358 for config_file_path in args .config_files :
0 commit comments