Skip to content

Commit f47878f

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

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

quark/subproject.py

+32-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,37 @@ 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.
831+
try:
832+
import keyring
833+
834+
token = keyring.get_password("comelz/quark", "gitlab-token")
835+
if token:
836+
self.gitlab_token = token
837+
return
838+
except ImportError:
839+
pass
840+
841+
msg = """
842+
Missing GitLab authentication token.
843+
844+
Please set one of the following environment variables: %s.
845+
846+
You can also install the Python "keyring" package and store the token inside the
847+
system's keyring like so:
848+
849+
keyring set comelz/quark gitlab-token
850+
851+
Then enter the token when prompted.
852+
853+
The keyring package is available:
854+
- On Ubuntu / Debian: python3-keyring
855+
- On macOS: brew install keyring
856+
- On PyPI: https://pypi.org/project/keyring/
857+
""" % (", ".join(env_vars))
858+
859+
raise QuarkError(msg)
830860

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

0 commit comments

Comments
 (0)