main.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. use log::error;
  2. use serde_json::json;
  3. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  4. use structopt_toml::StructOptToml;
  5. use darkfi::{
  6. util::{
  7. cli::{log_config, spawn_config},
  8. path::get_config_path,
  9. },
  10. Result,
  11. };
  12. mod cli;
  13. mod filter;
  14. mod jsonrpc;
  15. mod primitives;
  16. mod util;
  17. mod view;
  18. use cli::CliTauSubCommands;
  19. use primitives::{TaskEvent, TaskInfo};
  20. use util::{desc_in_editor, CONFIG_FILE, CONFIG_FILE_CONTENTS};
  21. use view::{comments_as_string, print_list_of_task, print_task_info};
  22. async fn start(mut options: cli::CliTau) -> Result<()> {
  23. let rpc_addr = &format!("tcp://{}", &options.rpc_listen.clone());
  24. let states: Vec<String> = vec!["stop".into(), "open".into(), "pause".into()];
  25. match options.id {
  26. Some(id) if id.len() < 4 && id.parse::<u64>().is_ok() => {
  27. let task = jsonrpc::get_task_by_id(rpc_addr, id.parse::<u64>().unwrap()).await?;
  28. let taskinfo: TaskInfo = serde_json::from_value(task.clone())?;
  29. print_task_info(taskinfo)?;
  30. return Ok(())
  31. }
  32. Some(id) => options.filters.push(id),
  33. None => {}
  34. }
  35. match options.command {
  36. Some(CliTauSubCommands::Add { values }) => {
  37. let mut task = cli::task_from_cli_values(values)?;
  38. if task.title.is_empty() {
  39. error!("Provide a title for the task");
  40. return Ok(())
  41. };
  42. if task.desc.is_none() {
  43. task.desc = desc_in_editor()?;
  44. };
  45. jsonrpc::add(rpc_addr, json!([task])).await?;
  46. }
  47. Some(CliTauSubCommands::Update { id, values }) => {
  48. let task = cli::task_from_cli_values(values)?;
  49. jsonrpc::update(rpc_addr, id, json!([task])).await?;
  50. }
  51. Some(CliTauSubCommands::State { id, state }) => match state {
  52. Some(state) => {
  53. let state = state.trim().to_lowercase();
  54. if states.contains(&state) {
  55. jsonrpc::set_state(rpc_addr, id, &state).await?;
  56. } else {
  57. error!("Task state could only be one of three states: open, pause or stop");
  58. }
  59. }
  60. None => {
  61. let task = jsonrpc::get_task_by_id(rpc_addr, id).await?;
  62. let taskinfo: TaskInfo = serde_json::from_value(task.clone())?;
  63. let default_event = TaskEvent::default();
  64. let state = &taskinfo.events.last().unwrap_or(&default_event).action;
  65. println!("Task {}: {}", id, state);
  66. }
  67. },
  68. Some(CliTauSubCommands::Comment { id, content }) => match content {
  69. Some(content) => {
  70. jsonrpc::set_comment(rpc_addr, id, content.trim()).await?;
  71. }
  72. None => {
  73. let task = jsonrpc::get_task_by_id(rpc_addr, id).await?;
  74. let taskinfo: TaskInfo = serde_json::from_value(task.clone())?;
  75. let comments = comments_as_string(taskinfo.comments);
  76. println!("Comments {}:\n{}", id, comments);
  77. }
  78. },
  79. Some(CliTauSubCommands::List {}) | None => {
  80. let task_ids = jsonrpc::get_ids(rpc_addr, json!([])).await?;
  81. let mut tasks: Vec<TaskInfo> = vec![];
  82. if let Some(ids) = task_ids.as_array() {
  83. for id in ids {
  84. let id = if id.is_u64() { id.as_u64().unwrap() } else { continue };
  85. let task = jsonrpc::get_task_by_id(&rpc_addr, id).await?;
  86. let taskinfo: TaskInfo = serde_json::from_value(task.clone())?;
  87. tasks.push(taskinfo);
  88. }
  89. }
  90. // let mut tasks: Vec<TaskInfo> = serde_json::from_value(tasks)?;
  91. print_list_of_task(&mut tasks, options.filters)?;
  92. }
  93. }
  94. Ok(())
  95. }
  96. #[async_std::main]
  97. async fn main() -> Result<()> {
  98. let args = cli::CliTau::from_args_with_toml("").unwrap();
  99. let cfg_path = get_config_path(args.config, CONFIG_FILE)?;
  100. spawn_config(&cfg_path, CONFIG_FILE_CONTENTS.as_bytes())?;
  101. let args = cli::CliTau::from_args_with_toml(&std::fs::read_to_string(cfg_path)?).unwrap();
  102. let (lvl, conf) = log_config(args.verbose.into())?;
  103. TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  104. start(args).await
  105. }