Skip to content

Commit 748d57e

Browse files
author
Gauvain Pocentek
committed
[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
1 parent c7b3f96 commit 748d57e

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

docs/cli.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ Example:
158158
159159
$ gitlab -o yaml -f id,permissions -g elsewhere -c /tmp/gl.cfg project list
160160
161-
162161
Examples
163162
========
164163

@@ -235,3 +234,18 @@ Use sudo to act as another user (admin only):
235234
.. code-block:: console
236235
237236
$ gitlab project create --name user_project1 --sudo username
237+
238+
Reading values from files
239+
-------------------------
240+
241+
You can make ``gitlab`` read values from files instead of providing them on the
242+
command line. This is handy for values containing new lines for instance:
243+
244+
.. code-block:: console
245+
246+
$ cat > /tmp/description << EOF
247+
This is the description of my project.
248+
249+
It is obviously the best project around
250+
EOF
251+
$ gitlab project create --name SuperProject --description @/tmp/description

gitlab/cli.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ def _get_parser(cli_module):
114114
return cli_module.extend_parser(parser)
115115

116116

117+
def _parse_value(v):
118+
if isinstance(v, str) and v.startswith('@'):
119+
# If the user-provided value starts with @, we try to read the file
120+
# path provided after @ as the real value. Exit on any error.
121+
try:
122+
return open(v[1:]).read()
123+
except Exception as e:
124+
sys.stderr.write("%s\n" % e)
125+
sys.exit(1)
126+
127+
return v
128+
129+
117130
def main():
118131
if "--version" in sys.argv:
119132
print(gitlab.__version__)
@@ -143,7 +156,7 @@ def main():
143156
for item in ('gitlab', 'config_file', 'verbose', 'debug', 'what', 'action',
144157
'version', 'output'):
145158
args.pop(item)
146-
args = {k: v for k, v in args.items() if v is not None}
159+
args = {k: _parse_value(v) for k, v in args.items() if v is not None}
147160

148161
try:
149162
gl = gitlab.Gitlab.from_config(gitlab_id, config_files)

gitlab/tests/test_cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from __future__ import absolute_import
2121

2222
import argparse
23+
import os
24+
import tempfile
2325

2426
import six
2527
try:
@@ -52,6 +54,29 @@ def test_die(self):
5254

5355
self.assertEqual(test.exception.code, 1)
5456

57+
def test_parse_value(self):
58+
ret = cli._parse_value('foobar')
59+
self.assertEqual(ret, 'foobar')
60+
61+
ret = cli._parse_value(True)
62+
self.assertEqual(ret, True)
63+
64+
ret = cli._parse_value(1)
65+
self.assertEqual(ret, 1)
66+
67+
ret = cli._parse_value(None)
68+
self.assertEqual(ret, None)
69+
70+
fd, temp_path = tempfile.mkstemp()
71+
os.write(fd, b'content')
72+
os.close(fd)
73+
ret = cli._parse_value('@%s' % temp_path)
74+
self.assertEqual(ret, 'content')
75+
os.unlink(temp_path)
76+
77+
with self.assertRaises(SystemExit):
78+
cli._parse_value('@/thisfileprobablydoesntexist')
79+
5580
def test_base_parser(self):
5681
parser = cli._get_base_parser()
5782
args = parser.parse_args(['-v', '-g', 'gl_id',

tools/cli_test_v4.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,16 @@ testcase "application settings get" '
108108
'
109109

110110
testcase "application settings update" '
111-
GITLAB application-settings update --signup-enabled false
111+
GITLAB application-settings update --signup-enabled false >/dev/null 2>&1
112+
'
113+
114+
cat > /tmp/gitlab-project-description << EOF
115+
Multi line
116+
117+
Data
118+
EOF
119+
testcase "values from files" '
120+
OUTPUT=$(GITLAB -v project create --name fromfile \
121+
--description @/tmp/gitlab-project-description)
122+
echo $OUTPUT | grep -q "Multi line"
112123
'

0 commit comments

Comments
 (0)