diff options
author | Nejc Habjan <hab.nejc@gmail.com> | 2021-02-12 00:47:32 +0100 |
---|---|---|
committer | John Villalovos <john@sodarock.com> | 2022-01-02 14:35:39 -0800 |
commit | ca58008607385338aaedd14a58adc347fa1a41a0 (patch) | |
tree | 3597e79d38ad47a0841c5a773950897863a87490 /gitlab/cli.py | |
parent | e19e4d7cdf9cd04359cd3e95036675c81f4e1dc5 (diff) | |
download | gitlab-ca58008607385338aaedd14a58adc347fa1a41a0.tar.gz |
feat(cli): allow options from args and environment variables
BREAKING-CHANGE: The gitlab CLI will now accept CLI arguments
and environment variables for its global options in addition
to configuration file options. This may change behavior for
some workflows such as running inside GitLab CI and with
certain environment variables configured.
Diffstat (limited to 'gitlab/cli.py')
-rw-r--r-- | gitlab/cli.py | 108 |
1 files changed, 103 insertions, 5 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py index c1a1334..a48b53b 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -19,6 +19,7 @@ import argparse import functools +import os import re import sys from types import ModuleType @@ -112,17 +113,25 @@ def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser: "-v", "--verbose", "--fancy", - help="Verbose mode (legacy format only)", + help="Verbose mode (legacy format only) [env var: GITLAB_VERBOSE]", action="store_true", + default=os.getenv("GITLAB_VERBOSE"), ) parser.add_argument( - "-d", "--debug", help="Debug mode (display HTTP requests)", action="store_true" + "-d", + "--debug", + help="Debug mode (display HTTP requests) [env var: GITLAB_DEBUG]", + action="store_true", + default=os.getenv("GITLAB_DEBUG"), ) parser.add_argument( "-c", "--config-file", action="append", - help="Configuration file to use. Can be used multiple times.", + help=( + "Configuration file to use. Can be used multiple times. " + "[env var: PYTHON_GITLAB_CFG]" + ), ) parser.add_argument( "-g", @@ -151,7 +160,86 @@ def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser: ), required=False, ) + parser.add_argument( + "--server-url", + help=("GitLab server URL [env var: GITLAB_URL]"), + required=False, + default=os.getenv("GITLAB_URL"), + ) + parser.add_argument( + "--ssl-verify", + help=( + "Whether SSL certificates should be validated. [env var: GITLAB_SSL_VERIFY]" + ), + required=False, + default=os.getenv("GITLAB_SSL_VERIFY"), + ) + parser.add_argument( + "--timeout", + help=( + "Timeout to use for requests to the GitLab server. " + "[env var: GITLAB_TIMEOUT]" + ), + required=False, + default=os.getenv("GITLAB_TIMEOUT"), + ) + parser.add_argument( + "--api-version", + help=("GitLab API version [env var: GITLAB_API_VERSION]"), + required=False, + default=os.getenv("GITLAB_API_VERSION"), + ) + parser.add_argument( + "--per-page", + help=( + "Number of entries to return per page in the response. " + "[env var: GITLAB_PER_PAGE]" + ), + required=False, + default=os.getenv("GITLAB_PER_PAGE"), + ) + parser.add_argument( + "--pagination", + help=( + "Whether to use keyset or offset pagination [env var: GITLAB_PAGINATION]" + ), + required=False, + default=os.getenv("GITLAB_PAGINATION"), + ) + parser.add_argument( + "--order-by", + help=("Set order_by globally [env var: GITLAB_ORDER_BY]"), + required=False, + default=os.getenv("GITLAB_ORDER_BY"), + ) + parser.add_argument( + "--user-agent", + help=( + "The user agent to send to GitLab with the HTTP request. " + "[env var: GITLAB_USER_AGENT]" + ), + required=False, + default=os.getenv("GITLAB_USER_AGENT"), + ) + tokens = parser.add_mutually_exclusive_group() + tokens.add_argument( + "--private-token", + help=("GitLab private access token [env var: GITLAB_PRIVATE_TOKEN]"), + required=False, + default=os.getenv("GITLAB_PRIVATE_TOKEN"), + ) + tokens.add_argument( + "--oauth-token", + help=("GitLab OAuth token [env var: GITLAB_OAUTH_TOKEN]"), + required=False, + default=os.getenv("GITLAB_OAUTH_TOKEN"), + ) + tokens.add_argument( + "--job-token", + help=("GitLab CI job token [env var: CI_JOB_TOKEN]"), + required=False, + ) return parser @@ -243,13 +331,23 @@ def main() -> None: "whaction", "version", "output", + "fields", + "server_url", + "ssl_verify", + "timeout", + "api_version", + "pagination", + "user_agent", + "private_token", + "oauth_token", + "job_token", ): args_dict.pop(item) args_dict = {k: _parse_value(v) for k, v in args_dict.items() if v is not None} try: - gl = gitlab.Gitlab.from_config(gitlab_id, config_files) - if gl.private_token or gl.oauth_token or gl.job_token: + gl = gitlab.Gitlab.merge_config(vars(options), gitlab_id, config_files) + if gl.private_token or gl.oauth_token: gl.auth() except Exception as e: die(str(e)) |