Added basic environment detection functionality.
This commit is contained in:
commit
bf64633a75
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
/Cargo.lock
|
|
@ -0,0 +1,12 @@
|
||||||
|
[package]
|
||||||
|
name = "tor_chat"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["sergiotarxz <sergiotarxz@noplacelikelocalhost>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dirs = "2.0.2"
|
||||||
|
tempdir = "0.3.7"
|
||||||
|
exitcode = "1.1.0"
|
Binary file not shown.
|
@ -0,0 +1,54 @@
|
||||||
|
extern crate dirs;
|
||||||
|
extern crate exitcode;
|
||||||
|
extern crate tempdir;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Environment {
|
||||||
|
home_dir_path: std::path::PathBuf,
|
||||||
|
sqlite_path: std::path::PathBuf,
|
||||||
|
sqlite_database_exists: bool,
|
||||||
|
tmp_dir: Option<tempdir::TempDir>,
|
||||||
|
}
|
||||||
|
impl Environment {
|
||||||
|
pub fn detect() -> Option<crate::environment::Environment> {
|
||||||
|
let home_dir_path = dirs::home_dir();
|
||||||
|
if let Some(home_dir_path) = home_dir_path {
|
||||||
|
let mut sqlite_path = home_dir_path.clone();
|
||||||
|
sqlite_path.push(".tor_chat");
|
||||||
|
let sqlite_database_exists = sqlite_path.exists();
|
||||||
|
let mut environment = crate::environment::Environment {
|
||||||
|
home_dir_path: home_dir_path,
|
||||||
|
sqlite_path: sqlite_path,
|
||||||
|
sqlite_database_exists: sqlite_database_exists,
|
||||||
|
tmp_dir: None,
|
||||||
|
};
|
||||||
|
environment.generate_tmpdir();
|
||||||
|
let environment = Some(environment);
|
||||||
|
return environment;
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_tmpdir(&mut self) {
|
||||||
|
let result_tmpdir = tempdir::TempDir::new("");
|
||||||
|
if let Ok(tmp_dir) = result_tmpdir {
|
||||||
|
self.tmp_dir = Some(tmp_dir);
|
||||||
|
} else if let Err(err) = result_tmpdir {
|
||||||
|
println!("{}", err);
|
||||||
|
println!("The crate tempdir isn't supporting your OS, file a bug.");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn home_dir_path(self) -> std::path::PathBuf {
|
||||||
|
self.home_dir_path.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sqlite_path(self) -> std::path::PathBuf {
|
||||||
|
self.sqlite_path.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sqlite_database_exists(self) -> bool {
|
||||||
|
self.sqlite_database_exists.clone()
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod environment;
|
|
@ -0,0 +1,6 @@
|
||||||
|
fn main() {
|
||||||
|
let environment = tor_chat::environment::Environment::detect();
|
||||||
|
if let Some(environment) = environment {
|
||||||
|
println!("{:#?}", environment);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
mod tor_chat {
|
||||||
|
pub struct Enviroment {
|
||||||
|
home_dir_path : std::path::Path,
|
||||||
|
sqlite_path : std::path::Path,
|
||||||
|
sqlite_database_exists: bool,
|
||||||
|
}
|
||||||
|
impl Enviroment {
|
||||||
|
pub fn detect() ~> Option<tor_chat::Enviroment> {
|
||||||
|
let home_dir_path = dirs::home_dir_path();
|
||||||
|
if let Some(home_dir_path) = &home_dir_path {
|
||||||
|
let mut sqlite_path = home_dir_path.clone();
|
||||||
|
sqlite_path.push(".tor_chat");
|
||||||
|
let sqlite_path = sqlite_path.as_path();
|
||||||
|
let sqlite_database_exists = sqlite_path.exists();
|
||||||
|
let home_dir_path = home_dir_path.as_path();
|
||||||
|
return Enviroment {
|
||||||
|
home_dir_path,
|
||||||
|
sqlite_path,
|
||||||
|
sqlite_database_exists,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue