forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
125 lines (100 loc) · 3.39 KB
/
cli.py
File metadata and controls
125 lines (100 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import io
import os
import sys
import logging
import argparse
from configparser import RawConfigParser, NoSectionError
from commitizen import (
registered,
run,
set_commiter,
show_example,
show_info,
show_schema,
version,
)
req_version = (3,)
cur_version = sys.version_info
logger = logging.getLogger(__name__)
if cur_version > req_version:
from pathlib import Path
else:
from pathlib2 import Path
def get_parser(config):
description = (
"Commitizen is a cli tool to generate conventional commits.\n"
"For more information about the topic go to "
"https://conventionalcommits.org/"
)
formater = argparse.RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(
prog="cz", description=description, formatter_class=formater
)
parser.set_defaults(func=run)
parser.add_argument(
"--debug", action="store_true", default=False, help="use debug mode"
)
parser.add_argument(
"-n", "--name", default=config.get("name"), help="use the given commitizen"
)
parser.add_argument(
"--version",
action="store_true",
default=False,
help="get the version of the installed commitizen",
)
subparser = parser.add_subparsers(title="commands")
lscz = subparser.add_parser("ls", help="show available commitizens")
lscz.set_defaults(func=registered)
if cur_version > req_version:
commit = subparser.add_parser("commit", aliases=["c"], help="create new commit")
else:
commit = subparser.add_parser("commit", help="create new commit")
commit = subparser.add_parser("c", help="alias for commit")
commit.set_defaults(func=run)
example = subparser.add_parser("example", help="show commit example")
example.set_defaults(func=show_example)
info = subparser.add_parser("info", help="show information about the cz")
info.set_defaults(func=show_info)
schema = subparser.add_parser("schema", help="show commit schema")
schema.set_defaults(func=show_schema)
return parser
def load_cfg():
defaults = {"name": "cz_conventional_commits"}
config = RawConfigParser("")
try:
home = str(Path.home())
except AttributeError:
home = os.path.expanduser("~")
config_file = ".cz"
global_cfg = os.path.join(home, config_file)
# load cfg from current project
configs = ["setup.cfg", ".cz.cfg", config_file, global_cfg]
for cfg in configs:
if not os.path.exists(config_file) and os.path.exists(cfg):
config_file = cfg
break
config_file_exists = os.path.exists(config_file)
if config_file_exists:
logger.debug('Reading file "%s"', config_file)
config.readfp(io.open(config_file, "rt", encoding="utf-8"))
log_config = io.StringIO()
config.write(log_config)
try:
defaults.update(dict(config.items("commitizen")))
break
except NoSectionError:
# The file does not have commitizen section
continue
return defaults
def main():
config = load_cfg()
parser = get_parser(config)
args = parser.parse_args()
if args.debug:
logging.getLogger("commitizen").setLevel(logging.DEBUG)
if args.version:
logger.info(version())
sys.exit(0)
set_commiter(args.name)
args.func(args)