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

50 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-29 07:50 +0000

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

2 

3import logging 

4from pathlib import Path 

5 

6import typer 

7 

8from somesy.commands import init_config 

9from somesy.core.core import discover_input 

10from somesy.core.log import SomesyLogLevel, set_log_level 

11 

12from .util import wrap_exceptions 

13 

14logger = logging.getLogger("somesy") 

15app = typer.Typer() 

16 

17 

18@app.command() 

19@wrap_exceptions 

20def config(): 

21 """Set CLI configs for somesy.""" 

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

23 input_file_default = discover_input() 

24 

25 # prompt for inputs 

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

27 options = dict(input_file=Path(input_file)) 

28 

29 # ---- 

30 

31 options["no_sync_cff"] = not typer.confirm( 

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

33 ) 

34 if cff_file := typer.prompt("CFF file path", default="CITATION.cff"): 

35 options["cff_file"] = cff_file 

36 

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

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

39 ) 

40 if codemeta_file := typer.prompt( 

41 "codemeta.json file path", default="codemeta.json" 

42 ): 

43 options["codemeta_file"] = codemeta_file 

44 

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

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

47 ) 

48 if pyproject_file := typer.prompt( 

49 "pyproject.toml file path", default="pyproject.toml" 

50 ): 

51 options["pyproject_file"] = pyproject_file 

52 

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

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

55 ) 

56 if package_json_file := typer.prompt( 

57 "package.json file path", default="package.json" 

58 ): 

59 options["package_json_file"] = package_json_file 

60 

61 options["no_sync_julia"] = not typer.confirm( 

62 "Do you want to sync to a Project.toml(Julia) file?", default=True 

63 ) 

64 if julia_file := typer.prompt( 

65 "Project.toml (Julia) file path", default="Project.toml" 

66 ): 

67 options["julia_file"] = julia_file 

68 

69 options["no_sync_fortran"] = not typer.confirm( 

70 "Do you want to sync to a fpm.toml(fortran) file?", default=True 

71 ) 

72 fortran_file = typer.prompt("fpm.toml(fortran) file path", default="fpm.toml") 

73 if fortran_file is not None or fortran_file != "": 

74 options["fortran_file"] = fortran_file 

75 

76 options["no_sync_pom_xml"] = not typer.confirm( 

77 "Do you want to sync to a pom.xml file?", default=True 

78 ) 

79 if pom_xml_file := typer.prompt("pom.xml file path", default="pom.xml"): 

80 options["pom_xml_file"] = pom_xml_file 

81 

82 options["no_sync_mkdocs"] = not typer.confirm( 

83 "Do you want to sync to a mkdocs.yml file?", default=True 

84 ) 

85 if mkdocs_file := typer.prompt("mkdocs.yml file path", default="mkdocs.yml"): 

86 options["mkdocs_file"] = mkdocs_file 

87 

88 options["no_sync_rust"] = not typer.confirm( 

89 "Do you want to sync to a Cargo.toml file?", default=True 

90 ) 

91 if rust_file := typer.prompt("Cargo.toml file path", default="Cargo.toml"): 

92 options["rust_file"] = rust_file 

93 

94 # ---- 

95 

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

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

98 ) 

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

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

101 

102 set_log_level( 

103 SomesyLogLevel.from_flags( 

104 debug=options["debug"], 

105 verbose=options["verbose"], 

106 info=options["show_info"], 

107 ) 

108 ) 

109 

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

111 

112 init_config(input_file, options) 

113 logger.info( 

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

115 )