Allow supplying sources from the command line (#2)

Allow supplying sources from the command line

Fixes #1
This commit is contained in:
Harsh Shandilya 2019-10-12 16:56:27 +05:30 committed by GitHub
commit 4af559da3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 40 deletions

20
Cargo.lock generated
View File

@ -128,9 +128,11 @@ dependencies = [
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1103,11 +1105,22 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "term_size"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1408,6 +1421,11 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "yaml-rust"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
@ -1534,6 +1552,7 @@ dependencies = [
"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
"checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
"checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
@ -1570,3 +1589,4 @@ dependencies = [
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9"
"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
"checksum yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e66366e18dc58b46801afbf2ca7661a9f59cc8c5962c29892b6039b4f86fa992"

View File

@ -7,6 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.33.0"
clap = { version = "^2.33.0", features = ["yaml", "wrap_help"] }
reqwest = "0.9.20"
regex = "1.3.1"
[build-dependencies]
clap = { version = "^2.33.0", features = ["yaml"] }

37
src/adnix.yml Normal file
View File

@ -0,0 +1,37 @@
name: adnix-rs
version: "0.1.0"
author: Harsh Shandilya <msfjarvis@gmail.com>
about: "CLI tool to convert ad blocking hosts files into DNSMasq or Unbound configuration files"
args:
- output:
help: Output file
short: o
long: output
takes_value: true
value_name: "OUTPUT"
- formatter:
help: Formatter
short: f
long: formatter
default_value: "dnsmasq"
takes_value: true
possible_values: ["dnsmasq", "dnsmasq-server", "unbound"]
value_name: "STRING"
- ipv4_addr:
help: IPv4 address
long: address
default_value: "127.0.0.1"
takes_value: true
value_name: "ADDRESS"
- ipv6_addr:
help: IPv6 address
long: v6address
default_value: "::1"
takes_value: true
value_name: "ADDRESS"
- sources_file:
help: Source File
short: s
long: sources_file
takes_value: true
value_name: "STRING"

View File

@ -4,49 +4,28 @@ extern crate reqwest;
mod source;
use clap::{App, Arg};
use clap::{load_yaml, App};
use source::Source;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader, BufWriter, Write},
process,
};
fn main() {
let yaml = load_yaml!("adnix.yml");
let matches = App::from_yaml(yaml).get_matches();
let sources_file = if matches.is_present("sources_file") {
matches.value_of("sources_file").unwrap()
} else {
""
};
let sources: HashMap<String, Source> = parse_file(&sources_file);
let mut contents = String::new();
let mut sources: HashMap<&str, Source> = HashMap::new();
sources.insert(
"Harsh Shandilya's hosts list",
Source {
url: String::from("https://download.msfjarvis.website/adblock/hosts"),
},
);
let matches = App::new("adnix-rs").version("0.1.0")
.author("Harsh Shandilya <msfjarvis@gmail.com>")
.about("CLI tool to convert ad blocking hosts files into DNSMasq or Unbound configuration files")
.args(
&[
Arg::with_name("output")
.short("o")
.long("file")
.value_name("OUTPUT")
.help("Output file")
.takes_value(true),
Arg::with_name("formatter")
.short("f")
.long("formatter")
.default_value("dnsmasq")
.takes_value(true)
.possible_values(&["dnsmasq", "dnsmasq-server", "unbound"]),
Arg::with_name("ipv4_addr")
.long("address")
.default_value("127.0.0.1")
.takes_value(true),
Arg::with_name("ipv6_addr")
.long("v6address")
.default_value("::1")
.takes_value(true)
]
)
.get_matches();
let ipv4_addr = matches.value_of("ipv4_addr").unwrap_or_default();
let ipv6_addr = matches.value_of("ipv6_addr").unwrap_or_default();
if let Some(val) = matches.value_of("formatter") {
@ -74,3 +53,37 @@ fn main() {
println!("{}", contents);
}
}
fn parse_file(filepath: &str) -> HashMap<String, Source> {
println!("Sources path {}", filepath);
let mut list: HashMap<String, Source> = HashMap::new();
if filepath != "" {
let file = File::open(filepath).unwrap_or_else(|err| {
eprintln!("Problem openning file: {}", err);
process::exit(1);
});
let lines = BufReader::new(file).lines();
for line in lines {
let content = line.unwrap_or_else(|err| {
eprintln!("Problem parsing lines: {}", err);
process::exit(1);
});
let vec: Vec<&str> = content.split('|').collect();
list.insert(
vec[0].to_owned(),
Source {
url: vec[1].to_owned(),
},
);
}
} else {
list.insert(
String::from("Harsh Shandilya's hosts list"),
Source {
url: String::from("https://download.msfjarvis.website/adblock/hosts"),
},
);
}
list
}