|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use crate::appimage::AppImage; |
| 4 | +use crate::error::{Error, Result}; |
| 5 | +use crate::update_info::UpdateInfo; |
| 6 | + |
| 7 | +pub struct Updater { |
| 8 | + #[allow(dead_code)] |
| 9 | + appimage: AppImage, |
| 10 | + #[allow(dead_code)] |
| 11 | + update_info: UpdateInfo, |
| 12 | + output_dir: PathBuf, |
| 13 | + overwrite: bool, |
| 14 | +} |
| 15 | + |
| 16 | +impl Updater { |
| 17 | + pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> { |
| 18 | + let path = path.as_ref(); |
| 19 | + let appimage = AppImage::open(path)?; |
| 20 | + let update_info_str = appimage.read_update_info()?; |
| 21 | + let update_info = UpdateInfo::parse(&update_info_str)?; |
| 22 | + |
| 23 | + Ok(Self { |
| 24 | + appimage, |
| 25 | + update_info, |
| 26 | + output_dir: std::env::current_dir().map_err(|e| { |
| 27 | + Error::Io(std::io::Error::other(format!( |
| 28 | + "Failed to get current directory: {}", |
| 29 | + e |
| 30 | + ))) |
| 31 | + })?, |
| 32 | + overwrite: false, |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + pub fn with_update_info<P: AsRef<Path>>(path: P, update_info: &str) -> Result<Self> { |
| 37 | + let appimage = AppImage::open(path.as_ref())?; |
| 38 | + let update_info = UpdateInfo::parse(update_info)?; |
| 39 | + |
| 40 | + Ok(Self { |
| 41 | + appimage, |
| 42 | + update_info, |
| 43 | + output_dir: std::env::current_dir().map_err(|e| { |
| 44 | + Error::Io(std::io::Error::other(format!( |
| 45 | + "Failed to get current directory: {}", |
| 46 | + e |
| 47 | + ))) |
| 48 | + })?, |
| 49 | + overwrite: false, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + pub fn output_dir<P: AsRef<Path>>(mut self, dir: P) -> Self { |
| 54 | + self.output_dir = dir.as_ref().to_path_buf(); |
| 55 | + self |
| 56 | + } |
| 57 | + |
| 58 | + pub fn overwrite(mut self, overwrite: bool) -> Self { |
| 59 | + self.overwrite = overwrite; |
| 60 | + self |
| 61 | + } |
| 62 | + |
| 63 | + pub fn check_for_update(&self) -> Result<bool> { |
| 64 | + todo!("Implement update check") |
| 65 | + } |
| 66 | + |
| 67 | + pub fn perform_update(&self) -> Result<PathBuf> { |
| 68 | + todo!("Implement update") |
| 69 | + } |
| 70 | +} |
0 commit comments