Skip to content

Commit

Permalink
initial version of CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravm committed Jul 15, 2024
1 parent a76ddd3 commit 433dd90
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ ga4gh-lib = { path = "../lib" }
clap = "3.0"
clap_complete = "3.0"
tokio = { version = "1", features = ["full"] }
serde_json = "^1.0"
tempfile = "3.2"

[[bin]]
name = "cli"
Expand Down
99 changes: 91 additions & 8 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use clap::{arg, Command};
use std::error::Error;
use crate::tes;
use ga4gh_sdk::tes;
use ga4gh_sdk::tes::models::TesTask;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut cmd = Command::new("cli")
run_cli(Command::new("cli"))
}
fn run_cli(cmd: Command) -> Result<(), Box<dyn Error>> {
let cmd = cmd
.bin_name("cli")
.version("1.0")
.about("CLI to manage tasks")
Expand Down Expand Up @@ -32,7 +39,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
Some(("create", sub)) => {
let task_file = sub.value_of("TASK_FILE").unwrap();
let url = sub.value_of("url").unwrap();
tes::create(task_file, url).await?;
let task_json = task_file.to_string();
let task: TesTask = serde_json::from_str(&task_json).expect("JSON was not well-formatted");
// config.set_base_path(&funnel_url);
// let tes = match TES::new(&config).await {
// Ok(tes) => tes,
// Err(e) => {
// println!("Error creating TES instance: {:?}", e);
// return Err(e);
// }
// };
// tes::create(task).await?;
}
_ => {}
}
Expand All @@ -42,9 +59,75 @@ async fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}

// lib/src/tes/mod.rs
pub async fn create(task_file: &str, url: &str) -> Result<(), Box<dyn Error>> {
// Your implementation here
println!("Creating task with file: {} and URL: {}", task_file, url);
Ok(())

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_create_task() {
// Create a temporary directory to store the task file
let temp_dir = tempdir().expect("Failed to create temporary directory");
let task_file_path = temp_dir.path().join("task.json");

// Create a sample task JSON
let task_json = r#"{
"name": "Hello world",
"inputs": [{
"url": "s3://funnel-bucket/hello.txt",
"path": "/inputs/hello.txt"
}],
"outputs": [{
"url": "s3://funnel-bucket/output.txt",
"path": "/outputs/stdout"
}],
"executors": [{
"image": "alpine",
"command": ["cat", "/inputs/hello.txt"],
"stdout": "/outputs/stdout"
}]
}"#;

// Write the task JSON to the temporary file
let mut task_file = File::create(&task_file_path).expect("Failed to create task file");
task_file
.write_all(task_json.as_bytes())
.expect("Failed to write task JSON to file");

// Call the create function with the temporary file path and a sample URL
let url = "http://localhost:8000";
let cmd = Command::new("cli")
.bin_name("cli")
.version("1.0")
.about("CLI to manage tasks")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("tes")
.about("TES subcommands")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("create")
.about("Create a task")
.arg(arg!(<TASK_FILE> "The task file to create"))
.arg(arg!(--url <URL> "The URL for the task").required(true))
.arg_required_else_help(true),
),
);

let matches = cmd.clone().get_matches_from(&[
"cli",
"tes",
"create",
task_file_path.to_str().unwrap(),
"--url",
"http://localhost:8000",
]);

// Call the run_cli function with the simulated arguments
assert!(run_cli(cmd).is_ok());

// Additional assertions or verifications can be added here
}
}
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ cargo-nextest = "0.9.30"

[lib]
name = "ga4gh_sdk"
path = "lib/src/lib.rs"
path = "src/lib.rs"
1 change: 1 addition & 0 deletions lib/src/tes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ mod tests {
let mut config = Configuration::default();
let funnel_url = ensure_funnel_running().await;
config.set_base_path(&funnel_url);
println!("{:?}", funnel_url);
let tes = match TES::new(&config).await {
Ok(tes) => tes,
Err(e) => {
Expand Down

0 comments on commit 433dd90

Please sign in to comment.