cli.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use serde::{Deserialize, Serialize};
  2. use structopt::StructOpt;
  3. use structopt_toml::StructOptToml;
  4. use darkfi::Result;
  5. use super::util::due_as_timestamp;
  6. /// Tau cli
  7. #[derive(Debug, Deserialize, StructOpt, StructOptToml)]
  8. #[serde(default)]
  9. #[structopt(name = "tau")]
  10. pub struct CliTau {
  11. /// Increase verbosity
  12. #[structopt(short, parse(from_occurrences))]
  13. pub verbose: u8,
  14. /// JSON-RPC listen URL
  15. #[structopt(long = "rpc", default_value = "tcp://127.0.0.1:11055")]
  16. pub rpc_listen: String,
  17. /// Sets a custom config file
  18. #[structopt(short, long)]
  19. pub config: Option<String>,
  20. #[structopt(subcommand)]
  21. pub command: Option<CliTauSubCommands>,
  22. /// Get task by ID
  23. pub id: Option<String>,
  24. /// Search criteria (zero or more)
  25. #[structopt(multiple = true)]
  26. pub filters: Vec<String>,
  27. }
  28. #[derive(StructOpt, Deserialize, Debug)]
  29. pub enum CliTauSubCommands {
  30. /// Add a new task
  31. Add { values: Vec<String> },
  32. /// Update/Edit an existing task by ID
  33. Update {
  34. /// Task ID
  35. id: u64,
  36. /// Values (ex: project:blockchain)
  37. values: Vec<String>,
  38. },
  39. /// Set or Get task state
  40. State {
  41. /// Task ID
  42. id: u64,
  43. /// Set task state
  44. state: Option<String>,
  45. },
  46. /// Set or Get comment for a task
  47. Comment {
  48. /// Task ID
  49. id: u64,
  50. /// Comment content
  51. content: Option<String>,
  52. },
  53. /// List all tasks
  54. List {},
  55. }
  56. #[derive(Serialize, Debug, Clone)]
  57. pub struct CliBaseTask {
  58. pub title: String,
  59. pub desc: Option<String>,
  60. pub assign: Vec<String>,
  61. pub project: Vec<String>,
  62. pub due: Option<i64>,
  63. pub rank: Option<f32>,
  64. }
  65. pub fn task_from_cli_values(values: Vec<String>) -> Result<CliBaseTask> {
  66. let mut title: String = String::new();
  67. let mut desc: Option<String> = None;
  68. let mut project: Vec<String> = vec![];
  69. let mut assign: Vec<String> = vec![];
  70. let mut due: Option<i64> = None;
  71. let mut rank: Option<f32> = None;
  72. for val in values {
  73. let field: Vec<&str> = val.split(':').collect();
  74. if field.len() == 1 {
  75. title = field[0].into();
  76. continue
  77. }
  78. if field.len() != 2 {
  79. continue
  80. }
  81. if field[0].starts_with("project") {
  82. project = field[1].split(',').map(|s| s.into()).collect();
  83. }
  84. if field[0].starts_with("desc") {
  85. desc = Some(field[1].into());
  86. }
  87. if field[0].starts_with("assign") {
  88. assign = field[1].split(',').map(|s| s.into()).collect();
  89. }
  90. if field[0].starts_with("due") {
  91. due = due_as_timestamp(&field[1])
  92. }
  93. if field[0].starts_with("rank") {
  94. rank = Some(field[1].parse::<f32>()?);
  95. }
  96. }
  97. let task = CliBaseTask { title, desc, project, assign, due, rank };
  98. Ok(task)
  99. }