Coverage for src/somesy/cli/init.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-10 14:33 +0000

1"""Set config files for somesy.""" 

2import logging 

3from pathlib import Path 

4 

5import typer 

6 

7from somesy.commands import init_config 

8from somesy.core.core import discover_input 

9from somesy.core.log import SomesyLogLevel, set_log_level 

10 

11from .util import wrap_exceptions 

12 

13logger = logging.getLogger("somesy") 

14app = typer.Typer() 

15 

16 

17@app.command() 

18@wrap_exceptions 

19def config(): 

20 """Set CLI configs for somesy.""" 

21 # check if input file exists, if not, try to find it from default list 

22 input_file_default = discover_input() 

23 

24 # prompt for inputs 

25 input_file = typer.prompt("Input file path", default=input_file_default) 

26 input_file = Path(input_file) 

27 options = { 

28 "input_file": input_file, 

29 "no_sync_cff": not typer.confirm( 

30 "Do you want to sync to a CFF file?", default=True 

31 ), 

32 } 

33 cff_file = typer.prompt("CFF file path", default="CITATION.cff") 

34 if cff_file is not None or cff_file != "": 

35 options["cff_file"] = cff_file 

36 

37 options["no_sync_pyproject"] = not typer.confirm( 

38 "Do you want to sync to a pyproject.toml file?", default=True 

39 ) 

40 

41 pyproject_file = typer.prompt("pyproject.toml file path", default="pyproject.toml") 

42 if pyproject_file is not None or pyproject_file != "": 

43 options["pyproject_file"] = pyproject_file 

44 

45 options["sync_package_json"] = typer.confirm( 

46 "Do you want to sync to a package.json file?", default=False 

47 ) 

48 package_json_file = typer.prompt("package.json file path", default="package.json") 

49 if package_json_file is not None or package_json_file != "": 

50 options["package_json_file"] = package_json_file 

51 

52 options["no_sync_codemeta"] = not typer.confirm( 

53 "Do you want to sync to a codemeta.json file?", default=True 

54 ) 

55 codemeta_file = typer.prompt("codemeta.json file path", default="codemeta.json") 

56 if codemeta_file is not None or codemeta_file != "": 

57 options["codemeta_file"] = codemeta_file 

58 

59 options["show_info"] = typer.confirm( 

60 "Do you want to show info about the sync process?" 

61 ) 

62 options["verbose"] = typer.confirm("Do you want to show verbose logs?") 

63 options["debug"] = typer.confirm("Do you want to show debug logs?") 

64 

65 set_log_level( 

66 SomesyLogLevel.from_flags( 

67 debug=options["debug"], 

68 verbose=options["verbose"], 

69 info=options["show_info"], 

70 ) 

71 ) 

72 

73 logger.debug(f"CLI options entered: {options}") 

74 

75 init_config(input_file, options) 

76 logger.info( 

77 f"[bold green]Input file is updated/created at {input_file}[/bold green]" 

78 )