Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 58400b0

Browse files
authored
Merge pull request #1 from paritytech/td-cli
Polkadot CLI and logging initialization.
2 parents 0d26ee7 + 5b299d4 commit 58400b0

File tree

8 files changed

+345
-3
lines changed

8 files changed

+345
-3
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
[*]
3+
indent_style=tab
4+
indent_size=tab
5+
tab_width=4
6+
end_of_line=lf
7+
charset=utf-8
8+
trim_trailing_whitespace=true
9+
max_line_length=120
10+
insert_final_newline=true
11+
12+
[*.yml]
13+
indent_style=space
14+
indent_size=2
15+
tab_width=8
16+
end_of_line=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target/
22
**/*.rs.bk
3+
*.swp

Cargo.lock

Lines changed: 234 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
[package]
22
name = "polkadot"
33
version = "0.1.0"
4-
authors = ["Robert Habermeier <[email protected]>"]
4+
authors = ["Parity Team <[email protected]>"]
55

66
[dependencies]
7+
polkadot-cli = { path = "cli" }
8+
9+
[workspace]

cli/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "polkadot-cli"
3+
version = "0.1.0"
4+
authors = ["Parity Team <[email protected]>"]
5+
description = "Polkadot node implementation in Rust."
6+
7+
[dependencies]
8+
clap = { version = "2.27", features = ["yaml"] }
9+
env_logger = "0.4"
10+
log = "0.3"

cli/src/cli.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: polkadot
2+
version: "1.0.0"
3+
author: "Parity Team <[email protected]>"
4+
about: Polkadot Node Rust Implementation
5+
args:
6+
- log:
7+
short: l
8+
value_name: LOG_PATTERN
9+
help: Sets a custom logging
10+
takes_value: true
11+
subcommands:
12+
- collator:
13+
about: Run collator node
14+
- validator:
15+
about: Run validator node

cli/src/lib.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2017 Parity Technologies (UK) Ltd.
2+
// This file is part of Polkadot.
3+
4+
// Polkadot is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Polkadot is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16+
17+
extern crate env_logger;
18+
19+
#[macro_use]
20+
extern crate clap;
21+
#[macro_use]
22+
extern crate log;
23+
24+
pub fn main() {
25+
let yaml = load_yaml!("./cli.yml");
26+
let matches = clap::App::from_yaml(yaml).get_matches();
27+
28+
let log_pattern = matches.value_of("log").unwrap_or("");
29+
init_logger(log_pattern);
30+
31+
if let Some(_) = matches.subcommand_matches("collator") {
32+
info!("Starting collator.");
33+
return;
34+
}
35+
36+
if let Some(_) = matches.subcommand_matches("validator") {
37+
info!("Starting validator.");
38+
return;
39+
}
40+
41+
println!("No command given.\n");
42+
let _ = clap::App::from_yaml(yaml).print_long_help();
43+
}
44+
45+
46+
fn init_logger(pattern: &str) {
47+
let mut builder = env_logger::LogBuilder::new();
48+
// Disable info logging by default for some modules:
49+
builder.filter(Some("hyper"), log::LogLevelFilter::Warn);
50+
// Enable info for others.
51+
builder.filter(None, log::LogLevelFilter::Info);
52+
53+
if let Ok(lvl) = std::env::var("RUST_LOG") {
54+
builder.parse(&lvl);
55+
}
56+
57+
builder.parse(pattern);
58+
59+
60+
builder.init().expect("Logger initialized only once.");
61+
}

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

17+
extern crate polkadot_cli;
18+
1719
fn main() {
18-
println!("Hello, world!");
19-
}
20+
polkadot_cli::main();
21+
}

0 commit comments

Comments
 (0)