Skip to content

Commit 16e673d

Browse files
committed
Reduce complexity
1 parent 196500e commit 16e673d

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

cli/config.py

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -184,51 +184,39 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
184184
log.info("Exporting configuration from %s completed", kwargs["url"])
185185

186186

187-
def __import_config(endpoint: platform.Platform, what: list[str], **kwargs) -> None:
188-
"""Imports a platform configuration from a JSON file"""
189-
log.info("Importing configuration to %s", kwargs[options.URL])
190-
key_list = kwargs[options.KEYS]
187+
def __read_input_file(file: str) -> dict[str, any]:
191188
try:
192-
with open(kwargs[options.REPORT_FILE], "r", encoding="utf-8") as fd:
189+
with open(file, "r", encoding="utf-8") as fd:
193190
data = json.loads(fd.read())
194191
except FileNotFoundError as e:
195192
utilities.exit_fatal(f"OS error while reading file: {e}", exit_code=errcodes.OS_ERROR)
196-
if options.WHAT_GROUPS in what:
197-
groups.import_config(endpoint, data)
198-
if options.WHAT_USERS in what:
199-
try:
200-
users.import_config(endpoint, data)
201-
except exceptions.UnsupportedOperation as e:
202-
log.warning(e.message)
203-
if options.WHAT_GATES in what:
204-
qualitygates.import_config(endpoint, data)
205-
if options.WHAT_RULES in what:
206-
try:
207-
rules.import_config(endpoint, data)
208-
except exceptions.UnsupportedOperation as e:
209-
log.warning(e.message)
210-
if options.WHAT_PROFILES in what:
211-
if options.WHAT_RULES not in what:
193+
return data
194+
195+
196+
def __import_config(endpoint: platform.Platform, what: list[str], data: dict[str, any], **kwargs) -> None:
197+
"""Imports a platform configuration from a JSON file"""
198+
log.info("Importing configuration to %s", kwargs[options.URL])
199+
key_list = kwargs[options.KEYS]
200+
201+
calls = {
202+
options.WHAT_GROUPS: groups.import_config,
203+
options.WHAT_USERS: users.import_config,
204+
options.WHAT_GATES: qualitygates.import_config,
205+
options.WHAT_RULES: rules.import_config,
206+
options.WHAT_SETTINGS: platform.import_config,
207+
options.WHAT_PROJECTS: projects.import_config,
208+
options.WHAT_APPS: applications.import_config,
209+
options.WHAT_PORTFOLIOS: portfolios.import_config,
210+
}
211+
212+
for what_item, func in calls.items():
213+
if what_item in what:
212214
try:
213-
rules.import_config(endpoint, data)
215+
func(endpoint, data, key_list=key_list)
214216
except exceptions.UnsupportedOperation as e:
215217
log.warning(e.message)
216-
qualityprofiles.import_config(endpoint, data)
217-
if options.WHAT_SETTINGS in what:
218-
endpoint.import_config(data)
219-
if options.WHAT_PROJECTS in what:
220-
projects.import_config(endpoint, data, key_list=key_list)
221-
if options.WHAT_APPS in what:
222-
try:
223-
applications.import_config(endpoint, data, key_list=key_list)
224-
except exceptions.UnsupportedOperation as e:
225-
log.warning(e.message)
226-
if options.WHAT_PORTFOLIOS in what:
227-
try:
228-
portfolios.import_config(endpoint, data, key_list=key_list)
229-
except exceptions.UnsupportedOperation as e:
230-
log.warning(e.message)
231218
log.info("Importing configuration to %s completed", kwargs[options.URL])
219+
return None
232220

233221

234222
def main() -> None:
@@ -244,6 +232,8 @@ def main() -> None:
244232
utilities.exit_fatal(f"One of --{options.EXPORT} or --{options.IMPORT} option must be chosen", exit_code=errcodes.ARGS_ERROR)
245233

246234
what = utilities.check_what(kwargs.pop(options.WHAT, None), _EVERYTHING, "exported or imported")
235+
if options.WHAT_PROFILES in what and options.WHAT_RULES not in what:
236+
what.append(options.WHAT_RULES)
247237
kwargs[options.FORMAT] = utilities.deduct_format(kwargs[options.FORMAT], kwargs[options.REPORT_FILE], allowed_formats=("json", "yaml"))
248238
if kwargs[options.EXPORT]:
249239
try:
@@ -255,7 +245,7 @@ def main() -> None:
255245
if kwargs["import"]:
256246
if kwargs["file"] is None:
257247
utilities.exit_fatal("--file is mandatory to import configuration", errcodes.ARGS_ERROR)
258-
__import_config(endpoint, what, **kwargs)
248+
__import_config(endpoint, what, __read_input_file(kwargs[options.REPORT_FILE]), **kwargs)
259249
utilities.stop_clock(start_time)
260250
sys.exit(0)
261251

0 commit comments

Comments
 (0)