2
0
mirror of https://github.com/xvxx/phd synced 2024-11-10 13:10:23 +00:00
phd/src/main.rs

104 lines
2.8 KiB
Rust
Raw Normal View History

2019-12-27 03:51:59 +00:00
use phd;
2019-12-27 03:57:59 +00:00
use std::process;
2019-12-23 02:05:48 +00:00
2019-12-28 22:06:43 +00:00
const DEFAULT_HOST: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 7070;
2019-12-23 02:05:48 +00:00
fn main() {
2020-05-09 19:40:25 +00:00
let args = std::env::args().skip(1).collect::<Vec<_>>();
let mut args = args.iter();
2019-12-27 23:42:57 +00:00
let mut root = ".";
2019-12-28 22:06:43 +00:00
let mut host = DEFAULT_HOST;
let mut port = DEFAULT_PORT;
2020-05-09 19:40:25 +00:00
let mut render = "";
while let Some(arg) = args.next() {
2019-12-27 04:59:34 +00:00
match arg.as_ref() {
"--version" | "-v" | "-version" => return print_version(),
2019-12-28 00:52:15 +00:00
"--help" | "-help" => return print_help(),
2020-05-09 19:40:25 +00:00
"--render" | "-render" | "-r" => {
if let Some(path) = args.next() {
render = path;
} else {
render = "/";
}
}
2019-12-27 04:59:34 +00:00
"--port" | "-p" | "-port" => {
2020-05-09 19:40:25 +00:00
if let Some(p) = args.next() {
2019-12-28 04:09:41 +00:00
port = p
.parse()
.map_err(|_| {
eprintln!("bad port: {}", p);
process::exit(1)
})
.unwrap();
2019-12-27 04:59:34 +00:00
}
}
2019-12-28 00:52:15 +00:00
"-h" => {
2020-05-09 19:40:25 +00:00
if let Some(h) = args.next() {
host = &h;
2019-12-28 00:52:15 +00:00
} else {
return print_help();
}
}
"--host" | "-host" => {
2020-05-09 19:40:25 +00:00
if let Some(h) = args.next() {
host = &h;
2019-12-27 04:59:34 +00:00
}
}
_ => {
if let Some('-') = arg.chars().nth(0) {
eprintln!("unknown flag: {}", arg);
process::exit(1);
} else {
2020-05-09 19:40:25 +00:00
root = &arg;
2019-12-27 04:59:34 +00:00
}
}
}
2019-12-27 03:57:59 +00:00
}
2020-05-09 19:40:25 +00:00
if !render.is_empty() {
return match phd::server::render(host, port, root, &render) {
2020-05-09 20:04:51 +00:00
Ok(out) => print!("{}", out),
2020-05-09 19:40:25 +00:00
Err(e) => eprintln!("{}", e),
};
}
2019-12-27 23:42:57 +00:00
if let Err(e) = phd::server::start(host, port, root) {
2019-12-23 02:05:48 +00:00
eprintln!("{}", e);
}
}
2019-12-27 03:57:59 +00:00
2019-12-27 04:59:34 +00:00
fn print_help() {
2019-12-27 03:57:59 +00:00
println!(
"Usage:
2019-12-28 00:52:15 +00:00
phd [options] <root directory>
2019-12-27 03:57:59 +00:00
Options:
2020-05-09 19:40:25 +00:00
-r, --render SELECTOR Render and print SELECTOR to stdout only.
-p, --port Port to bind to. [Default: {port}]
-h, --host Hostname for links. [Default: {host}]
2019-12-28 00:52:15 +00:00
Other flags:
2019-12-27 03:57:59 +00:00
-h, --help Print this screen.
2020-05-09 19:40:25 +00:00
-v, --version Print phd version.
Examples:
phd ./path/to/site # Serve directory over port 7070.
phd -p 70 docs # Serve 'docs' directory on port 70
phd -h gopher.com # Serve current directory over port 7070
# using hostname 'gopher.com'
phd -r / ./site # Render local gopher site to stdout.
",
2019-12-28 22:06:43 +00:00
host = DEFAULT_HOST,
port = DEFAULT_PORT,
2019-12-27 03:57:59 +00:00
);
}
fn print_version() {
println!("phd v{}", env!("CARGO_PKG_VERSION"));
}