Sfoglia il codice sorgente

bin/tau-cli: add get-state and set-state subcommands

Dastan-glitch 4 anni fa
parent
commit
2b2f96591a
1 ha cambiato i file con 51 aggiunte e 0 eliminazioni
  1. 51 0
      bin/tau-cli/src/main.rs

+ 51 - 0
bin/tau-cli/src/main.rs

@@ -57,6 +57,27 @@ pub enum CliTauSubCommands {
         #[clap(short, long)]
         data: Option<String>,
     },
+    /// Set task state
+    SetState {
+        #[clap(short, long)]
+        id: u64,
+        #[clap(short, long)]
+        state: String,
+    },
+    /// Get task state
+    GetState {
+        #[clap(short, long)]
+        id: u64,
+    },
+    /// Set comment for a task
+    SetComment {
+        #[clap(short, long)]
+        id: Option<u64>,
+        #[clap(short, long)]
+        author: Option<String>,
+        #[clap(short, long)]
+        content: Option<String>,
+    },
 }
 
 /// Tau cli
@@ -138,6 +159,22 @@ async fn update(url: &str, id: Option<u64>, data: Value) -> Result<Value> {
     Ok(request(req, url.to_string()).await?)
 }
 
+// Set state for a task and returns `true` upon success.
+// --> {"jsonrpc": "2.0", "method": "set_state", "params": [task_id, state], "id": 1}
+// <-- {"jsonrpc": "2.0", "result": true, "id": 1}
+async fn set_state(url: &str, id: u64, state: String) -> Result<Value> {
+    let req = jsonrpc::request(json!("set_state"), json!([id, state]));
+    Ok(request(req, url.to_string()).await?)
+}
+
+// Get task's state.
+// --> {"jsonrpc": "2.0", "method": "get_state", "params": [task_id], "id": 1}
+// <-- {"jsonrpc": "2.0", "result": "state", "id": 1}
+async fn get_state(url: &str, id: u64) -> Result<Value> {
+    let req = jsonrpc::request(json!("get_state"), json!([id]));
+    Ok(request(req, url.to_string()).await?)
+}
+
 async fn start(options: CliTau) -> Result<()> {
     let rpc_addr = "tcp://127.0.0.1:8875";
     match options.command {
@@ -221,6 +258,7 @@ async fn start(options: CliTau) -> Result<()> {
 
             return Ok(())
         }
+
         Some(CliTauSubCommands::List { month }) => {
             let ts = if month.is_some() {
                 let month = month.unwrap();
@@ -308,6 +346,19 @@ async fn start(options: CliTau) -> Result<()> {
 
             return Ok(())
         }
+
+        Some(CliTauSubCommands::SetState { id, state }) => {
+            set_state(rpc_addr, id, state).await?;
+
+            return Ok(())
+        }
+
+        Some(CliTauSubCommands::GetState { id }) => {
+            let state = get_state(rpc_addr, id).await?;
+            println!("Task with id: {} is {}", id, state);
+
+            return Ok(())
+        }
         _ => (),
     }
     error!("Please run 'tau help' to see usage.");