| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c7b3f96 commit 748d57e
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -158,7 +158,6 @@ Example: | |||
| 158 | 158 | ||
| 159 | 159 | $ gitlab -o yaml -f id,permissions -g elsewhere -c /tmp/gl.cfg project list | |
| 160 | 160 | ||
| 161 | - | ||
| 162 | 161 | Examples | |
| 163 | 162 | ======== | |
| 164 | 163 | ||
@@ -235,3 +234,18 @@ Use sudo to act as another user (admin only): | |||
| 235 | 234 | .. code-block:: console | |
| 236 | 235 | ||
| 237 | 236 | $ 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,6 +114,19 @@ def _get_parser(cli_module): | |||
| 114 | 114 | return cli_module.extend_parser(parser) | |
| 115 | 115 | ||
| 116 | 116 | ||
| 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 | + | ||
| 117 | 130 | def main(): | |
| 118 | 131 | if "--version" in sys.argv: | |
| 119 | 132 | print(gitlab.__version__) | |
@@ -143,7 +156,7 @@ def main(): | |||
| 143 | 156 | for item in ('gitlab', 'config_file', 'verbose', 'debug', 'what', 'action', | |
| 144 | 157 | 'version', 'output'): | |
| 145 | 158 | 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} | ||
| 147 | 160 | ||
| 148 | 161 | try: | |
| 149 | 162 | gl = gitlab.Gitlab.from_config(gitlab_id, config_files) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,8 @@ | |||
| 20 | 20 | from __future__ import absolute_import | |
| 21 | 21 | ||
| 22 | 22 | import argparse | |
| 23 | + import os | ||
| 24 | + import tempfile | ||
| 23 | 25 | ||
| 24 | 26 | import six | |
| 25 | 27 | try: | |
@@ -52,6 +54,29 @@ def test_die(self): | |||
| 52 | 54 | ||
| 53 | 55 | self.assertEqual(test.exception.code, 1) | |
| 54 | 56 | ||
| 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 | + | ||
| 55 | 80 | def test_base_parser(self): | |
| 56 | 81 | parser = cli._get_base_parser() | |
| 57 | 82 | args = parser.parse_args(['-v', '-g', 'gl_id', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,5 +108,16 @@ testcase "application settings get" ' | |||
| 108 | 108 | ' | |
| 109 | 109 | ||
| 110 | 110 | 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" | ||
| 112 | 123 | ' | |
| Back | FazBrowse Home | New Git URL |
0 commit comments