Commit 41174b52 authored by Heinz N. Gies's avatar Heinz N. Gies

Fix all linting errors and suggestions

parent c1809de1
This diff is collapsed.
...@@ -7,8 +7,9 @@ authors = ["Heinz N. Gies <heinz@project-fifo.net>"] ...@@ -7,8 +7,9 @@ authors = ["Heinz N. Gies <heinz@project-fifo.net>"]
serde = "1.0.8" serde = "1.0.8"
serde_json = "1.0.2" serde_json = "1.0.2"
serde_derive = "1.0.8" serde_derive = "1.0.8"
uuid = { version = "0.4", features = ["serde", "v4"] } uuid = { version = "0.5", features = ["serde", "v4"] }
toml = "0.4" toml = "0.4"
clippy = {version = "*", optional = true}
[dependencies.clap] [dependencies.clap]
version = "2.24.2" version = "2.24.2"
......
use serde_derive;
use std::io::Read; use std::io::Read;
use std::io::BufReader;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
......
...@@ -33,10 +33,10 @@ pub struct ConflictError { ...@@ -33,10 +33,10 @@ pub struct ConflictError {
} }
impl ConflictError { impl ConflictError {
pub fn new(uuid: String) -> ConflictError { pub fn new(uuid: &str) -> ConflictError {
ConflictError { uuid: uuid } ConflictError { uuid: String::from(uuid) }
} }
pub fn bx(uuid: String) -> Box<Error> { pub fn bx(uuid: &str) -> Box<Error> {
Box::new(ConflictError::new(uuid)) Box::new(ConflictError::new(uuid))
} }
} }
...@@ -58,10 +58,10 @@ pub struct NotFoundError { ...@@ -58,10 +58,10 @@ pub struct NotFoundError {
uuid: String, uuid: String,
} }
impl NotFoundError { impl NotFoundError {
pub fn new(uuid: String) -> NotFoundError { pub fn new(uuid: &str) -> NotFoundError {
NotFoundError { uuid: uuid } NotFoundError { uuid: String::from(uuid) }
} }
pub fn bx(uuid: String) -> Box<Error> { pub fn bx(uuid: &str) -> Box<Error> {
Box::new(NotFoundError::new(uuid)) Box::new(NotFoundError::new(uuid))
} }
......
...@@ -19,7 +19,7 @@ pub struct Config { ...@@ -19,7 +19,7 @@ pub struct Config {
ram: u64, ram: u64,
cpu: u64, cpu: u64,
disk: u64, disk: u64,
#[serde(default = "new_uuid")] #[serde(default = "bfalse")]
autostart: bool autostart: bool
} }
fn new_uuid() -> String { fn new_uuid() -> String {
...@@ -73,7 +73,7 @@ impl<'a> JDB<'a> { ...@@ -73,7 +73,7 @@ impl<'a> JDB<'a> {
/// let db = JDB::open("/etc/jails/index"); /// let db = JDB::open("/etc/jails/index");
/// ``` /// ```
pub fn open(path: &'a String) -> Result<Self, Box<Error>> { pub fn open(path: &'a str) -> Result<Self, Box<Error>> {
let idx_file = Path::new(path); let idx_file = Path::new(path);
match File::open(idx_file.join("index")) { match File::open(idx_file.join("index")) {
Ok(file) => { Ok(file) => {
...@@ -123,18 +123,18 @@ impl<'a> JDB<'a> { ...@@ -123,18 +123,18 @@ impl<'a> JDB<'a> {
serde_json::to_writer(file, &config)?; serde_json::to_writer(file, &config)?;
Ok(config) Ok(config)
} }
Some(_) => Err(ConflictError::bx(config.uuid)), Some(_) => Err(ConflictError::bx(config.uuid.as_str())),
} }
} }
/// Removes a jail with a given uuid from the index and removes it's /// Removes a jail with a given uuid from the index and removes it's
/// config file. /// config file.
pub fn remove(self: &'a mut JDB<'a>, uuid: String) -> Result<usize, Box<Error>> { pub fn remove(self: &'a mut JDB<'a>, uuid: & str) -> Result<usize, Box<Error>> {
match self.find(& uuid) { match self.find(uuid) {
None => Err(NotFoundError::bx(uuid.clone())), None => Err(NotFoundError::bx(uuid)),
Some(index) => { Some(index) => {
// remove the config file first // remove the config file first
let mut path = self.dir.join(uuid.clone()); let mut path = self.dir.join(uuid);
path.set_extension("json"); path.set_extension("json");
fs::remove_file(&path)?; fs::remove_file(&path)?;
self.index.entries.remove(index); self.index.entries.remove(index);
...@@ -175,7 +175,7 @@ impl<'a> JDB<'a> { ...@@ -175,7 +175,7 @@ impl<'a> JDB<'a> {
Ok(self.index.entries.len()) Ok(self.index.entries.len())
} }
/// Finds an entry for a given uuid /// Finds an entry for a given uuid
fn find(self: &'a JDB<'a>, uuid: & String) -> Option<usize> { fn find(self: &'a JDB<'a>, uuid: & str) -> Option<usize> {
self.index.entries.iter().position( self.index.entries.iter().position(
|x| *x.uuid == *uuid, |x| *x.uuid == *uuid,
) )
......
#![deny(trivial_numeric_casts,
// missing_docs,
unstable_features,
unused_import_braces,
)]
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
...@@ -8,10 +17,8 @@ extern crate serde_json; ...@@ -8,10 +17,8 @@ extern crate serde_json;
extern crate uuid; extern crate uuid;
extern crate toml; extern crate toml;
use std::path::Path;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use std::fs::File;
mod zfs; mod zfs;
...@@ -23,7 +30,6 @@ use config::Config; ...@@ -23,7 +30,6 @@ use config::Config;
pub mod errors; pub mod errors;
use errors::GenericError; use errors::GenericError;
static INDEX: &'static str = "/etc/jails/index";
fn main() { fn main() {
use clap::App; use clap::App;
...@@ -34,17 +40,17 @@ fn main() { ...@@ -34,17 +40,17 @@ fn main() {
let config = Config::new().unwrap(); let config = Config::new().unwrap();
let r = if matches.is_present("startup") { let r = if matches.is_present("startup") {
match matches.subcommand() { match matches.subcommand() {
("", None) => startup(config), ("", None) => startup(&config),
_ => Err(GenericError::bx("Can not use startup with a subcommand")), _ => Err(GenericError::bx("Can not use startup with a subcommand")),
} }
} else { } else {
match matches.subcommand() { match matches.subcommand() {
("list", Some(list_matches)) => list(config, list_matches), ("list", Some(list_matches)) => list(&config, list_matches),
("create", Some(create_matches)) => create(config, create_matches), ("create", Some(create_matches)) => create(&config, create_matches),
("update", Some(update_matches)) => update(config, update_matches), ("update", Some(update_matches)) => update(&config, update_matches),
("destroy", Some(destroy_matches)) => destroy(config, destroy_matches), ("destroy", Some(destroy_matches)) => destroy(&config, destroy_matches),
("start", Some(start_matches)) => start(config, start_matches), ("start", Some(start_matches)) => start(&config, start_matches),
("stop", Some(stop_matches)) => stop(config, stop_matches), ("stop", Some(stop_matches)) => stop(&config, stop_matches),
("", None) => { ("", None) => {
help_app.print_help().unwrap(); help_app.print_help().unwrap();
Ok(0) Ok(0)
...@@ -62,39 +68,39 @@ fn main() { ...@@ -62,39 +68,39 @@ fn main() {
} }
} }
fn startup(_conf: Config) -> Result<i32, Box<Error>> { fn startup(_conf: &Config) -> Result<i32, Box<Error>> {
println!("{:?}", zfs::list("tpool")); println!("{:?}", zfs::list("tpool"));
Ok(0) 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) 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) 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) Ok(0)
} }
fn list(conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> { fn list(conf: &Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let db = JDB::open(&conf.conf_dir)?; let db = JDB::open(&conf.conf_dir)?;
db.print(); db.print();
Ok(0) Ok(0)
} }
fn create(conf: Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> { fn create(conf: &Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let mut db = JDB::open(&conf.conf_dir)?; let mut db = JDB::open(&conf.conf_dir)?;
let conf: jdb::Config = serde_json::from_reader(io::stdin())?; let conf: jdb::Config = serde_json::from_reader(io::stdin())?;
db.insert(conf)?; db.insert(conf)?;
Ok(0) Ok(0)
} }
fn destroy(conf: Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> { fn destroy(conf: & Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let mut db = JDB::open(&conf.conf_dir)?; let mut db = JDB::open(&conf.conf_dir)?;
let uuid = value_t!(matches, "uuid", String).unwrap(); let uuid = value_t!(matches, "uuid", String).unwrap();
db.remove(uuid)?; db.remove(uuid.as_str())?;
Ok(0) Ok(0)
} }
\ No newline at end of file
...@@ -16,11 +16,11 @@ pub fn list(pool: &str) -> Vec<ZFSEntry> { ...@@ -16,11 +16,11 @@ pub fn list(pool: &str) -> Vec<ZFSEntry> {
); );
let reply = String::from_utf8_lossy(&output.stdout); let reply = String::from_utf8_lossy(&output.stdout);
println!("{:?}", reply); println!("{:?}", reply);
reply.split("\n").filter(|x| *x != "").map(&deconstruct_entry).collect() reply.split('\n').filter(|x| *x != "").map(&deconstruct_entry).collect()
} }
fn deconstruct_entry(line: &str) -> ZFSEntry { fn deconstruct_entry(line: &str) -> ZFSEntry {
let mut parts = line.split("\t"); let mut parts = line.split('\t');
let name = parts.next().unwrap(); let name = parts.next().unwrap();
let used: u64 = parts.next().unwrap().parse().unwrap(); let used: u64 = parts.next().unwrap().parse().unwrap();
let avail: u64 = parts.next().unwrap().parse().unwrap(); let avail: u64 = parts.next().unwrap().parse().unwrap();
......
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