Skip to content

Commit 3d8085b

Browse files
committed
Add minimal config and arg handling to agent
1 parent 6e87279 commit 3d8085b

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

agent/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "intecture_agent"
33
version = "0.4.0"
44
authors = [ "Pete Hayes <pete@intecture.io>" ]
55
license = "MPL-2.0"
6-
description = "Tiny daemon that wraps the core API"
6+
description = "Tiny daemon that exposes Intecture's API as a service"
77
homepage = "https://intecture.io"
88
repository = "https://github.com/intecture/api"
99
documentation = "https://intecture.io/rust/inapi/"
@@ -16,10 +16,14 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
1616
travis-ci = { repository = "intecture/api" }
1717

1818
[dependencies]
19+
clap = "2.26"
1920
env_logger = "0.4"
2021
error-chain = "0.11"
2122
futures = "0.1"
2223
intecture_api = { version = "0.4.0", path = "../core" }
24+
serde = "1.0"
25+
serde_derive = "1.0"
2326
serde_json = "1.0"
2427
tokio-proto = "0.1"
2528
tokio-service = "0.1"
29+
toml = "0.4"

agent/src/main.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
55
// modified, or distributed except according to those terms.
66

7-
#![recursion_limit = "1024"]
8-
7+
extern crate clap;
98
extern crate env_logger;
109
#[macro_use] extern crate error_chain;
1110
extern crate futures;
1211
extern crate intecture_api;
12+
#[macro_use] extern crate serde_derive;
1313
extern crate serde_json;
1414
extern crate tokio_proto;
1515
extern crate tokio_service;
16+
extern crate toml;
1617

1718
mod errors;
1819

1920
use errors::*;
2021
use futures::{future, Future};
2122
use intecture_api::{Executable, Runnable};
2223
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;
2427
use tokio_proto::TcpServer;
2528
use tokio_service::Service;
2629

@@ -54,11 +57,46 @@ impl Service for Api {
5457
}
5558
}
5659

60+
#[derive(Deserialize)]
61+
struct Config {
62+
addr: SocketAddr,
63+
}
64+
5765
quick_main!(|| -> Result<()> {
5866
env_logger::init().chain_err(|| "Could not start logging")?;
5967

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);
62100
server.serve(|| Ok(Api));
63101
Ok(())
64102
});

0 commit comments

Comments
 (0)