diff --git a/copier.yml b/copier.yml index b36070b..c1574ea 100644 --- a/copier.yml +++ b/copier.yml @@ -13,6 +13,7 @@ _jinja_extensions: ### question ### full_name: + default: "{{ git_user_name }}" type: str help: Enter your full name. This will appear in the License generated and `pyproject.toml`'s authors field. @@ -22,7 +23,7 @@ email: help: Your E-mail address. This will appear in the License generated and `pyproject.toml`'s authors field. github_username: - default: "{{ git_user_name }}" + default: "{{ github_username }}" type: str help: You Github username or organization name under which the projects lives. diff --git a/extensions.py b/extensions.py index e73881e..7aeed29 100644 --- a/extensions.py +++ b/extensions.py @@ -2,6 +2,7 @@ # Thanks to the original author import re +import shutil import subprocess import unicodedata from datetime import date @@ -17,6 +18,19 @@ def git_user_email() -> str: return subprocess.getoutput("git config user.email").strip() +def github_username() -> str: + # run if gh is installed + if not shutil.which("gh"): + return "" + import json + + data = subprocess.getoutput("gh api user").strip() + try: + return json.loads(data)["login"] + except json.JSONDecodeError: + return "" + + def slugify(value, separator="-"): value = ( unicodedata.normalize("NFKD", str(value)) @@ -30,8 +44,9 @@ def slugify(value, separator="-"): class GitExtension(Extension): def __init__(self, environment): super().__init__(environment) - environment.globals["git_user_name"] = git_user_name - environment.globals["git_user_email"] = git_user_email + environment.globals["git_user_name"] = git_user_name() + environment.globals["git_user_email"] = git_user_email() + environment.globals["github_username"] = github_username() class SlugifyExtension(Extension):