main.rs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #[clap(allow_hyphen_values = true)]
  69. /// Values (e.g. project:blockchain).
  70. values: Vec<String>,
  71. },
  72. /// List tasks.
  73. List,
  74. /// Start task(s).
  75. Start,
  76. /// Open task(s).
  77. Open,
  78. /// Pause task(s).
  79. Pause,
  80. /// Stop task(s).
  81. Stop,
  82. /// Set or Get comment for task(s).
  83. Comment {
  84. /// Set comment content if provided (Get comments otherwise).
  85. content: Vec<String>,
  86. },
  87. /// Get all data about selected task(s).
  88. Info,
  89. /// Switch workspace.
  90. Switch {
  91. /// Tau workspace.
  92. workspace: String,
  93. },
  94. /// Import tasks from a specified directory.
  95. Import {
  96. /// The parent directory from where you want to import tasks.
  97. path: Option<String>,
  98. },
  99. /// Export tasks to a specified directory.
  100. Export {
  101. /// The parent directory to where you want to export tasks.
  102. path: Option<String>,
  103. },
  104. /// Log drawdown.
  105. Log {
  106. /// The month in which we want to draw a heatmap (e.g. 0822 for August 2022).
  107. month: Option<String>,
  108. /// The person of which we want to draw a heatmap
  109. /// (if not provided we list all assignees).
  110. assignee: Option<String>,
  111. },
  112. }
  113. pub struct Tau {
  114. pub rpc_client: RpcClient,
  115. }
  116. #[async_std::main]
  117. async fn main() -> Result<()> {
  118. let args = Args::parse();
  119. let log_level = get_log_level(args.verbose.into());
  120. let log_config = get_log_config();
  121. TermLogger::init(log_level, log_config, TerminalMode::Mixed, ColorChoice::Auto)?;
  122. let rpc_client = RpcClient::new(args.endpoint).await?;
  123. let tau = Tau { rpc_client };
  124. let mut filters = args.filters.clone();
  125. // If IDs are provided in filter we use them to get the tasks from the daemon
  126. // then remove IDs from filter so we can do apply_filter() normally.
  127. // If not provided we use get_ids() to get them from the daemon.
  128. let ids = get_ids(&mut filters)?;
  129. let task_ids = if ids.is_empty() { tau.get_ids().await? } else { ids };
  130. let mut tasks =
  131. if filters.contains(&"state:stop".to_string()) || filters.contains(&"all".to_string()) {
  132. tau.get_stop_tasks(None).await?
  133. } else {
  134. vec![]
  135. };
  136. for id in task_ids {
  137. tasks.push(tau.get_task_by_id(id).await?);
  138. }
  139. for filter in filters {
  140. apply_filter(&mut tasks, &filter);
  141. }
  142. // Parse subcommands
  143. match args.command {
  144. Some(sc) => match sc {
  145. TauSubcommand::Add { values } => {
  146. let mut task = task_from_cli(values)?;
  147. if task.title.is_empty() {
  148. error!("Please provide a title for the task.");
  149. exit(1);
  150. };
  151. if task.desc.is_none() {
  152. task.desc = desc_in_editor()?;
  153. };
  154. return tau.add(task).await
  155. }
  156. TauSubcommand::Modify { values } => {
  157. if args.filters.is_empty() {
  158. no_filter_warn()
  159. }
  160. let base_task = task_from_cli(values)?;
  161. for task in tasks {
  162. tau.update(task.id.into(), base_task.clone()).await?;
  163. }
  164. Ok(())
  165. }
  166. TauSubcommand::Start => {
  167. if args.filters.is_empty() {
  168. no_filter_warn()
  169. }
  170. let state = State::Start;
  171. for task in tasks {
  172. tau.set_state(task.id.into(), &state).await?;
  173. }
  174. Ok(())
  175. }
  176. TauSubcommand::Open => {
  177. if args.filters.is_empty() {
  178. no_filter_warn()
  179. }
  180. let state = State::Open;
  181. for task in tasks {
  182. tau.set_state(task.id.into(), &state).await?;
  183. }
  184. Ok(())
  185. }
  186. TauSubcommand::Pause => {
  187. if args.filters.is_empty() {
  188. no_filter_warn()
  189. }
  190. let state = State::Pause;
  191. for task in tasks {
  192. tau.set_state(task.id.into(), &state).await?;
  193. }
  194. Ok(())
  195. }
  196. TauSubcommand::Stop => {
  197. if args.filters.is_empty() {
  198. no_filter_warn()
  199. }
  200. let state = State::Stop;
  201. for task in tasks {
  202. tau.set_state(task.id.into(), &state).await?;
  203. }
  204. Ok(())
  205. }
  206. TauSubcommand::Comment { content } => {
  207. if args.filters.is_empty() {
  208. no_filter_warn()
  209. }
  210. for task in tasks {
  211. if content.is_empty() {
  212. let task = tau.get_task_by_id(task.id.into()).await?;
  213. let comments = comments_as_string(task.comments);
  214. println!("Comments {}:\n{}", task.id, comments);
  215. } else {
  216. tau.set_comment(task.id.into(), &content.join(" ")).await?;
  217. }
  218. }
  219. Ok(())
  220. }
  221. TauSubcommand::Info => {
  222. for task in tasks {
  223. let task = tau.get_task_by_id(task.id.into()).await?;
  224. print_task_info(task)?;
  225. }
  226. Ok(())
  227. }
  228. TauSubcommand::Switch { workspace } => tau.switch_ws(workspace).await,
  229. TauSubcommand::Export { path } => {
  230. let path = path.unwrap_or_else(|| DEFAULT_PATH.into());
  231. let res = tau.export_to(path.clone()).await?;
  232. if res {
  233. info!("Exported to {}", path);
  234. } else {
  235. error!("Error exporting to {}", path);
  236. }
  237. Ok(())
  238. }
  239. TauSubcommand::Import { path } => {
  240. let path = path.unwrap_or_else(|| DEFAULT_PATH.into());
  241. let res = tau.import_from(path.clone()).await?;
  242. if res {
  243. info!("Imported from {}", path);
  244. } else {
  245. error!("Error importing from {}", path);
  246. }
  247. Ok(())
  248. }
  249. TauSubcommand::Log { month, assignee } => {
  250. match month {
  251. Some(date) => {
  252. let ts = to_naivedate(date.clone())?.and_hms(12, 0, 0).timestamp();
  253. let tasks = tau.get_stop_tasks(Some(ts)).await?;
  254. drawdown(date, tasks, assignee)?;
  255. }
  256. None => {
  257. let ws = tau.get_ws().await?;
  258. let tasks = tau.get_stop_tasks(None).await?;
  259. print_task_list(tasks, ws)?;
  260. }
  261. }
  262. Ok(())
  263. }
  264. TauSubcommand::List => {
  265. let ws = tau.get_ws().await?;
  266. print_task_list(tasks, ws)
  267. }
  268. },
  269. None => {
  270. let ws = tau.get_ws().await?;
  271. print_task_list(tasks, ws)
  272. }
  273. }?;
  274. tau.close_connection().await
  275. }