Browse Source

Enhance code formatting

pull/5/head
Rodolphe Breard 6 years ago
parent
commit
b8fed59235
  1. 10
      acmed/src/acmed.rs
  2. 4
      acmed/src/config.rs
  3. 8
      acmed/src/encoding.rs
  4. 2
      acmed/src/hooks.rs
  5. 14
      acmed/src/logs.rs
  6. 2
      acmed/src/main.rs
  7. 55
      acmed/src/storage.rs

10
acmed/src/acmed.rs

@ -1,13 +1,13 @@
use acme_lib::{Directory, DirectoryUrl};
use crate::config::{self, Hook}; use crate::config::{self, Hook};
use crate::errors::Error; use crate::errors::Error;
use crate::hooks; use crate::hooks;
use crate::storage::Storage; use crate::storage::Storage;
use acme_lib::{Directory, DirectoryUrl};
use log::{debug, info, warn}; use log::{debug, info, warn};
use openssl; use openssl;
use serde::Serialize; use serde::Serialize;
use std::{fmt, thread};
use std::time::Duration; use std::time::Duration;
use std::{fmt, thread};
use x509_parser::parse_x509_der; use x509_parser::parse_x509_der;
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)] #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
@ -220,10 +220,12 @@ impl Certificate {
let mut raw_crt = vec![]; let mut raw_crt = vec![];
let mut raw_pk = vec![]; let mut raw_pk = vec![];
if self.kp_reuse { if self.kp_reuse {
raw_crt = self.storage
raw_crt = self
.storage
.get_certificate(&Format::Der)? .get_certificate(&Format::Der)?
.unwrap_or_else(|| vec![]); .unwrap_or_else(|| vec![]);
raw_pk = self.storage
raw_pk = self
.storage
.get_private_key(&Format::Der)? .get_private_key(&Format::Der)?
.unwrap_or_else(|| vec![]); .unwrap_or_else(|| vec![]);
}; };

4
acmed/src/config.rs

@ -3,8 +3,8 @@ use crate::errors::Error;
use log::info; use log::info;
use serde::Deserialize; use serde::Deserialize;
use std::fs::{self, File}; use std::fs::{self, File};
use std::path::Path;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::Path;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Config { pub struct Config {
@ -266,7 +266,7 @@ impl Certificate {
} }
} }
fn get_hooks(lst: &Vec<String>, cnf: &Config) -> Result<Vec<Hook>, Error> {
fn get_hooks(lst: &[String], cnf: &Config) -> Result<Vec<Hook>, Error> {
let mut res = vec![]; let mut res = vec![];
for name in lst.iter() { for name in lst.iter() {
let mut h = cnf.get_hook(&name)?; let mut h = cnf.get_hook(&name)?;

8
acmed/src/encoding.rs

@ -1,6 +1,6 @@
use acme_lib::Error;
use acme_lib::persist::PersistKind;
use crate::acmed::Format; use crate::acmed::Format;
use acme_lib::persist::PersistKind;
use acme_lib::Error;
use log::debug; use log::debug;
use pem::{encode, Pem}; use pem::{encode, Pem};
@ -71,9 +71,9 @@ pub fn convert(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use acme_lib::persist::PersistKind;
use crate::acmed::Format;
use super::convert; use super::convert;
use crate::acmed::Format;
use acme_lib::persist::PersistKind;
// Test data generated using: // Test data generated using:
// //

2
acmed/src/hooks.rs

@ -20,7 +20,7 @@ macro_rules! get_hook_output {
}}; }};
} }
pub fn call_multiple<T: Serialize>(data: &T, hooks: &Vec<Hook>) -> Result<(), Error> {
pub fn call_multiple<T: Serialize>(data: &T, hooks: &[Hook]) -> Result<(), Error> {
for hook in hooks.iter() { for hook in hooks.iter() {
call(data, &hook)?; call(data, &hook)?;
} }

14
acmed/src/logs.rs

@ -44,13 +44,13 @@ pub fn set_log_system(
has_stderr: bool, has_stderr: bool,
) -> Result<(LogSystem, LevelFilter), Error> { ) -> Result<(LogSystem, LevelFilter), Error> {
let log_level = get_loglevel(log_level)?; let log_level = get_loglevel(log_level)?;
let mut logtype = crate::DEFAULT_LOG_SYSTEM;
if has_stderr {
logtype = LogSystem::StdErr;
}
if has_syslog {
logtype = LogSystem::SysLog;
}
let logtype = if has_syslog {
LogSystem::SysLog
} else if has_stderr {
LogSystem::StdErr
} else {
crate::DEFAULT_LOG_SYSTEM
};
match logtype { match logtype {
LogSystem::SysLog => set_log_syslog(log_level)?, LogSystem::SysLog => set_log_syslog(log_level)?,
LogSystem::StdErr => set_log_stderr(log_level)?, LogSystem::StdErr => set_log_stderr(log_level)?,

2
acmed/src/main.rs

@ -61,7 +61,7 @@ fn main() {
Arg::with_name("foregroung") Arg::with_name("foregroung")
.short("f") .short("f")
.long("foregroung") .long("foregroung")
.help("Runs in the foregroung")
.help("Runs in the foregroung"),
) )
.arg( .arg(
Arg::with_name("pid-file") Arg::with_name("pid-file")

55
acmed/src/storage.rs

@ -1,10 +1,10 @@
use acme_lib::Error;
use acme_lib::persist::{Persist, PersistKey, PersistKind};
use crate::acmed::{Algorithm, Format}; use crate::acmed::{Algorithm, Format};
use crate::config::Hook; use crate::config::Hook;
use crate::errors;
use crate::encoding::convert; use crate::encoding::convert;
use crate::errors;
use crate::hooks; use crate::hooks;
use acme_lib::persist::{Persist, PersistKey, PersistKind};
use acme_lib::Error;
use log::debug; use log::debug;
use serde::Serialize; use serde::Serialize;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
@ -78,25 +78,29 @@ impl Storage {
} }
}; };
let uid = match uid { let uid = match uid {
Some(u) => if u.bytes().all(|b| b.is_ascii_digit()) {
let raw_uid = u.parse::<u32>().unwrap();
let nix_uid = nix::unistd::Uid::from_raw(raw_uid);
Some(nix_uid)
} else {
// TODO: handle username
None
},
Some(u) => {
if u.bytes().all(|b| b.is_ascii_digit()) {
let raw_uid = u.parse::<u32>().unwrap();
let nix_uid = nix::unistd::Uid::from_raw(raw_uid);
Some(nix_uid)
} else {
// TODO: handle username
None
}
}
None => None, None => None,
}; };
let gid = match gid { let gid = match gid {
Some(g) => if g.bytes().all(|b| b.is_ascii_digit()) {
let raw_gid = g.parse::<u32>().unwrap();
let nix_gid = nix::unistd::Gid::from_raw(raw_gid);
Some(nix_gid)
} else {
// TODO: handle group name
None
},
Some(g) => {
if g.bytes().all(|b| b.is_ascii_digit()) {
let raw_gid = g.parse::<u32>().unwrap();
let nix_gid = nix::unistd::Gid::from_raw(raw_gid);
Some(nix_gid)
} else {
// TODO: handle group name
None
}
}
None => None, None => None,
}; };
match nix::unistd::chown(path, uid, gid) { match nix::unistd::chown(path, uid, gid) {
@ -122,7 +126,7 @@ impl Storage {
path.push(&file_name); path.push(&file_name);
FileData { FileData {
file_directory: base_path.to_string(), file_directory: base_path.to_string(),
file_name: file_name,
file_name,
file_path: path, file_path: path,
} }
} }
@ -170,13 +174,17 @@ impl Persist for Storage {
if file_exists { if file_exists {
hooks::call_multiple(&file_data, &self.file_pre_edit_hooks).map_err(to_acme_err)?; hooks::call_multiple(&file_data, &self.file_pre_edit_hooks).map_err(to_acme_err)?;
} else { } else {
hooks::call_multiple(&file_data, &self.file_pre_create_hooks).map_err(to_acme_err)?;
hooks::call_multiple(&file_data, &self.file_pre_create_hooks)
.map_err(to_acme_err)?;
} }
{ {
let mut f = if cfg!(unix) { let mut f = if cfg!(unix) {
let mut options = OpenOptions::new(); let mut options = OpenOptions::new();
options.mode(self.get_file_mode(key.kind)); options.mode(self.get_file_mode(key.kind));
options.write(true).create(true).open(&file_data.file_path)?
options
.write(true)
.create(true)
.open(&file_data.file_path)?
} else { } else {
File::create(&file_data.file_path)? File::create(&file_data.file_path)?
}; };
@ -193,7 +201,8 @@ impl Persist for Storage {
self.set_owner(&file_data.file_path, key.kind)?; self.set_owner(&file_data.file_path, key.kind)?;
} }
if file_exists { if file_exists {
hooks::call_multiple(&file_data, &self.file_post_edit_hooks).map_err(to_acme_err)?;
hooks::call_multiple(&file_data, &self.file_post_edit_hooks)
.map_err(to_acme_err)?;
} else { } else {
hooks::call_multiple(&file_data, &self.file_post_create_hooks) hooks::call_multiple(&file_data, &self.file_post_create_hooks)
.map_err(to_acme_err)?; .map_err(to_acme_err)?;

Loading…
Cancel
Save