Skip to content

Commit df74cc4

Browse files
chrissnyder2337const-cloudinaryjackieros
authored
Add support for --exclude-dir-name option in upload_dir
* Add ability to upload contents of directory only without creating the parent directory in Cloudinary --------- Co-authored-by: Constantine Nathanson <[email protected]> Co-authored-by: Jackie Rosenzveig <[email protected]>
1 parent 6875d9b commit df74cc4

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

cloudinary_cli/modules/upload_dir.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,29 @@
2323
@option("-f", "--folder", default="",
2424
help="The Cloudinary folder where you want to upload the assets. "
2525
"You can specify a whole path, for example folder1/folder2/folder3. "
26-
"Any folders that do not exist are automatically created.")
26+
"If your product environment uses fixed folder mode, then any folders that do not exist are automatically created.")
2727
@option("-p", "--preset", help="The upload preset to use.")
28+
@option("-e", "--exclude-dir-name", is_flag=True, default=False,
29+
help="Don't include the selected parent directory name in the public ID path of the uploaded files."
30+
"This ensures that the public ID paths of the uploaded assets will be directly under the specified --(f)older, avoiding an extraneous level in the path."
31+
"When this option is used, the contents of the parent directory are uploaded instead of the parent directory itself and thus the name of the specified parent directory is not included in the pubic ID path of the uploaded assets.")
2832
@option("-w", "--concurrent_workers", type=int, default=30, help="Specify the number of concurrent network threads.")
2933
def upload_dir(directory, glob_pattern, include_hidden, optional_parameter, optional_parameter_parsed, transformation,
30-
folder, preset, concurrent_workers):
34+
folder, preset, concurrent_workers, exclude_dir_name):
3135
items, skipped = {}, {}
3236

3337
dir_to_upload = Path(path_join(getcwd(), directory))
3438
if not dir_to_upload.exists():
3539
logger.error(f"Directory: {dir_to_upload} does not exist")
3640
return False
3741

38-
logger.info(f"Uploading directory '{dir_to_upload}'")
39-
parent = dirname(dir_to_upload)
42+
if exclude_dir_name:
43+
logger.info(f"Uploading contents of directory '{dir_to_upload}'")
44+
parent = dir_to_upload
45+
else:
46+
logger.info(f"Uploading directory '{dir_to_upload}'")
47+
parent = dirname(dir_to_upload)
48+
4049
options = {
4150
**{k: v for k, v in optional_parameter},
4251
**{k: parse_option_value(v) for k, v in optional_parameter_parsed},

test/test_modules/test_cli_upload_dir.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,18 @@ def test_cli_upload_dir_glob(self):
3636

3737
self.assertEqual(0, result.exit_code)
3838
self.assertIn("11 resources uploaded", result.output)
39+
40+
def test_upload_dir_without_exclude_dir_name_option(self):
41+
result = self.runner.invoke(cli, ["upload_dir", TEST_FILES_DIR, "-f", self.CLD_UPLOAD_DIR])
42+
43+
self.assertEqual(0, result.exit_code)
44+
self.assertIn("12 resources uploaded", result.output)
45+
self.assertIn("as " + self.CLD_UPLOAD_DIR + "/test_sync/", result.output)
46+
47+
def test_upload_dir_with_exclude_dir_name_option(self):
48+
result = self.runner.invoke(cli, ["upload_dir", TEST_FILES_DIR, "-e", "-f", self.CLD_UPLOAD_DIR])
49+
50+
self.assertEqual(0, result.exit_code)
51+
self.assertIn("12 resources uploaded", result.output)
52+
self.assertIn("as " + self.CLD_UPLOAD_DIR, result.output)
53+
self.assertNotIn("as " + self.CLD_UPLOAD_DIR + "/test_sync/", result.output)

0 commit comments

Comments
 (0)