main.rs 9.3 KB

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