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

Use config to get basic values

parent c8f09a39
......@@ -12,7 +12,13 @@ static CONFIG: &'static str = "/etc/vmadm.toml";
#[derive(Deserialize)]
pub struct Config {
pool: String,
pub pool: String,
#[serde(default = "default_conf_dir")]
pub conf_dir: String
}
fn default_conf_dir() -> String {
"/etc/jails".to_string()
}
impl Config {
......
......@@ -15,9 +15,19 @@ pub struct Config {
#[serde(default = "new_uuid")]
pub uuid: String,
alias: String,
hostname: String,
ram: u64,
cpu: u64,
disk: u64,
#[serde(default = "new_uuid")]
autostart: bool
}
fn new_uuid() -> String {
Uuid::new_v4().hyphenated().to_string()
}
fn bfalse() -> bool {
false
}
#[derive(Debug, Serialize, Deserialize)]
......@@ -35,9 +45,6 @@ impl PartialEq for IdxEntry {
}
}
fn new_uuid() -> String {
Uuid::new_v4().hyphenated().to_string()
}
#[derive(Debug, Serialize, Deserialize)]
struct Index {
......@@ -66,14 +73,14 @@ impl<'a> JDB<'a> {
/// let db = JDB::open("/etc/jails/index");
/// ```
pub fn open(path: &'a Path) -> Result<Self, Box<Error>> {
match File::open(path) {
pub fn open(path: &'a String) -> Result<Self, Box<Error>> {
let idx_file = Path::new(path);
match File::open(idx_file.join("index")) {
Ok(file) => {
let index: Index = serde_json::from_reader(file)?;
Ok(JDB {
index: index,
dir: path.parent().unwrap(),
dir: Path::new(path),
})
}
Err(_) => {
......@@ -84,7 +91,7 @@ impl<'a> JDB<'a> {
};
let db = JDB {
index: index,
dir: path.parent().unwrap(),
dir: Path::new(path),
};
db.save()?;
Ok(db)
......
......@@ -62,38 +62,38 @@ fn main() {
}
}
fn startup(conf: Config) -> Result<i32, Box<Error>> {
fn startup(_conf: Config) -> Result<i32, Box<Error>> {
println!("{:?}", zfs::list("tpool"));
Ok(0)
}
fn start(conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
fn start(_conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
Ok(0)
}
fn stop(conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
fn stop(_conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
Ok(0)
}
fn update(conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
fn update(_conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
Ok(0)
}
fn list(conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let db = JDB::open(Path::new(INDEX))?;
let db = JDB::open(&conf.conf_dir)?;
db.print();
Ok(0)
}
fn create(conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let mut db = JDB::open(Path::new(INDEX))?;
let mut db = JDB::open(&conf.conf_dir)?;
let conf: jdb::Config = serde_json::from_reader(io::stdin())?;
db.insert(conf)?;
Ok(0)
}
fn destroy(conf: Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let mut db = JDB::open(Path::new(INDEX))?;
let mut db = JDB::open(&conf.conf_dir)?;
let uuid = value_t!(matches, "uuid", String).unwrap();
db.remove(uuid)?;
Ok(0)
......
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