Prechádzať zdrojové kódy

Add tau-cli in bin/

Dastan-glitch 4 rokov pred
rodič
commit
7c3810021f
2 zmenil súbory, kde vykonal 150 pridanie a 0 odobranie
  1. 30 0
      bin/tau-cli/Cargo.toml
  2. 120 0
      bin/tau-cli/src/main.rs

+ 30 - 0
bin/tau-cli/Cargo.toml

@@ -0,0 +1,30 @@
+[package]
+name = "taucli"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies.darkfi]
+path = "../../"
+features = ["rpc"]
+
+[dependencies]
+# Async
+smol = "1.2.5"
+futures = "0.3.19"
+async-std = {version = "1.10.0", features = ["attributes"]}
+async-trait = "0.1.52"
+async-channel = "1.6.1"
+async-executor = "1.4.1"
+easy-parallel = "3.2.0"
+
+# Misc
+clap = {version = "3.0.7", features = ["derive"]}
+log = "0.4.14"
+num_cpus = "1.13.1"
+simplelog = "0.11.2"
+url = "2.2.2"
+
+# Encoding and parsing
+serde_json = "1.0.74"

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

@@ -0,0 +1,120 @@
+use clap::{IntoApp, Parser};
+use log::{debug, error};
+
+use darkfi::{
+    rpc::jsonrpc::{self, JsonResult},
+    util::cli::log_config,
+    Error, Result,
+};
+use serde_json::{json, Value};
+use simplelog::{ColorChoice, TermLogger, TerminalMode};
+use url::Url;
+
+/// Tau cli
+#[derive(Parser)]
+#[clap(name = "tau")]
+pub struct CliTau {
+    /// Add a new task
+    #[clap(long)]
+    pub add: Option<String>,
+    /// list open tasks
+    #[clap(long)]
+    pub list: Option<String>,
+    /// Show task by ID
+    #[clap(long)]
+    pub show: Option<u32>,
+    /// Start task by ID
+    #[clap(long)]
+    pub start: Option<u32>,
+    /// Pause task by ID
+    #[clap(long)]
+    pub pause: Option<u32>,
+    /// Stop task by ID
+    #[clap(long)]
+    pub stop: Option<u32>,
+    /// Comment on task by ID
+    #[clap(long)]
+    pub comment: Option<u32>,
+    /// Log drawdown
+    #[clap(long)]
+    pub log: Option<String>,
+    /// Increase verbosity
+    #[clap(short, parse(from_occurrences))]
+    pub verbose: u8,
+}
+
+pub struct Client {
+    url: String,
+}
+
+impl Client {
+    pub fn new(url: String) -> Self {
+        Self { url }
+    }
+
+    async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
+        let reply: JsonResult =
+            match jsonrpc::send_request(&Url::parse(&self.url)?, json!(r), None).await {
+                Ok(v) => v,
+                Err(e) => return Err(e),
+            };
+
+        match reply {
+            JsonResult::Resp(r) => {
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
+                Ok(r.result)
+            }
+
+            JsonResult::Err(e) => {
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
+                Err(Error::JsonRpcError(e.error.message.to_string()))
+            }
+
+            JsonResult::Notif(n) => {
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
+                Err(Error::JsonRpcError("Unexpected reply".to_string()))
+            }
+        }
+    }
+
+    // --> {"jsonrpc": "2.0", "method": "cmd_add", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "params", "id": 42}
+    async fn cmd_add(&self) -> Result<Value> {
+        let req = jsonrpc::request(json!("cmd_add"), json!([]));
+        Ok(self.request(req).await?)
+    }
+}
+
+async fn start(options: CliTau) -> Result<()> {
+    let rpc_addr = "tcp://127.0.0.1:7777";
+    let client = Client::new(rpc_addr.to_string());
+    if options.add.is_some() {
+        let reply = client.cmd_add().await?;
+        println!("Server replied: {}", &reply.to_string());
+        return Ok(())
+    }
+    error!("Please run 'tau help' to see usage.");
+
+    Err(Error::MissingParams)
+}
+
+#[async_std::main]
+async fn main() -> Result<()> {
+    let args = CliTau::parse();
+    let matches = CliTau::into_app().get_matches();
+    let verbosity_level = matches.occurrences_of("verbose");
+
+    //let config_path = if args.config.is_some() {
+    //    expand_path(&args.config.clone().unwrap())?
+    //} else {
+    //    join_config_path(&PathBuf::from("tau.toml"))?
+    //};
+
+    // Spawn config file if it's not in place already.
+    //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
+
+    let (lvl, conf) = log_config(verbosity_level)?;
+    TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
+
+    start(args).await
+}