Commit d31c26f6 authored by Heinz N. Gies's avatar Heinz N. Gies

Add via create and check for conflicts

parent 191953fb
......@@ -6,6 +6,7 @@ dependencies = [
"serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -78,6 +79,19 @@ name = "quote"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.24 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.8"
......@@ -161,6 +175,15 @@ name = "unicode-xid"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "uuid"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "vec_map"
version = "0.8.0"
......@@ -192,6 +215,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum libc 0.2.24 (registry+https://github.com/rust-lang/crates.io-index)" = "38f5c2b18a287cf78b4097db62e20f43cace381dc76ae5c0a3073067f78b7ddc"
"checksum num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "1708c0628602a98b52fad936cf3edb9a107af06e52e49fdf0707e884456a6af6"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum serde 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)" = "34b623917345a631dc9608d5194cc206b3fe6c3554cd1c75b937e55e285254af"
"checksum serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c2f530d36fb84ec48fb7146936881f026cdbf4892028835fd9398475f82c1bb4"
"checksum serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "10552fad5500771f3902d0c5ba187c5881942b811b7ba0d8fbbfbf84d80806d3"
"checksum serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37aee4e0da52d801acfbc0cc219eb1eda7142112339726e427926a6f6ee65d3a"
......@@ -203,6 +228,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum uuid 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7cfec50b0842181ba6e713151b72f4ec84a6a7e2c9c8a8a3ffc37bb1cd16b231"
"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
......
......@@ -7,6 +7,7 @@ authors = ["Heinz N. Gies <heinz@project-fifo.net>"]
serde = "1.0.8"
serde_json = "1.0.2"
serde_derive = "1.0.8"
uuid = { version = "0.4", features = ["serde", "v4"] }
[dependencies.clap]
version = "2.24.2"
......
......@@ -5,13 +5,36 @@ extern crate clap;
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate uuid;
use std::path::Path;
use std::io::{self, Read};
use serde_json::Error;
use std::error::Error;
use std::fmt;
mod jdb;
use jdb::JDB;
use jdb::{JDB, Config};
static INDEX: &'static str = "/etc/jails/index";
#[derive(Debug)]
struct ConflictError {
uuid: String,
}
impl fmt::Display for ConflictError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Duplicated UUID:{}", self.uuid)
}
}
impl Error for ConflictError {
fn description(&self) -> &str {
"Conflict"
}
}
fn main() {
use clap::App;
......@@ -26,12 +49,15 @@ fn main() {
} else {
match matches.subcommand() {
("list", Some(list_matches)) => {
match JDB::open(Path::new("/etc/jails/index")) {
match JDB::open(Path::new(INDEX)) {
Err(e) => println!("cound not open index: {}", e),
Ok(db) => db.print(),
}
}
("create", Some(create_matches)) => println!("create jail"),
("create", Some(create_matches)) => {
let conf = create();
println!("create jail: {:?}", conf);
}
("update", Some(update_matches)) => println!("update jail"),
("destroy", Some(destroy_matches)) => println!("destroy jail"),
("start", Some(destroy_matches)) => println!("start jail"),
......@@ -40,4 +66,13 @@ fn main() {
_ => unreachable!(),
};
};
}
fn create() -> Result<jdb::Config, Box<Error>> {
let mut db = JDB::open(Path::new(INDEX))?;
let conf: jdb::Config = serde_json::from_reader(io::stdin())?;
match db.find(conf.uuid.clone()) {
None => db.insert(conf),
Some(_) => Err(Box::new(ConflictError { uuid: conf.uuid })),
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment