-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
989e3d0
commit c2dc39c
Showing
6 changed files
with
177 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use crate::{ | ||
download::download_tar_gz, | ||
error::{self, Result}, | ||
graphql_client::GraphQLClient, | ||
}; | ||
use clap::Args; | ||
use graphql_client::GraphQLQuery; | ||
use indicatif::{MultiProgress, ProgressBar}; | ||
use std::path::PathBuf; | ||
use url::Url; | ||
|
||
#[derive(GraphQLQuery)] | ||
#[graphql( | ||
query_path = "src/graphql/get_competition_template.graphql", | ||
schema_path = "src/graphql/schema.graphql", | ||
response_derives = "Debug" | ||
)] | ||
pub struct GetCompetitionTemplate; | ||
|
||
#[derive(Args, Debug)] | ||
#[command(author, version, about)] | ||
pub struct Template { | ||
#[arg(short, long, default_value = "https://app.aqora.io")] | ||
pub url: String, | ||
pub competition: String, | ||
pub destination: Option<PathBuf>, | ||
} | ||
|
||
pub async fn template(args: Template) -> Result<()> { | ||
let client = GraphQLClient::new(args.url.parse()?).await?; | ||
|
||
let destination = args | ||
.destination | ||
.unwrap_or_else(|| PathBuf::from(args.competition.clone())); | ||
|
||
if destination.exists() | ||
&& (destination.is_file() | ||
|| destination.is_symlink() | ||
|| tokio::fs::read_dir(&destination) | ||
.await? | ||
.next_entry() | ||
.await? | ||
.is_some()) | ||
{ | ||
return Err(error::user( | ||
&format!("Destination '{}' already exists", destination.display()), | ||
"Please specify a different destination", | ||
)); | ||
} | ||
|
||
let m = MultiProgress::new(); | ||
let mut pb = ProgressBar::new_spinner().with_message("Fetching competition..."); | ||
pb.enable_steady_tick(std::time::Duration::from_millis(100)); | ||
pb = m.add(pb); | ||
|
||
let use_case = client | ||
.send::<GetCompetitionTemplate>(get_competition_template::Variables { | ||
slug: args.competition.clone(), | ||
}) | ||
.await? | ||
.competition_by_slug | ||
.ok_or_else(|| { | ||
error::user( | ||
&format!("Competition '{}' not found", &args.competition), | ||
"Please make sure the competition exists", | ||
) | ||
})? | ||
.use_case | ||
.latest | ||
.ok_or_else(|| { | ||
error::system( | ||
"No use case found", | ||
"Please contact the competition organizer", | ||
) | ||
})?; | ||
|
||
let download_url = use_case | ||
.files | ||
.into_iter() | ||
.find(|file| { | ||
matches!( | ||
file.kind, | ||
get_competition_template::ProjectVersionFileKind::TEMPLATE | ||
) | ||
}) | ||
.ok_or_else(|| { | ||
error::system( | ||
"No template found", | ||
"Please contact the competition organizer", | ||
) | ||
})? | ||
.download_url; | ||
|
||
pb.set_message("Downloading competition template..."); | ||
|
||
download_tar_gz(download_url, &destination).await?; | ||
|
||
pb.finish_with_message(format!( | ||
"Competition template downloaded to {}", | ||
destination.display() | ||
)); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{ | ||
compress::decompress, | ||
error::{self, Result}, | ||
}; | ||
use futures::prelude::*; | ||
use std::path::Path; | ||
use url::Url; | ||
|
||
pub async fn download_tar_gz(url: Url, dir: impl AsRef<Path>) -> Result<()> { | ||
tokio::fs::create_dir_all(&dir).await.map_err(|e| { | ||
error::user( | ||
&format!( | ||
"Failed to create directory {}: {}", | ||
dir.as_ref().display(), | ||
e | ||
), | ||
"Please make sure you have permission to create directories in this directory", | ||
) | ||
})?; | ||
let client = reqwest::Client::new(); | ||
let mut byte_stream = client | ||
.get(url) | ||
.send() | ||
.await | ||
.map_err(|e| { | ||
error::user( | ||
&format!("Failed to download data: {e}"), | ||
"Check your internet connection and try again", | ||
) | ||
})? | ||
.error_for_status() | ||
.map_err(|e| error::system(&format!("Failed to download data: {e}"), ""))? | ||
.bytes_stream(); | ||
let tempfile = tempfile::NamedTempFile::new().map_err(|e| { | ||
error::user( | ||
&format!("Failed to create temporary file: {e}"), | ||
"Please make sure you have permission to create files in this directory", | ||
) | ||
})?; | ||
let mut tar_file = tokio::fs::File::create(tempfile.path()).await?; | ||
while let Some(item) = byte_stream.next().await { | ||
tokio::io::copy(&mut item?.as_ref(), &mut tar_file).await?; | ||
} | ||
decompress(tempfile.path(), &dir).await.map_err(|e| { | ||
error::user( | ||
&format!("Failed to decompress data: {e}"), | ||
"Please make sure you have permission to create files in this directory", | ||
) | ||
})?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
query GetCompetitionTemplate($slug: String!) { | ||
competitionBySlug(slug: $slug) { | ||
id | ||
useCase { | ||
latest { | ||
files { | ||
kind | ||
downloadUrl | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters