feat(bootstrap): improve bucket creation and error handling logic

Enhance the bucket creation process in `BootstrapManager` to handle existing buckets gracefully. Adds logic to reuse the default template when a bucket already exists and ensures proper path formatting before creation. This improves reliability and prevents redundant bucket creation errors.
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-10-29 14:49:25 -03:00
parent 205cd13b49
commit 0330b8fdb8

View file

@ -395,14 +395,27 @@ impl BootstrapManager {
let bot_name = path.file_name().unwrap().to_string_lossy().to_string(); let bot_name = path.file_name().unwrap().to_string_lossy().to_string();
let bucket = bot_name.trim_start_matches('/').to_string(); let bucket = bot_name.trim_start_matches('/').to_string();
info!("Uploading template {} to Drive bucket {}", bot_name, bucket); info!("Uploading template {} to Drive bucket {}", bot_name, bucket);
if operator.stat(&bucket).await.is_err() { if operator.stat(&bucket).await.is_err() {
info!("Bucket {} not found, creating it", bucket); info!("Bucket {} not found, creating it", bucket);
operator.create_dir("/").await?; let bucket_path = if bucket.ends_with('/') { bucket.clone() } else { format!("{}/", bucket) };
debug!("Bucket {} created successfully", bucket); match operator.create_dir(&bucket_path).await {
} Ok(_) => {
self.upload_directory_recursive(&operator, &path, &bucket) debug!("Bucket {} created successfully", bucket);
.await?; }
info!("Uploaded template {} to Drive bucket {}", bot_name, bucket); Err(e) => {
let err_msg = format!("{}", e);
if err_msg.contains("BucketAlreadyOwnedByYou") {
log::warn!("Bucket {} already exists, reusing default.gbai", bucket);
self.upload_directory_recursive(&operator, &Path::new("templates/default.gbai"), "default.gbai").await?;
continue;
} else {
return Err(e.into());
}
}
}
}
self.upload_directory_recursive(&operator, &path, &bucket).await?;
info!("Uploaded template {} to Drive bucket {}", bot_name, bucket);
} }
} }
Ok(()) Ok(())