feat: Add name default and manual configuration
This commit is contained in:
@@ -3,29 +3,35 @@ use std::path::Path;
|
||||
use crate::{
|
||||
cli::UploadInfo,
|
||||
commands::{
|
||||
connect::config::Config,
|
||||
connect::{config::Config, config_option::ConfigOption},
|
||||
upload::chunk_reader::ChunkReader,
|
||||
},
|
||||
manifest::generate_manifest, operator_builder::OperatorBuilder,
|
||||
manifest::{CompressionOption, DepotManifest, generate_v2_manifest},
|
||||
operator_builder::OperatorBuilder,
|
||||
};
|
||||
use futures::AsyncWriteExt;
|
||||
use log::info;
|
||||
use opendal::Operator;
|
||||
use tokio_util::compat::FuturesAsyncWriteCompatExt;
|
||||
|
||||
pub async fn upload(info: &UploadInfo, config: Config) -> anyhow::Result<()> {
|
||||
pub async fn upload(
|
||||
info: &UploadInfo,
|
||||
config: Config,
|
||||
name: &Option<String>,
|
||||
) -> anyhow::Result<()> {
|
||||
let game_id = &info.game_id;
|
||||
let path = &info.path;
|
||||
let version_id = &info.version_id;
|
||||
|
||||
let manifest = generate_manifest(Path::new(path)).await?;
|
||||
let operator = match info.upload_style {
|
||||
crate::cli::UploadStyle::S3 => config
|
||||
.get_active_s3()
|
||||
.ok_or(anyhow::Error::msg("Could not get active S3 value"))?
|
||||
.build()?,
|
||||
};
|
||||
let operator = get_operator(config, name)?;
|
||||
|
||||
let mut existing_depot_manifest = get_depot_manifest(&operator).await?;
|
||||
|
||||
let v2_manifest = generate_v2_manifest(Path::new(path)).await?;
|
||||
|
||||
info!("Uploading chunks");
|
||||
for (id, data) in &manifest.chunks {
|
||||
|
||||
for (id, data) in &v2_manifest.chunks {
|
||||
info!("Uploading chunk id {id}");
|
||||
let mut reader = ChunkReader::new(path, data);
|
||||
let mut writer = operator
|
||||
@@ -36,6 +42,35 @@ pub async fn upload(info: &UploadInfo, config: Config) -> anyhow::Result<()> {
|
||||
tokio::io::copy(&mut reader, &mut writer).await?;
|
||||
writer.into_inner().close().await?;
|
||||
}
|
||||
|
||||
info!("Finished uploading chunks");
|
||||
|
||||
existing_depot_manifest.append(
|
||||
game_id.to_string(),
|
||||
version_id.to_string(),
|
||||
CompressionOption::None,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_depot_manifest(operator: &Operator) -> Result<DepotManifest, anyhow::Error> {
|
||||
let existing_depot_manifest = operator.read("manifest.json").await?.to_bytes();
|
||||
let existing_depot_manifest: DepotManifest =
|
||||
serde_json::from_slice(existing_depot_manifest.as_ref())?;
|
||||
Ok(existing_depot_manifest)
|
||||
}
|
||||
|
||||
fn get_operator(config: Config, name: &Option<String>) -> anyhow::Result<Operator> {
|
||||
let operator = match if let Some(name) = name {
|
||||
config
|
||||
.get(name)
|
||||
.ok_or(anyhow::anyhow!("Name does not exist"))?
|
||||
} else {
|
||||
config.get_active().ok_or(anyhow::anyhow!(
|
||||
"No active connection set. Please specify with --name"
|
||||
))?
|
||||
} {
|
||||
ConfigOption::S3(s3_config) => s3_config.build()?,
|
||||
};
|
||||
Ok(operator)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user