|
4 | 4 | // https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
|
5 | 5 | // modified, or distributed except according to those terms.
|
6 | 6 |
|
7 |
| -#![recursion_limit = "1024"] |
8 |
| - |
| 7 | +extern crate clap; |
9 | 8 | extern crate env_logger;
|
10 | 9 | #[macro_use] extern crate error_chain;
|
11 | 10 | extern crate futures;
|
12 | 11 | extern crate intecture_api;
|
| 12 | +#[macro_use] extern crate serde_derive; |
13 | 13 | extern crate serde_json;
|
14 | 14 | extern crate tokio_proto;
|
15 | 15 | extern crate tokio_service;
|
| 16 | +extern crate toml; |
16 | 17 |
|
17 | 18 | mod errors;
|
18 | 19 |
|
19 | 20 | use errors::*;
|
20 | 21 | use futures::{future, Future};
|
21 | 22 | use intecture_api::{Executable, Runnable};
|
22 | 23 | use intecture_api::host::remote::JsonProto;
|
23 |
| -use std::io; |
| 24 | +use std::fs::File; |
| 25 | +use std::io::{self, Read}; |
| 26 | +use std::net::SocketAddr; |
24 | 27 | use tokio_proto::TcpServer;
|
25 | 28 | use tokio_service::Service;
|
26 | 29 |
|
@@ -54,11 +57,46 @@ impl Service for Api {
|
54 | 57 | }
|
55 | 58 | }
|
56 | 59 |
|
| 60 | +#[derive(Deserialize)] |
| 61 | +struct Config { |
| 62 | + addr: SocketAddr, |
| 63 | +} |
| 64 | + |
57 | 65 | quick_main!(|| -> Result<()> {
|
58 | 66 | env_logger::init().chain_err(|| "Could not start logging")?;
|
59 | 67 |
|
60 |
| - let addr = "127.0.0.1:7101".parse().chain_err(|| "Invalid server address")?; |
61 |
| - let server = TcpServer::new(JsonProto, addr); |
| 68 | + let matches = clap::App::new("Intecture Agent") |
| 69 | + .version(env!("CARGO_PKG_VERSION")) |
| 70 | + .author(env!("CARGO_PKG_AUTHORS")) |
| 71 | + .about(env!("CARGO_PKG_DESCRIPTION")) |
| 72 | + .arg(clap::Arg::with_name("config") |
| 73 | + .short("c") |
| 74 | + .long("config") |
| 75 | + .value_name("FILE") |
| 76 | + .help("Path to the agent configuration file") |
| 77 | + .takes_value(true)) |
| 78 | + .arg(clap::Arg::with_name("addr") |
| 79 | + .short("a") |
| 80 | + .long("address") |
| 81 | + .value_name("ADDR") |
| 82 | + .help("Set the socket address this server will listen on (e.g. 0.0.0.0:7101)") |
| 83 | + .takes_value(true)) |
| 84 | + .group(clap::ArgGroup::with_name("config_or_else") |
| 85 | + .args(&["config", "addr"]) |
| 86 | + .required(true)) |
| 87 | + .get_matches(); |
| 88 | + |
| 89 | + let config = if let Some(c) = matches.value_of("config") { |
| 90 | + let mut fh = File::open(c).chain_err(|| "Could not open config file")?; |
| 91 | + let mut buf = Vec::new(); |
| 92 | + fh.read_to_end(&mut buf).chain_err(|| "Could not read config file")?; |
| 93 | + toml::from_slice(&buf).chain_err(|| "Config file contained invalid TOML")? |
| 94 | + } else { |
| 95 | + let addr = matches.value_of("addr").unwrap().parse().chain_err(|| "Invalid server address")?; |
| 96 | + Config { addr } |
| 97 | + }; |
| 98 | + |
| 99 | + let server = TcpServer::new(JsonProto, config.addr); |
62 | 100 | server.serve(|| Ok(Api));
|
63 | 101 | Ok(())
|
64 | 102 | });
|
0 commit comments