|
| 1 | +from click import command, option, style, argument |
| 2 | +from cloudinary_cli.utils.utils import normalize_list_params, print_help_and_exit |
| 3 | +import cloudinary |
| 4 | +from cloudinary_cli.utils.utils import run_tasks_concurrently |
| 5 | +from cloudinary_cli.utils.api_utils import upload_file |
| 6 | +from cloudinary_cli.utils.config_utils import load_config, get_cloudinary_config, config_to_dict |
| 7 | +from cloudinary_cli.defaults import logger |
| 8 | +from cloudinary_cli.core.search import execute_single_request, handle_auto_pagination |
| 9 | + |
| 10 | +DEFAULT_MAX_RESULTS = 500 |
| 11 | + |
| 12 | + |
| 13 | +@command("clone", |
| 14 | + short_help="""Clone assets from one product environment to another.""", |
| 15 | + help=""" |
| 16 | +\b |
| 17 | +Clone assets from one product environment to another with/without tags and/or context (structured metadata is not currently supported). |
| 18 | +Source will be your `CLOUDINARY_URL` environment variable but you also can specify a different source using the `-c/-C` option. |
| 19 | +Cloning restricted assets is also not supported currently. |
| 20 | +Format: cld clone <target_environment> <command options> |
| 21 | +`<target_environment>` can be a CLOUDINARY_URL or a saved config (see `config` command) |
| 22 | +Example 1 (Copy all assets including tags and context using CLOUDINARY URL): |
| 23 | + cld clone cloudinary://<api_key>:<api_secret>@<cloudname> -fi tags,context |
| 24 | +Example 2 (Copy all assets with a specific tag via a search expression using a saved config): |
| 25 | + cld clone <config_name> -se "tags:<tag_name>" |
| 26 | +""") |
| 27 | +@argument("target") |
| 28 | +@option("-F", "--force", is_flag=True, |
| 29 | + help="Skip confirmation.") |
| 30 | +@option("-ow", "--overwrite", is_flag=True, default=False, |
| 31 | + help="Specify whether to overwrite existing assets.") |
| 32 | +@option("-w", "--concurrent_workers", type=int, default=30, |
| 33 | + help="Specify the number of concurrent network threads.") |
| 34 | +@option("-fi", "--fields", multiple=True, |
| 35 | + help="Specify whether to copy tags and/or context. Valid options: `tags,context`.") |
| 36 | +@option("-se", "--search_exp", default="", |
| 37 | + help="Define a search expression to filter the assets to clone.") |
| 38 | +@option("--async", "async_", is_flag=True, default=False, |
| 39 | + help="Clone the assets asynchronously.") |
| 40 | +@option("-nu", "--notification_url", |
| 41 | + help="Webhook notification URL.") |
| 42 | +def clone(target, force, overwrite, concurrent_workers, fields, search_exp, async_, notification_url): |
| 43 | + if not target: |
| 44 | + print_help_and_exit() |
| 45 | + |
| 46 | + target_config = get_cloudinary_config(target) |
| 47 | + if not target_config: |
| 48 | + logger.error("The specified config does not exist or the CLOUDINARY_URL scheme provided is invalid" |
| 49 | + " (expecting to start with 'cloudinary://').") |
| 50 | + return False |
| 51 | + |
| 52 | + if cloudinary.config().cloud_name == target_config.cloud_name: |
| 53 | + logger.error("Target environment cannot be the same as source environment.") |
| 54 | + return False |
| 55 | + |
| 56 | + source_assets = search_assets(force, search_exp) |
| 57 | + |
| 58 | + upload_list = [] |
| 59 | + for r in source_assets.get('resources'): |
| 60 | + updated_options, asset_url = process_metadata(r, overwrite, async_, notification_url, |
| 61 | + normalize_list_params(fields)) |
| 62 | + updated_options.update(config_to_dict(target_config)) |
| 63 | + upload_list.append((asset_url, {**updated_options})) |
| 64 | + |
| 65 | + if not upload_list: |
| 66 | + logger.error(style(f'No assets found in {cloudinary.config().cloud_name}', fg="red")) |
| 67 | + return False |
| 68 | + |
| 69 | + logger.info(style(f'Copying {len(upload_list)} asset(s) from {cloudinary.config().cloud_name} to {target_config.cloud_name}', fg="blue")) |
| 70 | + |
| 71 | + run_tasks_concurrently(upload_file, upload_list, concurrent_workers) |
| 72 | + |
| 73 | + return True |
| 74 | + |
| 75 | + |
| 76 | +def search_assets(force, search_exp): |
| 77 | + search = cloudinary.search.Search().expression(search_exp) |
| 78 | + search.fields(['tags', 'context', 'access_control', 'secure_url', 'display_name']) |
| 79 | + search.max_results(DEFAULT_MAX_RESULTS) |
| 80 | + |
| 81 | + res = execute_single_request(search, fields_to_keep="") |
| 82 | + res = handle_auto_pagination(res, search, force, fields_to_keep="") |
| 83 | + |
| 84 | + return res |
| 85 | + |
| 86 | + |
| 87 | +def process_metadata(res, overwrite, async_, notification_url, copy_fields=""): |
| 88 | + cloned_options = {} |
| 89 | + asset_url = res.get('secure_url') |
| 90 | + cloned_options['public_id'] = res.get('public_id') |
| 91 | + cloned_options['type'] = res.get('type') |
| 92 | + cloned_options['resource_type'] = res.get('resource_type') |
| 93 | + cloned_options['overwrite'] = overwrite |
| 94 | + cloned_options['async'] = async_ |
| 95 | + if "tags" in copy_fields: |
| 96 | + cloned_options['tags'] = res.get('tags') |
| 97 | + if "context" in copy_fields: |
| 98 | + cloned_options['context'] = res.get('context') |
| 99 | + if res.get('folder'): |
| 100 | + # This is required to put the asset in the correct asset_folder |
| 101 | + # when copying from a fixed to DF (dynamic folder) cloud as if |
| 102 | + # you just pass a `folder` param to a DF cloud, it will append |
| 103 | + # this to the `public_id` and we don't want this. |
| 104 | + cloned_options['asset_folder'] = res.get('folder') |
| 105 | + elif res.get('asset_folder'): |
| 106 | + cloned_options['asset_folder'] = res.get('asset_folder') |
| 107 | + if res.get('display_name'): |
| 108 | + cloned_options['display_name'] = res.get('display_name') |
| 109 | + if notification_url: |
| 110 | + cloned_options['notification_url'] = notification_url |
| 111 | + |
| 112 | + return cloned_options, asset_url |
0 commit comments