main.rs 12 KB

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