Skip to content

Commit fe2d37c

Browse files
committed
add internal function get_git_config(); use run() to invoke subprocess; remove "global" from comment; remove spaces from generated email address
Signed-off-by: Larry Gezelius <[email protected]>
1 parent 2e13f57 commit fe2d37c

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

ros2pkg/ros2pkg/verb/create.py

+18-33
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,23 @@ def add_arguments(self, parser, cli_name):
8787
parser.add_argument(
8888
'--library-name',
8989
help='name of the empty library')
90-
90+
9191
def main(self, *, args):
92+
93+
def get_git_config(key: str) -> str | None:
94+
# retrieve a value from git config
95+
git = shutil.which('git')
96+
if git is not None:
97+
result = subprocess.run(
98+
[git, 'config', key],
99+
stdout=subprocess.PIPE,
100+
text=True
101+
)
102+
value = result.stdout.strip()
103+
if value:
104+
return value
105+
return None
106+
92107
available_licenses = {}
93108
for shortname, entry in ament_copyright.get_licenses().items():
94109
available_licenses[entry.spdx] = entry.license_files
@@ -97,39 +112,9 @@ def main(self, *, args):
97112
print('Supported licenses:\n%s' % ('\n'.join(available_licenses)))
98113
sys.exit(0)
99114

100-
git = shutil.which('git')
101-
102-
if args.maintainer_name:
103-
maintainer_name = args.maintainer_name
104-
else:
105-
# try getting the name from the global git config
106-
if git is not None:
107-
p = subprocess.Popen(
108-
[git, 'config', 'user.name'],
109-
stdout=subprocess.PIPE)
110-
resp = p.communicate()
111-
name = resp[0].decode().rstrip()
112-
if name:
113-
maintainer_name = name
114-
if not maintainer_name:
115-
maintainer_name = getpass.getuser()
116-
115+
maintainer_name: str = args.maintainer_name or get_git_config('user.name') or getpass.getuser()
117116
maintainer = Person(maintainer_name)
118-
119-
if args.maintainer_email:
120-
maintainer.email = args.maintainer_email
121-
else:
122-
# try getting the email from the global git config
123-
if git is not None:
124-
p = subprocess.Popen(
125-
[git, 'config', 'user.email'],
126-
stdout=subprocess.PIPE)
127-
resp = p.communicate()
128-
email = resp[0].decode().rstrip()
129-
if email:
130-
maintainer.email = email
131-
if not maintainer.email:
132-
maintainer.email = maintainer.name + '@todo.todo'
117+
maintainer.email = args.maintainer_email or get_git_config('user.email') or f"{maintainer.name.replace(' ', '')}@todo.todo"
133118

134119
node_name = None
135120
library_name = None

0 commit comments

Comments
 (0)