Skip to content

Commit

Permalink
Add kstarsrc to backed up files
Browse files Browse the repository at this point in the history
Also, this is a major rework of paths, all of them
have been simplified.
  • Loading branch information
MattBlack85 committed Mar 7, 2024
1 parent 283ebe4 commit 1de4005
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
35 changes: 25 additions & 10 deletions src/backup/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> {
let mut arch = Builder::new(file);

// Add all indi devices xml configs to the archive
match arch.append_dir_all("backup/indi", paths.indi_conf_full_path()) {
match arch.append_dir_all("backup/indi", &paths.indi_conf_path) {
Ok(_) => (),
Err(e) => panic!("Couldn't append indi folder to the archive, reason: {}", e),
}

// Add kstars database to the archive
let mut db = match File::open(paths.db_full_path()) {
let mut db = match File::open(&paths.db_path) {
Ok(f) => f,
Err(e) => panic!("Couldn't open the Kstars database, reason: {}", e),
};
Expand All @@ -32,7 +32,7 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> {
}

// Add city database to the archive
let mut city_db = match File::open(paths.city_db_full_path()) {
let mut city_db = match File::open(&paths.city_db_path) {
Ok(f) => f,
Err(e) => panic!("Couldn't open the Kstars city database, reason: {}", e),
};
Expand All @@ -43,7 +43,7 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> {
}

// Add fov.dat to the archive
let mut fov_db = match File::open(paths.fov_full_path()) {
let mut fov_db = match File::open(&paths.fov_path) {
Ok(f) => f,
Err(e) => panic!("Couldn't open the FOV database, reason: {}", e),
};
Expand All @@ -53,6 +53,20 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> {
Err(e) => panic!("Couldn't append the database to the archive, reason: {}", e),
}

// Add kstarsrc to the archive
let mut kstarsrc = match File::open(&paths.kstars_rc_path) {
Ok(f) => f,
Err(e) => panic!("Couldn't open kstarsrc, reason: {}", e),
};

match arch.append_file("backup/kstars/kstarsrc", &mut kstarsrc) {
Ok(_) => (),
Err(e) => panic!(
"Couldn't append the kstarsrc file to the archive, reason: {}",
e
),
}

match arch.finish() {
Ok(_) => (),
Err(e) => panic!("Couldn't create the archive, reason: {}", e),
Expand Down Expand Up @@ -89,7 +103,7 @@ pub fn retrieve_db(paths: &Paths, token: &String) -> Result<(), String> {
{
Ok(r) => {
// Just make sure ~/.indi exists
match std::fs::create_dir(format!("{}/{}", paths.home_path, ".indi")) {
match std::fs::create_dir(&paths.indi_conf_path) {
Ok(_) => (),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => (),
Err(e) => panic!("IO error: {}", e),
Expand All @@ -112,17 +126,18 @@ pub fn retrieve_db(paths: &Paths, token: &String) -> Result<(), String> {
if path.to_str().unwrap().contains(&"indi") {
full_path = format!(
"{}{}",
paths.indi_conf_full_path(),
&paths.indi_conf_path,
&path.file_name().unwrap().to_str().unwrap()
);
} else if path.to_str().unwrap().contains(&"mycity") {
full_path = paths.city_db_full_path();
full_path = paths.city_db_path.to_owned();
} else if path.to_str().unwrap().contains(&"fov") {
full_path = paths.fov_full_path();
full_path = paths.fov_path.to_owned();
} else if path.to_str().unwrap().contains(&"kstarsrc") {
full_path = paths.kstars_rc_path.to_owned();
} else {
full_path = paths.db_full_path();
full_path = paths.db_path.to_owned();
};

tf.read_to_end(&mut s).unwrap();
let mut f = File::create(full_path).unwrap();
f.write(&s).unwrap();
Expand Down
60 changes: 21 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ pub struct CliArgs {
pub struct Paths {
folder_path: String,
logs_path: String,
pub home_path: String,
db_path: String,
city_db_path: String,
indi_conf_path: String,
fov_path: String,
home_path: String,
pub db_path: String,
pub city_db_path: String,
pub indi_conf_path: String,
pub fov_path: String,
pub kstars_rc_path: String,
}

impl Paths {
Expand All @@ -40,44 +41,25 @@ impl Paths {
return root_path;
}

pub fn db_full_path(&self) -> String {
let db_path = format!("{}/{}", self.home_path, self.db_path);
return db_path;
}

pub fn city_db_full_path(&self) -> String {
let db_path = format!("{}/{}", self.home_path, self.city_db_path);
return db_path;
}

pub fn fov_full_path(&self) -> String {
let path = format!("{}/{}", self.home_path, self.fov_path);
return path;
}

pub fn indi_conf_full_path(&self) -> String {
let indi_conf_path = format!("{}/{}", self.home_path, self.indi_conf_path);
return indi_conf_path;
}

pub fn init() -> Self {
let home_path = dirs::home_dir().unwrap().as_path().display().to_string();
let config_path = dirs::preference_dir()
.unwrap()
.as_path()
.display()
.to_string();
let data_path = dirs::data_dir().unwrap().as_path().display().to_string();

Self {
folder_path: String::from(".local/share/astromonitor"),
logs_path: String::from("logs"),
home_path: dirs::home_dir().unwrap().as_path().display().to_string(),
#[cfg(target_os = "linux")]
db_path: String::from(".local/share/kstars/userdb.sqlite"),
#[cfg(target_os = "macos")]
db_path: String::from("Library/Application Support/kstars/userdb.sqlite"),
indi_conf_path: String::from(".indi/"),
#[cfg(target_os = "linux")]
city_db_path: String::from(".local/share/kstars/mycitydb.sqlite"),
#[cfg(target_os = "macos")]
city_db_path: String::from("Library/Application Support/kstars/mycitydb.sqlite"),
#[cfg(target_os = "linux")]
fov_path: String::from(".local/share/kstars/fov.dat"),
#[cfg(target_os = "macos")]
fov_path: String::from("Library/Application Support/kstars/fov.dat")
home_path: home_path.clone(),

db_path: format!("{}/kstars/userdb.sqlite", &data_path),
city_db_path: format!("{}/kstars/mycitydb.sqlite", &data_path),
fov_path: format!("{}/kstars/fov.dat", &data_path),
indi_conf_path: format!("{}/.indi/", &home_path),
kstars_rc_path: format!("{}/kstarsrc", &config_path),
}
}
}

0 comments on commit 1de4005

Please sign in to comment.