main.rs 12 KB

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