|
1 | 1 | import os |
2 | 2 | import subprocess |
| 3 | +from configparser import ConfigParser |
| 4 | +from pathlib import Path |
3 | 5 |
|
4 | 6 | from pkgcore.repository import errors as repo_errors |
5 | 7 | from snakeoil.cli.arghparse import ArgumentParser |
@@ -41,3 +43,40 @@ def _determine_git_repo(parser, namespace): |
41 | 43 | pass |
42 | 44 |
|
43 | 45 | namespace.git_repo = path |
| 46 | + |
| 47 | + |
| 48 | +class BugzillaApiKey: |
| 49 | + @classmethod |
| 50 | + def mangle_argparser(cls, parser): |
| 51 | + parser.add_argument( |
| 52 | + "--api-key", |
| 53 | + metavar="KEY", |
| 54 | + help="Bugzilla API key", |
| 55 | + docs=""" |
| 56 | + The Bugzilla API key to use for authentication. WARNING: using this |
| 57 | + option will expose your API key to other users of the same system. |
| 58 | + Consider instead saving your API key in a file named ``~/.bugzrc`` |
| 59 | + in an INI format like so:: |
| 60 | +
|
| 61 | + [default] |
| 62 | + key = <your API key> |
| 63 | +
|
| 64 | + ANother supported option is to save your API key in a file named |
| 65 | + ``~/.bugz_token``. |
| 66 | + """, |
| 67 | + ) |
| 68 | + |
| 69 | + parser.bind_delayed_default(1000, "api_key")(cls._default_api_key) |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def _default_api_key(namespace, attr): |
| 73 | + """Use all known arches by default.""" |
| 74 | + if (bugz_rc_file := Path.home() / ".bugzrc").is_file(): |
| 75 | + try: |
| 76 | + config = ConfigParser(default_section="default") |
| 77 | + config.read(bugz_rc_file) |
| 78 | + setattr(namespace, attr, config.get("default", "key")) |
| 79 | + except Exception as e: |
| 80 | + raise ValueError(f"failed parsing {bugz_rc_file}: {e}") |
| 81 | + elif (bugz_token_file := Path.home() / ".bugz_token").is_file(): |
| 82 | + setattr(namespace, attr, bugz_token_file.read_text().strip()) |
0 commit comments