2
2
3
3
from commitizen import factory , out
4
4
from commitizen .cz import registry
5
- from commitizen .config import BaseConfig
5
+ from commitizen .config import BaseConfig , TomlConfig , IniConfig
6
6
from commitizen .git import get_latest_tag , get_all_tags
7
+ from commitizen .defaults import config_files
7
8
8
9
9
10
class Init :
@@ -16,20 +17,37 @@ def __call__(self):
16
17
17
18
# No config file exist
18
19
if not self .config .path :
20
+ config_path = self ._ask_config_path ()
21
+
22
+ if "toml" in config_path :
23
+ self .config = TomlConfig (data = "" , path = config_path )
24
+ else :
25
+ self .config = IniConfig (data = "" , path = config_path )
26
+
27
+ self .config .init_empty_config_file ()
28
+
19
29
values_to_add ["name" ] = self ._ask_name ()
20
30
tag = self ._ask_tag ()
21
31
values_to_add ["version" ] = tag
22
32
values_to_add ["tag_format" ] = self ._ask_tag_format (tag )
33
+ self ._update_config_file (values_to_add )
34
+ out .write ("The configuration are all set." )
23
35
else :
36
+ # TODO: handle the case that config file exist but no value
24
37
out .line (f"Config file { self .config .path } already exists" )
25
- raise SystemExit ()
26
38
27
- self ._update_config_file (values_to_add )
28
- out .write ("The configuration are all set." )
39
+ def _ask_config_path (self ) -> str :
40
+ name = questionary .select (
41
+ "Please choose a supported config file: (default: pyproject.tml)" ,
42
+ choices = config_files ,
43
+ default = "pyproject.toml" ,
44
+ style = self .cz .style ,
45
+ ).ask ()
46
+ return name
29
47
30
48
def _ask_name (self ) -> str :
31
49
name = questionary .select (
32
- "Please choose a cz: " ,
50
+ "Please choose a cz: (default: cz_conventional_commits) " ,
33
51
choices = list (registry .keys ()),
34
52
default = "cz_conventional_commits" ,
35
53
style = self .cz .style ,
@@ -38,14 +56,18 @@ def _ask_name(self) -> str:
38
56
39
57
def _ask_tag (self ) -> str :
40
58
latest_tag = get_latest_tag ()
59
+ if not latest_tag :
60
+ out .error ("No Existing Tag. Set tag to v0.0.1" )
61
+ return 'v0.0.1'
62
+
41
63
is_correct_tag = questionary .confirm (
42
64
f"Is { latest_tag } the latest tag?" , style = self .cz .style , default = False
43
65
).ask ()
44
66
if not is_correct_tag :
45
67
tags = get_all_tags ()
46
68
if not tags :
47
- out .line ("No Existing Tag. Set tag to v0.0.1" )
48
- return " v0.0.1"
69
+ out .error ("No Existing Tag. Set tag to v0.0.1" )
70
+ return ' v0.0.1'
49
71
50
72
latest_tag = questionary .select (
51
73
"Please choose the latest tag: " ,
0 commit comments