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

Commit e1eed73

Browse files
authored
feat: initializing CLI for node initing and running (#55)
1 parent ff3e987 commit e1eed73

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
9+
clap = { version = "4.4.11", features = ["cargo"] }
10+
config = { version = "*", path = "./config" }
1011

1112
[workspace]
1213

@@ -17,4 +18,4 @@ members = [
1718
"node",
1819
"rpc",
1920
"config"
20-
]
21+
]

config/src/lib.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use serde::Deserialize;
2-
use std::{io, path::PathBuf};
2+
use std::{
3+
io::{self},
4+
path::PathBuf,
5+
};
36
use toml::from_str;
47

58
#[derive(Debug, Deserialize)]
@@ -75,39 +78,44 @@ impl Config {
7578
let contents = std::fs::read_to_string(path)?;
7679
Ok(from_str(&contents)?)
7780
}
78-
}
79-
80-
#[cfg(test)]
81-
mod tests {
82-
use std::io::Write;
83-
84-
use super::*;
8581

86-
fn temp_config_file() {
87-
let mut p = std::env::temp_dir();
88-
p.push("./config.toml");
89-
let mut file = std::fs::File::create(p).expect("Failed to create temp config file");
90-
file.write_all(
91-
b"
92-
[network]
82+
pub fn default_file() -> &'static [u8] {
83+
b"[network]
9384
port = 37771
9485
max_connections = 10
9586
moniker = ''
87+
9688
[nostr]
9789
port = 443
9890
bootstraps = []
9991
max_ws_connections = 100
92+
10093
[rpc]
10194
enable_grpc = true
10295
grpc_port = 9090
96+
10397
[metrics]
10498
enable_metrics = false
99+
105100
[log]
106101
write_to_file = true
107102
path = 'log.r7'
108-
",
109-
)
110-
.expect("Failed to write to temp config file")
103+
"
104+
}
105+
}
106+
107+
#[cfg(test)]
108+
mod tests {
109+
use std::io::Write;
110+
111+
use super::*;
112+
113+
fn temp_config_file() {
114+
let mut p = std::env::temp_dir();
115+
p.push("./config.toml");
116+
let mut file = std::fs::File::create(p).expect("Failed to create temp config file");
117+
file.write_all(Config::default_file())
118+
.expect("Failed to write to temp config file")
111119
}
112120

113121
#[test]

rpc/src/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use config::RpcConfig;
12
use std::io;
23
use tonic::transport::Server;
34

@@ -8,8 +9,8 @@ use crate::nostr;
89
use crate::nostr::nostr::nostr_rpc_server::NostrRpcServer;
910

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

1415
let network_service = network::NetworkService::default();
1516
let nostr_rpc_service = nostr::NostrRpcService::default();

src/main.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
1+
use clap::*;
2+
use config::Config;
3+
use std::fs;
4+
15
fn main() {
2-
println!("Hello, world!");
6+
let init_command = command!()
7+
.name("init")
8+
.about("Initializing a Redvin node.")
9+
.arg(
10+
Arg::new("working-directory")
11+
.long("working-directory")
12+
.short('w')
13+
.help("Working directory is the path for saving relay, peers data and some other stuff for managing node.")
14+
.aliases(["workdir", "wdir", "workingdirectory", "workingdir", "wdirectory"])
15+
.required(true)
16+
);
17+
18+
let start_command = command!()
19+
.name("start")
20+
.about("Starting a Redvin instance by passing a working directory.");
21+
22+
let root_command = command!()
23+
.about("Redvin is an IPNN implementation in rust, helping to build decentralized future.")
24+
.subcommand(init_command)
25+
.subcommand(start_command)
26+
.get_matches();
27+
28+
match root_command.subcommand() {
29+
Some(subcommand) => {
30+
if subcommand.0 == "init" {
31+
let dir_builder = fs::DirBuilder::new();
32+
33+
let working_directory = subcommand
34+
.1
35+
.get_one::<String>("working-directory")
36+
.expect("invalid working directory path.");
37+
38+
dir_builder
39+
.create(working_directory)
40+
.expect("can not create working directory.");
41+
42+
let config_path = format!("{}/config.toml", working_directory);
43+
fs::write(config_path, Config::default_file())
44+
.expect("can not create config file on working directory.");
45+
} else if subcommand.0 == "start" {
46+
println!("not implemented yet!")
47+
}
48+
}
49+
None => {
50+
println!("please use `redvin --help`")
51+
}
52+
}
353
}

0 commit comments

Comments
 (0)