Skip to content

Commit cab343c

Browse files
committed
feat(cli): allow options from args and environment variables
1 parent 9d6c188 commit cab343c

File tree

2 files changed

+140
-5
lines changed

2 files changed

+140
-5
lines changed

gitlab/__init__.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""Wrapper for the GitLab API."""
1818

1919
import importlib
20+
import os
2021
import time
2122
import warnings
2223

@@ -210,6 +211,55 @@ def from_config(cls, gitlab_id=None, config_files=None):
210211
user_agent=config.user_agent,
211212
)
212213

214+
@classmethod
215+
def merge_config(
216+
cls,
217+
options,
218+
gitlab_id=None,
219+
config_files=None,
220+
):
221+
"""Create a Gitlab connection by merging configuration in the following
222+
order:
223+
224+
1. Explicitly provided CLI arguments,
225+
2. Environment variables,
226+
3. Configuration files:
227+
a. explicitly defined config files:
228+
i. via the `--config-file` CLI argument,
229+
ii. via the `PYTHON_GITLAB_CFG` environment variable,
230+
b. user-specific config file,
231+
c. system-level config file,
232+
4. Environment variables always present in CI (CI_SERVER_URL, CI_JOB_TOKEN).
233+
234+
Args:
235+
options list[str]: List of options provided via the CLI.
236+
gitlab_id (str): ID of the configuration section.
237+
config_files list[str]: List of paths to configuration files.
238+
Returns:
239+
(gitlab.Gitlab): A Gitlab connection.
240+
241+
Raises:
242+
gitlab.config.GitlabDataError: If the configuration is not correct.
243+
"""
244+
config = gitlab.config.GitlabConfigParser(
245+
gitlab_id=gitlab_id, config_files=config_files
246+
)
247+
return cls(
248+
url=options.url or config.url or os.getenv("CI_SERVER_URL"),
249+
private_token=options.private_token or config.private_token,
250+
oauth_token=options.oauth_token or config.oauth_token,
251+
job_token=options.job_token
252+
or config.job_token
253+
or os.getenv("CI_JOB_TOKEN"),
254+
ssl_verify=options.ssl_verify or config.ssl_verify,
255+
timeout=options.timeout or config.timeout,
256+
api_version=options.api_version or config.api_version,
257+
per_page=options.per_page or config.per_page,
258+
pagination=options.pagination or config.pagination,
259+
order_by=options.order_by or config.order_by,
260+
user_agent=options.user_agent or config.user_agent,
261+
)
262+
213263
def auth(self):
214264
"""Performs an authentication using private token.
215265

gitlab/cli.py

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import argparse
2121
import functools
2222
import importlib
23+
import os
2324
import re
2425
import sys
2526

@@ -87,17 +88,22 @@ def _get_base_parser(add_help=True):
8788
"-v",
8889
"--verbose",
8990
"--fancy",
90-
help="Verbose mode (legacy format only)",
91+
help="Verbose mode (legacy format only) [env var: GITLAB_VERBOSE]",
9192
action="store_true",
93+
default=os.getenv("GITLAB_VERBOSE"),
9294
)
9395
parser.add_argument(
94-
"-d", "--debug", help="Debug mode (display HTTP requests)", action="store_true"
96+
"-d",
97+
"--debug",
98+
help="Debug mode (display HTTP requests) [env var: GITLAB_DEBUG]",
99+
action="store_true",
100+
default=os.getenv("GITLAB_DEBUG"),
95101
)
96102
parser.add_argument(
97103
"-c",
98104
"--config-file",
99105
action="append",
100-
help="Configuration file to use. Can be used multiple times.",
106+
help="Configuration file to use. Can be used multiple times. [env var: PYTHON_GITLAB_CFG]",
101107
)
102108
parser.add_argument(
103109
"-g",
@@ -126,7 +132,86 @@ def _get_base_parser(add_help=True):
126132
),
127133
required=False,
128134
)
129-
135+
# TODO: deduplicate everything by providing some data struct to loop over
136+
parser.add_argument(
137+
"--url",
138+
help=("GitLab server URL [env var: GITLAB_URL]"),
139+
required=False,
140+
default=os.getenv("GITLAB_URL"),
141+
)
142+
parser.add_argument(
143+
"--private-token",
144+
help=("GitLab private token [env var: GITLAB_PRIVATE_TOKEN]"),
145+
required=False,
146+
default=os.getenv("GITLAB_PRIVATE_TOKEN"),
147+
)
148+
parser.add_argument(
149+
"--oauth-token",
150+
help=("GitLab OAuth token [env var: GITLAB_OAUTH_TOKEN]"),
151+
required=False,
152+
default=os.getenv("GITLAB_OAUTH_TOKEN"),
153+
)
154+
parser.add_argument(
155+
"--job-token",
156+
help=(
157+
"GitLab CI job token. Explicitly providing this is usually not needed.\n"
158+
"[env var, only if explicitly overriding CI_JOB_TOKEN: GITLAB_JOB_TOKEN]"
159+
),
160+
required=False,
161+
default=os.getenv("GITLAB_JOB_TOKEN"),
162+
)
163+
parser.add_argument(
164+
"--ssl-verify",
165+
help=(
166+
"Whether SSL certificates should be validated. [env var: GITLAB_SSL_VERIFY]"
167+
),
168+
required=False,
169+
default=os.getenv("GITLAB_SSL_VERIFY"),
170+
)
171+
parser.add_argument(
172+
"--timeout",
173+
help=(
174+
"Timeout to use for requests to the GitLab server. [env var: GITLAB_TIMEOUT]"
175+
),
176+
required=False,
177+
default=os.getenv("GITLAB_TIMEOUT"),
178+
)
179+
parser.add_argument(
180+
"--api-version",
181+
help=("GitLab API version [env var: GITLAB_API_VERSION]"),
182+
required=False,
183+
default=os.getenv("GITLAB_API_VERSION"),
184+
)
185+
parser.add_argument(
186+
"--per-page",
187+
help=(
188+
"Number of entries to return per page in the response. [env var: GITLAB_PER_PAGE]"
189+
),
190+
required=False,
191+
default=os.getenv("GITLAB_PER_PAGE"),
192+
)
193+
parser.add_argument(
194+
"--pagination",
195+
help=(
196+
"Whether to use keyset or offset pagination [env var: GITLAB_PAGINATION]"
197+
),
198+
required=False,
199+
default=os.getenv("GITLAB_PAGINATION"),
200+
)
201+
parser.add_argument(
202+
"--order-by",
203+
help=("Set order_by globally [env var: GITLAB_ORDER_BY]"),
204+
required=False,
205+
default=os.getenv("GITLAB_ORDER_BY"),
206+
)
207+
parser.add_argument(
208+
"--user-agent",
209+
help=(
210+
"The user agent to send to GitLab with the HTTP request. [env var: GITLAB_USER_AGENT]"
211+
),
212+
required=False,
213+
default=os.getenv("GITLAB_USER_AGENT"),
214+
)
130215
return parser
131216

132217

@@ -220,7 +305,7 @@ def main():
220305
args = {k: _parse_value(v) for k, v in args.items() if v is not None}
221306

222307
try:
223-
gl = gitlab.Gitlab.from_config(gitlab_id, config_files)
308+
gl = gitlab.Gitlab.merge_config(options, gitlab_id, config_files)
224309
if gl.private_token or gl.oauth_token or gl.job_token:
225310
gl.auth()
226311
except Exception as e:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy