Skip to content

Commit

Permalink
Backup PHD2 profile if available
Browse files Browse the repository at this point in the history
  • Loading branch information
MattBlack85 committed Mar 8, 2024
1 parent baeb790 commit 0706242
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/backup/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ fn check_city_db_exists(paths: &Paths) -> bool {
Path::new(&paths.city_db_path).exists()
}

fn check_indi_folder_exists(paths: &Paths) -> bool {
Path::new(&paths.indi_conf_path).exists()
}

fn check_phd2_profile_exists(paths: &Paths) -> bool {
Path::new(&paths.phd2_profile_path).exists()
}

pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> {
// Init the zip archive
let file = match File::create("/tmp/k_backup.tar") {
Expand All @@ -19,10 +27,12 @@ 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_path) {
Ok(_) => (),
Err(e) => panic!("Couldn't append indi folder to the archive, reason: {}", e),
if check_indi_folder_exists(paths) {
// Add all indi devices xml configs to the archive
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
Expand All @@ -49,6 +59,22 @@ pub fn send_db(paths: &Paths, token: &String) -> Result<(), String> {
}
}

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

match arch.append_file(format!("backup/{}", paths.phd2_filename), &mut phd2) {
Ok(_) => (),
Err(e) => panic!(
"Couldn't append the PHD2 profile to the archive, reason: {}",
e
),
}
}

// Add fov.dat to the archive
let mut fov_db = match File::open(&paths.fov_path) {
Ok(f) => f,
Expand Down Expand Up @@ -142,6 +168,8 @@ pub fn retrieve_db(paths: &Paths, token: &String) -> Result<(), String> {
full_path = paths.fov_path.to_owned();
} else if path.to_str().unwrap().contains(&"kstarsrc") {
full_path = paths.kstars_rc_path.to_owned();
} else if path.to_str().unwrap().contains(&"PHD") {
full_path = paths.phd2_profile_path.to_owned();
} else {
full_path = paths.db_path.to_owned();
};
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct Paths {
pub indi_conf_path: String,
pub fov_path: String,
pub kstars_rc_path: String,
pub phd2_profile_path: String,
pub phd2_filename: String,
}

impl Paths {
Expand All @@ -50,6 +52,12 @@ impl Paths {
.to_string();
let data_path = dirs::data_dir().unwrap().as_path().display().to_string();

#[cfg(target_os = "linux")]
let phd2_filename = String::from(".PHDGuidingV2");

#[cfg(target_os = "macos")]
let phd2_filename = String::from("PHDGuidingV2_Preferences");

Self {
folder_path: String::from(".local/share/astromonitor"),
logs_path: String::from("logs"),
Expand All @@ -60,6 +68,11 @@ impl Paths {
fov_path: format!("{}/kstars/fov.dat", &data_path),
indi_conf_path: format!("{}/.indi/", &home_path),
kstars_rc_path: format!("{}/kstarsrc", &config_path),
phd2_filename: phd2_filename.clone(),
#[cfg(target_os = "linux")]
phd2_profile_path: format!("{}/{}", &home_path, &phd2_filename),
#[cfg(target_os = "macos")]
phd2_profile_path: format!("{}/{}", &config_path, &phd2_filename),
}
}
}

0 comments on commit 0706242

Please sign in to comment.