2323"""
2424import sys
2525import json
26+ import yaml
2627
2728from cli import options
2829from sonar import exceptions , errcodes , utilities
7071def __parse_args (desc ):
7172 parser = options .set_common_args (desc )
7273 parser = options .set_key_arg (parser )
73- parser = options .set_output_file_args (parser , allowed_formats = ("json" ,))
74+ parser = options .set_output_file_args (parser , allowed_formats = ("json" , "yaml" ))
7475 parser = options .add_thread_arg (parser , "project export" )
7576 parser = options .set_what (parser , what_list = _EVERYTHING , operation = "export or import" )
7677 parser = options .add_import_export_arg (parser , "configuration" )
@@ -103,6 +104,15 @@ def __parse_args(desc):
103104 return args
104105
105106
107+ def __write_export (config : dict [str , str ], file : str , format : str ) -> None :
108+ """Writes the configuration in file"""
109+ with utilities .open_file (file ) as fd :
110+ if format == "yaml" :
111+ print (yaml .dump (config ), file = fd )
112+ else :
113+ print (utilities .json_dump (config ), file = fd )
114+
115+
106116def __export_config (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
107117 """Exports a platform configuration in a JSON file"""
108118 export_settings = {
@@ -118,15 +128,12 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
118128
119129 log .info ("Exporting configuration from %s" , kwargs [options .URL ])
120130 key_list = kwargs [options .KEYS ]
121- sq_settings = {}
122- sq_settings [__JSON_KEY_PLATFORM ] = endpoint .basics ()
131+ sq_settings = {__JSON_KEY_PLATFORM : endpoint .basics ()}
123132 if options .WHAT_SETTINGS in what :
124133 sq_settings [__JSON_KEY_SETTINGS ] = endpoint .export (export_settings = export_settings )
125- if options .WHAT_RULES in what :
134+ if options .WHAT_RULES in what or options . WHAT_PROFILES in what :
126135 sq_settings [__JSON_KEY_RULES ] = rules .export (endpoint , export_settings = export_settings )
127136 if options .WHAT_PROFILES in what :
128- if options .WHAT_RULES not in what :
129- sq_settings [__JSON_KEY_RULES ] = rules .export (endpoint , export_settings = export_settings )
130137 sq_settings [__JSON_KEY_PROFILES ] = qualityprofiles .export (endpoint , export_settings = export_settings )
131138 if options .WHAT_GATES in what :
132139 sq_settings [__JSON_KEY_GATES ] = qualitygates .export (endpoint , export_settings = export_settings )
@@ -150,8 +157,7 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
150157 sq_settings = utilities .remove_empties (sq_settings )
151158 if not kwargs ["dontInlineLists" ]:
152159 sq_settings = utilities .inline_lists (sq_settings , exceptions = ("conditions" ,))
153- with utilities .open_file (kwargs ["file" ]) as fd :
154- print (utilities .json_dump (sq_settings ), file = fd )
160+ __write_export (sq_settings , kwargs [options .REPORT_FILE ], kwargs [options .FORMAT ])
155161 log .info ("Exporting configuration from %s completed" , kwargs ["url" ])
156162
157163
@@ -193,7 +199,7 @@ def __import_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
193199 log .info ("Importing configuration to %s completed" , kwargs [options .URL ])
194200
195201
196- def main ():
202+ def main () -> None :
197203 """Main entry point for sonar-config"""
198204 start_time = utilities .start_clock ()
199205 try :
@@ -206,6 +212,7 @@ def main():
206212 utilities .exit_fatal (f"One of --{ options .EXPORT } or --{ options .IMPORT } option must be chosen" , exit_code = errcodes .ARGS_ERROR )
207213
208214 what = utilities .check_what (kwargs .pop (options .WHAT , None ), _EVERYTHING , "exported or imported" )
215+ kwargs [options .FORMAT ] = utilities .deduct_format (kwargs [options .FORMAT ], kwargs [options .REPORT_FILE ], allowed_formats = ("json" , "yaml" ))
209216 if kwargs [options .EXPORT ]:
210217 try :
211218 __export_config (endpoint , what , ** kwargs )
0 commit comments