main.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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()?;
  170. };
  171. return tau.add(task).await
  172. }
  173. TauSubcommand::Modify { values } => {
  174. if args.filters.is_empty() {
  175. no_filter_warn()
  176. }
  177. let base_task = task_from_cli(values)?;
  178. for task in tasks {
  179. tau.update(task.id.into(), base_task.clone()).await?;
  180. }
  181. Ok(())
  182. }
  183. TauSubcommand::Start => {
  184. if args.filters.is_empty() {
  185. no_filter_warn()
  186. }
  187. let state = State::Start;
  188. for task in tasks {
  189. tau.set_state(task.id.into(), &state).await?;
  190. }
  191. Ok(())
  192. }
  193. TauSubcommand::Open => {
  194. if args.filters.is_empty() {
  195. no_filter_warn()
  196. }
  197. let state = State::Open;
  198. for task in tasks {
  199. tau.set_state(task.id.into(), &state).await?;
  200. }
  201. Ok(())
  202. }
  203. TauSubcommand::Pause => {
  204. if args.filters.is_empty() {
  205. no_filter_warn()
  206. }
  207. let state = State::Pause;
  208. for task in tasks {
  209. tau.set_state(task.id.into(), &state).await?;
  210. }
  211. Ok(())
  212. }
  213. TauSubcommand::Stop => {
  214. if args.filters.is_empty() {
  215. no_filter_warn()
  216. }
  217. let state = State::Stop;
  218. for task in tasks {
  219. tau.set_state(task.id.into(), &state).await?;
  220. }
  221. Ok(())
  222. }
  223. TauSubcommand::Comment { content } => {
  224. if args.filters.is_empty() {
  225. no_filter_warn()
  226. }
  227. for task in tasks {
  228. if content.is_empty() {
  229. let task = tau.get_task_by_id(task.id.into()).await?;
  230. let comments = comments_as_string(task.comments);
  231. println!("Comments {}:\n{}", task.id, comments);
  232. } else {
  233. tau.set_comment(task.id.into(), &content.join(" ")).await?;
  234. }
  235. }
  236. Ok(())
  237. }
  238. TauSubcommand::Info => {
  239. for task in tasks {
  240. let task = tau.get_task_by_id(task.id.into()).await?;
  241. print_task_info(task)?;
  242. }
  243. Ok(())
  244. }
  245. TauSubcommand::Switch { workspace } => tau.switch_ws(workspace).await,
  246. TauSubcommand::Export { path } => {
  247. let path = path.unwrap_or_else(|| DEFAULT_PATH.into());
  248. let res = tau.export_to(path.clone()).await?;
  249. if res {
  250. info!("Exported to {}", path);
  251. } else {
  252. error!("Error exporting to {}", path);
  253. }
  254. Ok(())
  255. }
  256. TauSubcommand::Import { path } => {
  257. let path = path.unwrap_or_else(|| DEFAULT_PATH.into());
  258. let res = tau.import_from(path.clone()).await?;
  259. if res {
  260. info!("Imported from {}", path);
  261. } else {
  262. error!("Error importing from {}", path);
  263. }
  264. Ok(())
  265. }
  266. TauSubcommand::Log { month, assignee } => {
  267. match month {
  268. Some(date) => {
  269. let ts =
  270. to_naivedate(date.clone())?.and_hms_opt(12, 0, 0).unwrap().timestamp();
  271. let tasks = tau.get_stop_tasks(Some(ts)).await?;
  272. drawdown(date, tasks, assignee)?;
  273. }
  274. None => {
  275. let ws = tau.get_ws().await?;
  276. let tasks = tau.get_stop_tasks(None).await?;
  277. print_task_list(tasks, ws)?;
  278. }
  279. }
  280. Ok(())
  281. }
  282. TauSubcommand::List => {
  283. let ws = tau.get_ws().await?;
  284. print_task_list(tasks, ws)
  285. }
  286. },
  287. None => {
  288. let ws = tau.get_ws().await?;
  289. print_task_list(tasks, ws)
  290. }
  291. }?;
  292. tau.close_connection().await
  293. }