1+ import glob
12from os import getcwd , walk
23from os .path import dirname , split , join as path_join , abspath
4+ from pathlib import Path
35
46from click import command , argument , option , style
57
68from cloudinary_cli .utils .api_utils import upload_file
79from cloudinary_cli .utils .utils import parse_option_value , logger , run_tasks_concurrently
8- from cloudinary_cli .utils .file_utils import get_destination_folder
10+ from cloudinary_cli .utils .file_utils import get_destination_folder , is_hidden_path
911
1012
1113@command ("upload_dir" , help = """Upload a folder of assets, maintaining the folder structure.""" )
1214@argument ("directory" , default = "." )
15+ @option ("-g" , "--glob-pattern" , default = "**/*" , help = "The glob pattern. "
16+ "For example use '**/*.jpg' to upload only jpg files." )
17+ @option ("-H" , "--include-hidden" , is_flag = True , help = "Include hidden files." )
1318@option ("-o" , "--optional_parameter" , multiple = True , nargs = 2 , help = "Pass optional parameters as raw strings." )
1419@option ("-O" , "--optional_parameter_parsed" ,
1520 multiple = True ,
2227 "Any folders that do not exist are automatically created." )
2328@option ("-p" , "--preset" , help = "The upload preset to use." )
2429@option ("-w" , "--concurrent_workers" , type = int , default = 30 , help = "Specify the number of concurrent network threads." )
25- def upload_dir (directory , optional_parameter , optional_parameter_parsed , transformation , folder , preset ,
26- concurrent_workers ):
30+ def upload_dir (directory , glob_pattern , include_hidden , optional_parameter , optional_parameter_parsed , transformation ,
31+ folder , preset , concurrent_workers ):
2732 items , skipped = {}, {}
28- dir_to_upload = abspath (path_join (getcwd (), directory ))
33+
34+ dir_to_upload = Path (path_join (getcwd (), directory ))
2935 logger .info ("Uploading directory '{}'" .format (dir_to_upload ))
3036 parent = dirname (dir_to_upload )
3137 options = {
@@ -41,14 +47,12 @@ def upload_dir(directory, optional_parameter, optional_parameter_parsed, transfo
4147
4248 uploads = []
4349
44- for root , _ , files in walk (dir_to_upload ):
45- for fi in files :
46- file_path = abspath (path_join (dir_to_upload , root , fi ))
47-
48- if split (file_path )[1 ][0 ] == "." :
50+ for file_path in dir_to_upload .glob (glob_pattern ):
51+ if file_path .is_file ():
52+ if not include_hidden and is_hidden_path (file_path ):
4953 continue
5054
51- options = {** options , "folder" : get_destination_folder (folder , file_path , parent = parent )}
55+ options = {** options , "folder" : get_destination_folder (folder , str ( file_path ) , parent = parent )}
5256 uploads .append ((file_path , options , items , skipped ))
5357
5458 run_tasks_concurrently (upload_file , uploads , concurrent_workers )
0 commit comments