summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2018-03-05 17:35:41 +0100
committerGauvain Pocentek <gauvain@pocentek.net>2018-03-05 17:35:41 +0100
commit748d57ee64036305a84301db7211b713c1995391 (patch)
tree1ecb834a0db215a1bbb784f3e8d45ad2ab80f183 /gitlab
parentc7b3f969fc3fcf9d057a23638d121f51513bb13c (diff)
downloadgitlab-748d57ee64036305a84301db7211b713c1995391.tar.gz
[cli] Allow to read args from files
With the @/file/path syntax (similar to curl) user can provide values from attributes in files. Fixes #448
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/cli.py15
-rw-r--r--gitlab/tests/test_cli.py25
2 files changed, 39 insertions, 1 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py
index af82c09..36a1bda 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -114,6 +114,19 @@ def _get_parser(cli_module):
return cli_module.extend_parser(parser)
+def _parse_value(v):
+ if isinstance(v, str) and v.startswith('@'):
+ # If the user-provided value starts with @, we try to read the file
+ # path provided after @ as the real value. Exit on any error.
+ try:
+ return open(v[1:]).read()
+ except Exception as e:
+ sys.stderr.write("%s\n" % e)
+ sys.exit(1)
+
+ return v
+
+
def main():
if "--version" in sys.argv:
print(gitlab.__version__)
@@ -143,7 +156,7 @@ def main():
for item in ('gitlab', 'config_file', 'verbose', 'debug', 'what', 'action',
'version', 'output'):
args.pop(item)
- args = {k: v for k, v in args.items() if v is not None}
+ args = {k: _parse_value(v) for k, v in args.items() if v is not None}
try:
gl = gitlab.Gitlab.from_config(gitlab_id, config_files)
diff --git a/gitlab/tests/test_cli.py b/gitlab/tests/test_cli.py
index e6e290a..b8062b3 100644
--- a/gitlab/tests/test_cli.py
+++ b/gitlab/tests/test_cli.py
@@ -20,6 +20,8 @@ from __future__ import print_function
from __future__ import absolute_import
import argparse
+import os
+import tempfile
import six
try:
@@ -52,6 +54,29 @@ class TestCLI(unittest.TestCase):
self.assertEqual(test.exception.code, 1)
+ def test_parse_value(self):
+ ret = cli._parse_value('foobar')
+ self.assertEqual(ret, 'foobar')
+
+ ret = cli._parse_value(True)
+ self.assertEqual(ret, True)
+
+ ret = cli._parse_value(1)
+ self.assertEqual(ret, 1)
+
+ ret = cli._parse_value(None)
+ self.assertEqual(ret, None)
+
+ fd, temp_path = tempfile.mkstemp()
+ os.write(fd, b'content')
+ os.close(fd)
+ ret = cli._parse_value('@%s' % temp_path)
+ self.assertEqual(ret, 'content')
+ os.unlink(temp_path)
+
+ with self.assertRaises(SystemExit):
+ cli._parse_value('@/thisfileprobablydoesntexist')
+
def test_base_parser(self):
parser = cli._get_base_parser()
args = parser.parse_args(['-v', '-g', 'gl_id',