Skip to content

Commit b86f532

Browse files
committed
feat(config): new loads from ~/.cz and working project .cz .cz.cfg and setup.cfg
1 parent 4025ff2 commit b86f532

File tree

2 files changed

+82
-14
lines changed

2 files changed

+82
-14
lines changed

commitizen/__init__.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,39 @@ def registered(*args, **kwargs):
3737
logger.info(_r)
3838

3939

40-
def commiter(name=None):
40+
name = None
41+
42+
43+
def commiter():
4144
"""Loaded commitizen.
4245
4346
:rtype: instance of implemented BaseCommitizen
4447
"""
45-
if name is None:
46-
logger.debug('Loading default commitizen')
47-
name = 'cz_angular'
48-
48+
global name
4949
return registry[name]()
5050

5151

52+
def set_commiter(new_name):
53+
global name
54+
logger.debug('Updating commiter name')
55+
name = new_name
56+
57+
58+
def show_example(args):
59+
_commiter = commiter()
60+
_commiter.show_example()
61+
62+
63+
def show_info(args):
64+
_commiter = commiter()
65+
_commiter.show_info()
66+
67+
68+
def show_schema(args):
69+
_commiter = commiter()
70+
_commiter.show_schema()
71+
72+
5273
def run(args):
53-
_commiter = commiter(name=args.use_cz)
74+
_commiter = commiter()
5475
_commiter.run()

commitizen/cli.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
import os
2+
import io
13
import logging
24
import argparse
3-
from commitizen import commiter, registered, run
5+
from pathlib import Path
6+
from configparser import RawConfigParser
7+
from commitizen import (registered, run, set_commiter, show_example,
8+
show_info, show_schema)
49

510

6-
def get_parser():
11+
logger = logging.getLogger(__name__)
12+
13+
14+
def get_parser(config):
715
description = (
816
'Commitizen is a python cli tool to generate conventional commits.\n'
917
'For more information about the topic go to '
@@ -14,9 +22,11 @@ def get_parser():
1422
parser = argparse.ArgumentParser(prog='cz',
1523
description=description,
1624
formatter_class=formater)
17-
parser.set_defaults(func=commiter().run)
25+
parser.set_defaults(func=run)
1826
parser.add_argument('--debug', action='store_true', default=False,
1927
help='use debug mode')
28+
parser.add_argument('-n', '--name', default=config.get('name'),
29+
help='use the given commitizen')
2030
subparser = parser.add_subparsers(title='commands')
2131
# subparser.add_argument('--debug', default=False)
2232

@@ -26,24 +36,61 @@ def get_parser():
2636
commit = subparser.add_parser('commit', aliases=['c'],
2737
help='create new commit')
2838
commit.set_defaults(func=run)
29-
commit.add_argument('-u', '--use_cz', help='use the given commitizen')
3039

3140
example = subparser.add_parser('example', help='show commit example')
32-
example.set_defaults(func=commiter().show_example)
41+
example.set_defaults(func=show_example)
3342

3443
info = subparser.add_parser('info', help='show information about the cz')
35-
info.set_defaults(func=commiter().show_info)
44+
info.set_defaults(func=show_info)
3645

3746
schema = subparser.add_parser('schema', help='show commit schema')
38-
schema.set_defaults(func=commiter().show_schema)
47+
schema.set_defaults(func=show_schema)
3948

4049
return parser
4150

4251

52+
def load_cfg():
53+
defaults = {
54+
'name': 'cz_angular'
55+
}
56+
config = RawConfigParser('')
57+
58+
home = str(Path.home())
59+
config_file = '.cz'
60+
61+
# load cfg from home folder
62+
global_cfg = os.path.join(home, config_file)
63+
if os.path.exists(global_cfg):
64+
config.readfp(io.open(global_cfg, 'rt', encoding='utf-8'))
65+
log_config = io.StringIO()
66+
config.write(log_config)
67+
defaults.update(dict(config.items("commitizen")))
68+
69+
# load cfg from current project
70+
configs = ['setup.cfg', '.cz.cfg']
71+
for cfg in configs:
72+
if not os.path.exists(config_file) and os.path.exists(cfg):
73+
config_file = cfg
74+
break
75+
76+
config_file_exists = os.path.exists(config_file)
77+
if config_file_exists:
78+
logger.debug('Reading file "%s"', config_file)
79+
config.readfp(io.open(config_file, 'rt', encoding='utf-8'))
80+
log_config = io.StringIO()
81+
config.write(log_config)
82+
defaults.update(dict(config.items("commitizen")))
83+
84+
return defaults
85+
86+
4387
def main():
44-
parser = get_parser()
88+
config = load_cfg()
89+
parser = get_parser(config)
4590
args = parser.parse_args()
4691

4792
if args.debug:
4893
logging.getLogger('commitizen').setLevel(logging.DEBUG)
94+
95+
set_commiter(args.name)
4996
args.func(args)

0 commit comments

Comments
 (0)