Skip to content

Commit 175686e

Browse files
committed
Add support for retrieving the GitLab token from the system's keyring
1 parent 7582a43 commit 175686e

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

quark/subproject.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import xml.etree.ElementTree as ElementTree
1414
import zipfile
1515
from os.path import exists, isdir, join
16-
from subprocess import PIPE, CalledProcessError, Popen
16+
from subprocess import PIPE, CalledProcessError, Popen, check_output
1717
from urllib.parse import urlparse
1818

1919
from quark.utils import DirectoryContext as cd
@@ -826,7 +826,36 @@ def _gitlab_setup(self, url):
826826
self.gitlab_token = os.environ[var]
827827
return
828828

829-
raise QuarkError("Missing authentication token. Please set one of the following environment variables: %s" % (", ".join(env_vars)))
829+
# Try to retrieve the token from the system's keyring if the "keyring" package
830+
# is available. The CLI is used instead of the "keyring" module to avoid
831+
# PYTHONPATH, virtualenv, and import issues that could cause unexpected
832+
# failures.
833+
if shutil.which("keyring"):
834+
try:
835+
self.gitlab_token = check_output(["keyring", "get", "comelz/quark", "gitlab-token"]).decode("utf-8").strip() # fmt: skip
836+
return
837+
except CalledProcessError:
838+
pass
839+
840+
msg = """
841+
Missing GitLab authentication token.
842+
843+
Please set one of the following environment variables: %s.
844+
845+
You can also install the Python "keyring" package and store the token inside the
846+
system's keyring like so:
847+
848+
keyring set comelz/quark gitlab-token
849+
850+
Then enter the token when prompted.
851+
852+
The keyring package is available:
853+
- On Debian / Ubuntu: python3-keyring
854+
- On macOS: brew install keyring
855+
- On PyPI: https://pypi.org/project/keyring/
856+
""" % (", ".join(env_vars))
857+
858+
raise QuarkError(msg)
830859

831860
def _parse_url(self, url):
832861
fragments = Subproject._parse_fragment(url) if url.fragment else {}

0 commit comments

Comments
 (0)