Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
feat: initializing CLI for node initing and running (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Dec 26, 2023
1 parent ff3e987 commit e1eed73
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

clap = { version = "4.4.11", features = ["cargo"] }
config = { version = "*", path = "./config" }

[workspace]

Expand All @@ -17,4 +18,4 @@ members = [
"node",
"rpc",
"config"
]
]
44 changes: 26 additions & 18 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use serde::Deserialize;
use std::{io, path::PathBuf};
use std::{
io::{self},
path::PathBuf,
};
use toml::from_str;

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -75,39 +78,44 @@ impl Config {
let contents = std::fs::read_to_string(path)?;
Ok(from_str(&contents)?)
}
}

#[cfg(test)]
mod tests {
use std::io::Write;

use super::*;

fn temp_config_file() {
let mut p = std::env::temp_dir();
p.push("./config.toml");
let mut file = std::fs::File::create(p).expect("Failed to create temp config file");
file.write_all(
b"
[network]
pub fn default_file() -> &'static [u8] {
b"[network]
port = 37771
max_connections = 10
moniker = ''
[nostr]
port = 443
bootstraps = []
max_ws_connections = 100
[rpc]
enable_grpc = true
grpc_port = 9090
[metrics]
enable_metrics = false
[log]
write_to_file = true
path = 'log.r7'
",
)
.expect("Failed to write to temp config file")
"
}
}

#[cfg(test)]
mod tests {
use std::io::Write;

use super::*;

fn temp_config_file() {
let mut p = std::env::temp_dir();
p.push("./config.toml");
let mut file = std::fs::File::create(p).expect("Failed to create temp config file");
file.write_all(Config::default_file())
.expect("Failed to write to temp config file")
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions rpc/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use config::RpcConfig;
use std::io;
use tonic::transport::Server;

Expand All @@ -8,8 +9,8 @@ use crate::nostr;
use crate::nostr::nostr::nostr_rpc_server::NostrRpcServer;

#[tokio::main]
pub async fn start(port: u16) -> Result<(), io::Error> {
let addr = format!("[::1]:{}", port).parse().unwrap();
pub async fn start(cfg: RpcConfig) -> Result<(), io::Error> {
let addr = format!("[::1]:{}", cfg.grpc_port).parse().unwrap();

let network_service = network::NetworkService::default();
let nostr_rpc_service = nostr::NostrRpcService::default();
Expand Down
52 changes: 51 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
use clap::*;
use config::Config;
use std::fs;

fn main() {
println!("Hello, world!");
let init_command = command!()
.name("init")
.about("Initializing a Redvin node.")
.arg(
Arg::new("working-directory")
.long("working-directory")
.short('w')
.help("Working directory is the path for saving relay, peers data and some other stuff for managing node.")
.aliases(["workdir", "wdir", "workingdirectory", "workingdir", "wdirectory"])
.required(true)
);

let start_command = command!()
.name("start")
.about("Starting a Redvin instance by passing a working directory.");

let root_command = command!()
.about("Redvin is an IPNN implementation in rust, helping to build decentralized future.")
.subcommand(init_command)
.subcommand(start_command)
.get_matches();

match root_command.subcommand() {
Some(subcommand) => {
if subcommand.0 == "init" {
let dir_builder = fs::DirBuilder::new();

let working_directory = subcommand
.1
.get_one::<String>("working-directory")
.expect("invalid working directory path.");

dir_builder
.create(working_directory)
.expect("can not create working directory.");

let config_path = format!("{}/config.toml", working_directory);
fs::write(config_path, Config::default_file())
.expect("can not create config file on working directory.");
} else if subcommand.0 == "start" {
println!("not implemented yet!")
}
}
None => {
println!("please use `redvin --help`")
}
}
}

0 comments on commit e1eed73

Please sign in to comment.