Skip to content

Commit

Permalink
add task accept, decline and finish subcommands (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
trusch authored Dec 5, 2024
1 parent 58b0aed commit bd5d684
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 10 deletions.
117 changes: 107 additions & 10 deletions src/commands/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;

use gevulot_rs::builders::{ByteSize, ByteUnit, MsgCreateTaskBuilder};
use gevulot_rs::builders::{
ByteSize, ByteUnit, MsgAcceptTaskBuilder, MsgCreateTaskBuilder, MsgDeclineTaskBuilder, MsgFinishTaskBuilder,
};

use crate::{connect_to_gevulot, print_object, read_file};

Expand Down Expand Up @@ -39,10 +41,13 @@ pub async fn get_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::erro
let task: gevulot_rs::models::Task = task.into();
print_object(_sub_m, &task)?;
} else {
print_object(_sub_m, &serde_json::json!({
"status": "error",
"message": "Task ID is required"
}))?;
print_object(
_sub_m,
&serde_json::json!({
"status": "error",
"message": "Task ID is required"
}),
)?;
}
Ok(())
}
Expand Down Expand Up @@ -112,10 +117,102 @@ pub async fn create_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::e
)
.await?;

print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": "Task created successfully",
"task_id": resp.id
}))?;
print_object(
_sub_m,
&serde_json::json!({
"status": "success",
"message": "Task created successfully",
"task_id": resp.id
}),
)?;
Ok(())
}

pub async fn accept_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let mut client = connect_to_gevulot(_sub_m).await?;
let me = client
.base_client
.write()
.await
.address
.clone()
.ok_or("No address found, did you set a mnemonic?")?;

let task_id = _sub_m.get_one::<String>("id").unwrap();
let worker_id = _sub_m.get_one::<String>("worker_id").unwrap();
client
.tasks
.accept(
MsgAcceptTaskBuilder::default()
.creator(me.clone())
.task_id(task_id.clone())
.worker_id(worker_id.clone())
.into_message()?,
)
.await?;
Ok(())
}

pub async fn decline_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let mut client = connect_to_gevulot(_sub_m).await?;
let me = client
.base_client
.write()
.await
.address
.clone()
.ok_or("No address found, did you set a mnemonic?")?;

let task_id = _sub_m.get_one::<String>("id").unwrap();
let worker_id = _sub_m.get_one::<String>("worker_id").unwrap();
client
.tasks
.decline(
MsgDeclineTaskBuilder::default()
.creator(me.clone())
.task_id(task_id.clone())
.worker_id(worker_id.clone())
.into_message()?,
)
.await?;
Ok(())
}

pub async fn finish_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let mut client = connect_to_gevulot(_sub_m).await?;
let me = client
.base_client
.write()
.await
.address
.clone()
.ok_or("No address found, did you set a mnemonic?")?;

let task_id = _sub_m.get_one::<String>("id").unwrap();
let exit_code = _sub_m.get_one::<i32>("exit_code").cloned();
let stdout = _sub_m.get_one::<String>("stdout").cloned();
let stderr = _sub_m.get_one::<String>("stderr").cloned();
let error = _sub_m.get_one::<String>("error").cloned();
let output_contexts: Vec<String> = _sub_m
.get_many::<String>("output_contexts")
.unwrap_or_default()
.into_iter()
.map(|e| e.to_string())
.collect();

client
.tasks
.finish(
MsgFinishTaskBuilder::default()
.creator(me.clone())
.task_id(task_id.clone())
.exit_code(exit_code.unwrap_or(0))
.stdout(stdout.unwrap_or_default())
.stderr(stderr.unwrap_or_default())
.output_contexts(output_contexts)
.error(error.unwrap_or_default())
.into_message()?,
)
.await?;
Ok(())
}
101 changes: 101 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(("list", sub_m)) => list_tasks(sub_m).await?,
Some(("get", sub_m)) => get_task(sub_m).await?,
Some(("create", sub_m)) => create_task(sub_m).await?,
Some(("accept", sub_m)) => accept_task(sub_m).await?,
Some(("decline", sub_m)) => decline_task(sub_m).await?,
Some(("finish", sub_m)) => finish_task(sub_m).await?,
_ => println!("Unknown task command"),
},
Some(("workflow", sub_m)) => match sub_m.subcommand() {
Expand Down Expand Up @@ -260,6 +263,88 @@ fn setup_command_line_args() -> Result<Command, Box<dyn std::error::Error>> {
.action(ArgAction::Set),
)
.args(&chain_args),
)
.subcommand(
Command::new("accept")
.about("Accept a task (you probably should not use this)")
.arg(
Arg::new("id")
.value_name("ID")
.help("The ID of the task to accept")
.required(true)
.index(1),
)
.arg(
Arg::new("worker_id")
.value_name("WORKER_ID")
.help("The ID of the worker accepting the task")
.required(true)
.index(2),
)
.args(&chain_args),
)
.subcommand(
Command::new("decline")
.about("Decline a task (you probably should not use this)")
.arg(
Arg::new("id")
.value_name("ID")
.help("The ID of the task to decline")
.required(true)
.index(1),
)
.arg(
Arg::new("worker_id")
.value_name("WORKER_ID")
.help("The ID of the worker declining the task")
.required(true)
.index(2),
)
.args(&chain_args),
)
.subcommand(
Command::new("finish")
.about("Finish a task (you probably should not use this)")
.arg(
Arg::new("id")
.value_name("ID")
.help("The ID of the task to finish")
.required(true)
.index(1),
)
.arg(
Arg::new("exit_code")
.value_name("EXIT_CODE")
.help("The exit code of the task")
.value_parser(value_parser!(i32))
.required(false),
)
.arg(
Arg::new("stdout")
.value_name("STDOUT")
.help("The stdout output of the task")
.required(false),
)
.arg(
Arg::new("stderr")
.value_name("STDERR")
.help("The stderr output of the task")
.required(false),
)
.arg(
Arg::new("error")
.value_name("ERROR")
.help("Any error message from the task")
.required(false),
)
.arg(
Arg::new("output_contexts")
.value_name("OUTPUT_CONTEXTS")
.help("Output contexts produced by the task")
.required(false)
.action(ArgAction::Append),
)
.args(&chain_args),
),
)
// Workflow subcommand
Expand Down Expand Up @@ -327,6 +412,14 @@ fn setup_command_line_args() -> Result<Command, Box<dyn std::error::Error>> {
.value_hint(ValueHint::Other)
.action(ArgAction::Set)
.global(true),
)
.arg(
Arg::new("format")
.short('F')
.long("format")
.value_name("FORMAT")
.default_value("yaml")
.help("Sets the output format (yaml, json, prettyjson, toml)"),
),
)
.subcommand(
Expand All @@ -348,6 +441,14 @@ fn setup_command_line_args() -> Result<Command, Box<dyn std::error::Error>> {
.env("GEVULOT_PASSWORD")
.help("The password to compute the key with")
.value_hint(ValueHint::Other),
)
.arg(
Arg::new("format")
.short('F')
.long("format")
.value_name("FORMAT")
.default_value("yaml")
.help("Sets the output format (yaml, json, prettyjson, toml"),
),
)
// Send subcommand
Expand Down

0 comments on commit bd5d684

Please sign in to comment.