main.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{process::exit, sync::Arc};
  19. use clap::{Parser, Subcommand};
  20. use log::{error, info};
  21. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  22. use smol::Executor;
  23. use url::Url;
  24. use darkfi::{
  25. rpc::client::RpcClient,
  26. util::cli::{get_log_config, get_log_level},
  27. Result,
  28. };
  29. mod drawdown;
  30. mod filter;
  31. mod primitives;
  32. mod rpc;
  33. mod util;
  34. mod view;
  35. use drawdown::{drawdown, to_naivedate};
  36. use filter::{apply_filter, get_ids, no_filter_warn};
  37. use primitives::{task_from_cli, State, TaskEvent};
  38. use util::{due_as_timestamp, prompt_text};
  39. use view::{print_task_info, print_task_list};
  40. use taud::task_info::TaskInfo;
  41. const DEFAULT_PATH: &str = "~/tau_exported_tasks";
  42. #[derive(Parser)]
  43. #[clap(name = "tau", version)]
  44. #[clap(subcommand_precedence_over_arg = true)]
  45. struct Args {
  46. #[arg(short, action = clap::ArgAction::Count)]
  47. /// Increase verbosity (-vvv supported)
  48. verbose: u8,
  49. #[clap(short, long, default_value = "tcp://127.0.0.1:23330")]
  50. /// taud JSON-RPC endpoint
  51. endpoint: Url,
  52. /// Search filters (zero or more)
  53. filters: Vec<String>,
  54. #[clap(subcommand)]
  55. command: Option<TauSubcommand>,
  56. }
  57. #[derive(Subcommand)]
  58. enum TauSubcommand {
  59. /// Add a new task.
  60. ///
  61. /// Quick start:
  62. /// Adding a new task named "New task":
  63. /// tau add New task
  64. /// New task with description:
  65. /// tau add Add more info to tau desc:"some awesome description"
  66. /// New task with project and assignee:
  67. /// tau add Third task project:p2p Arusty
  68. /// Add a task with due date September 12th and rank of 4.6:
  69. /// tau add Task no. Four due:1209 rank:4.6
  70. ///
  71. /// Notice that if the command does not have "desc" key it will open
  72. /// an Editor so you can write the description there.
  73. ///
  74. /// Also note that "project" key can have multiple
  75. /// comma-separated values.
  76. /// "assign" on the other hand uses '@' character but also could be
  77. /// multiple values, but like so:
  78. /// @person1 @person2
  79. ///
  80. /// All keys example:
  81. /// tau add Improve CLI desc:"Description here" project:tau,darkirc @dave @rusty due:0210 rank:2.2
  82. ///
  83. #[clap(verbatim_doc_comment)]
  84. Add {
  85. /// Pairs of key:value (e.g. desc:description @dark).
  86. values: Vec<String>,
  87. },
  88. /// Modify/Edit an existing task.
  89. Modify {
  90. #[clap(allow_hyphen_values = true)]
  91. /// Values (e.g. project:blockchain).
  92. values: Vec<String>,
  93. },
  94. /// List tasks.
  95. List,
  96. /// Start task(s).
  97. Start,
  98. /// Open task(s).
  99. Open,
  100. /// Pause task(s).
  101. Pause,
  102. /// Stop task(s).
  103. Stop,
  104. /// Set or Get comment for task(s).
  105. Comment {
  106. /// Set comment content if provided (Get comments otherwise).
  107. content: Vec<String>,
  108. },
  109. /// Get all data about selected task(s).
  110. Info,
  111. /// Switch workspace.
  112. Switch {
  113. /// Tau workspace.
  114. workspace: String,
  115. },
  116. /// Import tasks from a specified directory.
  117. Import {
  118. /// The parent directory from where you want to import tasks.
  119. path: Option<String>,
  120. },
  121. /// Export tasks to a specified directory.
  122. Export {
  123. /// The parent directory to where you want to export tasks.
  124. path: Option<String>,
  125. },
  126. /// Log drawdown.
  127. Log {
  128. /// The month in which we want to draw a heatmap (e.g. 0822 for August 2022).
  129. month: Option<String>,
  130. /// The person of which we want to draw a heatmap
  131. /// (if not provided we list all assignees).
  132. assignee: Option<String>,
  133. },
  134. }
  135. pub struct Tau {
  136. pub rpc_client: RpcClient,
  137. }
  138. fn main() -> Result<()> {
  139. let args = Args::parse();
  140. let log_level = get_log_level(args.verbose);
  141. let log_config = get_log_config(args.verbose);
  142. TermLogger::init(log_level, log_config, TerminalMode::Mixed, ColorChoice::Auto)?;
  143. let executor = Arc::new(Executor::new());
  144. smol::block_on(executor.run(async {
  145. let rpc_client = RpcClient::new(args.endpoint, executor.clone()).await?;
  146. let tau = Tau { rpc_client };
  147. let mut filters = args.filters.clone();
  148. // If IDs are provided in filter we use them to get the tasks from the daemon
  149. // then remove IDs from filter so we can do apply_filter() normally.
  150. // If not provided we use get_ids() to get them from the daemon.
  151. let ids = get_ids(&mut filters)?;
  152. let ids_clone = ids.clone();
  153. let task_ids = if ids.is_empty() { tau.get_ids().await? } else { ids };
  154. let mut tasks = if filters.contains(&"state:stop".to_string()) ||
  155. filters.contains(&"all".to_string())
  156. {
  157. tau.get_stop_tasks(None).await?
  158. } else {
  159. vec![]
  160. };
  161. for id in task_ids {
  162. tasks.push(tau.get_task_by_id(id).await?);
  163. }
  164. if ids_clone.len() == 1 && args.command.is_none() {
  165. let tsk = tasks[0].clone();
  166. print_task_info(tsk)?;
  167. return Ok(())
  168. }
  169. for filter in filters {
  170. apply_filter(&mut tasks, &filter);
  171. }
  172. // Parse subcommands
  173. match args.command {
  174. Some(sc) => match sc {
  175. TauSubcommand::Add { values } => {
  176. let mut task = task_from_cli(values)?;
  177. if task.title.is_empty() {
  178. error!("Please provide a title for the task.");
  179. exit(1);
  180. };
  181. if task.desc.is_none() {
  182. task.desc = prompt_text(TaskInfo::from(task.clone()), "description")?;
  183. };
  184. if task.clone().desc.unwrap().trim().is_empty() {
  185. error!("Abort adding the task due to empty description.");
  186. exit(1)
  187. }
  188. let title = task.clone().title;
  189. let task_id = tau.add(task).await?;
  190. if task_id > 0 {
  191. println!("Created task {} \"{}\"", task_id, title);
  192. }
  193. Ok(())
  194. }
  195. TauSubcommand::Modify { values } => {
  196. if args.filters.is_empty() {
  197. no_filter_warn()
  198. }
  199. let base_task = task_from_cli(values)?;
  200. for task in tasks.clone() {
  201. let res = tau.update(task.id, base_task.clone()).await?;
  202. if res {
  203. let tsk = tau.get_task_by_id(task.id).await?;
  204. print_task_info(tsk)?;
  205. }
  206. }
  207. Ok(())
  208. }
  209. TauSubcommand::Start => {
  210. if args.filters.is_empty() {
  211. no_filter_warn()
  212. }
  213. let state = State::Start;
  214. for task in tasks {
  215. if tau.set_state(task.id, &state).await? {
  216. println!("Started task: {:?}", task.id);
  217. }
  218. }
  219. Ok(())
  220. }
  221. TauSubcommand::Open => {
  222. if args.filters.is_empty() {
  223. no_filter_warn()
  224. }
  225. let state = State::Open;
  226. for task in tasks {
  227. if tau.set_state(task.id, &state).await? {
  228. println!("Opened task: {:?}", task.id);
  229. }
  230. }
  231. Ok(())
  232. }
  233. TauSubcommand::Pause => {
  234. if args.filters.is_empty() {
  235. no_filter_warn()
  236. }
  237. let state = State::Pause;
  238. for task in tasks {
  239. if tau.set_state(task.id, &state).await? {
  240. println!("Paused task: {:?}", task.id);
  241. }
  242. }
  243. Ok(())
  244. }
  245. TauSubcommand::Stop => {
  246. if args.filters.is_empty() {
  247. no_filter_warn()
  248. }
  249. let state = State::Stop;
  250. for task in tasks {
  251. if tau.set_state(task.id, &state).await? {
  252. println!("Stopped task: {}", task.id);
  253. }
  254. }
  255. Ok(())
  256. }
  257. TauSubcommand::Comment { content } => {
  258. if args.filters.is_empty() {
  259. no_filter_warn()
  260. }
  261. for task in tasks {
  262. let comment = if content.is_empty() {
  263. prompt_text(task.clone(), "comment")?
  264. } else {
  265. Some(content.join(" "))
  266. };
  267. if comment.clone().unwrap().trim().is_empty() || comment.is_none() {
  268. error!("Abort due to empty comment.");
  269. exit(1)
  270. }
  271. let res = tau.set_comment(task.id, comment.unwrap().trim()).await?;
  272. if res {
  273. let tsk = tau.get_task_by_id(task.id).await?;
  274. print_task_info(tsk)?;
  275. }
  276. }
  277. Ok(())
  278. }
  279. TauSubcommand::Info => {
  280. for task in tasks {
  281. let task = tau.get_task_by_id(task.id).await?;
  282. print_task_info(task)?;
  283. }
  284. Ok(())
  285. }
  286. TauSubcommand::Switch { workspace } => {
  287. if tau.switch_ws(workspace.clone()).await? {
  288. println!("You are now on \"{}\" workspace", workspace);
  289. } else {
  290. println!("Workspace \"{}\" is not configured", workspace);
  291. }
  292. Ok(())
  293. }
  294. TauSubcommand::Export { path } => {
  295. let path = path.unwrap_or_else(|| DEFAULT_PATH.into());
  296. let res = tau.export_to(path.clone()).await?;
  297. if res {
  298. info!("Exported to {}", path);
  299. } else {
  300. error!("Error exporting to {}", path);
  301. }
  302. Ok(())
  303. }
  304. TauSubcommand::Import { path } => {
  305. let path = path.unwrap_or_else(|| DEFAULT_PATH.into());
  306. let res = tau.import_from(path.clone()).await?;
  307. if res {
  308. info!("Imported from {}", path);
  309. } else {
  310. error!("Error importing from {}", path);
  311. }
  312. Ok(())
  313. }
  314. TauSubcommand::Log { month, assignee } => {
  315. match month {
  316. Some(date) => {
  317. let ts = to_naivedate(date.clone())?
  318. .and_hms_opt(12, 0, 0)
  319. .unwrap()
  320. .timestamp();
  321. let tasks = tau.get_stop_tasks(Some(ts.try_into().unwrap())).await?;
  322. drawdown(date, tasks, assignee)?;
  323. }
  324. None => {
  325. let ws = tau.get_ws().await?;
  326. let tasks = tau.get_stop_tasks(None).await?;
  327. print_task_list(tasks, ws)?;
  328. }
  329. }
  330. Ok(())
  331. }
  332. TauSubcommand::List => {
  333. let ws = tau.get_ws().await?;
  334. print_task_list(tasks, ws)
  335. }
  336. },
  337. None => {
  338. let ws = tau.get_ws().await?;
  339. print_task_list(tasks, ws)
  340. }
  341. }?;
  342. tau.close_connection().await;
  343. Ok(())
  344. }))
  345. }