cli.rs 2.8 KB

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