general API consolidation
This commit is contained in:
parent
c246de0c99
commit
fd79455de4
2 changed files with 21 additions and 24 deletions
|
|
@ -29,20 +29,20 @@ enum FileState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
fn cache_dir() -> Option<PathBuf> {
|
fn cache_dir() -> PathBuf {
|
||||||
let dir_name = dirs_next::cache_dir()?.join("shrupl");
|
let dir_name = dirs_next::cache_dir()
|
||||||
|
.expect("could not determine cache directory")
|
||||||
|
.join("shrupl");
|
||||||
|
|
||||||
trace!("cachedir: {}", dir_name.display());
|
trace!("cachedir: {}", dir_name.display());
|
||||||
|
dir_name
|
||||||
Some(dir_name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cache_file(args: &Cli) -> Option<PathBuf> {
|
fn cache_file(args: &Cli) -> PathBuf {
|
||||||
let file_name = Self::cache_dir()?.join(format!("{}.json", args.get_hash()));
|
let file_name = Self::cache_dir().join(format!("{}.json", args.get_hash()));
|
||||||
|
|
||||||
trace!("cachefile: {}", file_name.display());
|
trace!("cachefile: {}", file_name.display());
|
||||||
|
file_name
|
||||||
Some(file_name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load(file_name: impl AsRef<Path>) -> io::Result<Self> {
|
fn load(file_name: impl AsRef<Path>) -> io::Result<Self> {
|
||||||
|
|
@ -51,7 +51,7 @@ impl AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_resume(args: &Cli) -> Option<Self> {
|
pub fn try_resume(args: &Cli) -> Option<Self> {
|
||||||
let file_name = Self::cache_file(args)?;
|
let file_name = Self::cache_file(args);
|
||||||
|
|
||||||
Self::load(&file_name)
|
Self::load(&file_name)
|
||||||
.inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display()))
|
.inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display()))
|
||||||
|
|
@ -68,16 +68,15 @@ impl AppState {
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_args(args: Cli, agent: &ureq::Agent) -> Option<Self> {
|
pub fn from_args(args: &Cli, agent: &ureq::Agent) -> Option<Self> {
|
||||||
let file_name = Self::cache_file(&args)?;
|
let file_name = Self::cache_file(args);
|
||||||
|
|
||||||
let alias = args.get_alias();
|
let alias = args.get_alias();
|
||||||
|
|
||||||
let share = Share::create(&agent, &alias, args.get_share_request())
|
let share = Share::create(agent, &alias, args.get_share_request())
|
||||||
.inspect_err(|e| error!("could not create Share: {e}"))
|
.inspect_err(|e| error!("could not create Share: {e}"))
|
||||||
.ok()?;
|
.ok()?;
|
||||||
|
|
||||||
let files: Vec<_> = args.files.into_iter().map(|f| FileState::C(f)).collect();
|
let files: Vec<_> = args.files.clone().into_iter().map(FileState::C).collect();
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
file_name,
|
file_name,
|
||||||
|
|
@ -88,24 +87,20 @@ impl AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&self) -> io::Result<()> {
|
pub fn save(&self) -> io::Result<()> {
|
||||||
let cache_dir = Self::cache_dir()
|
fs::create_dir_all(Self::cache_dir())?;
|
||||||
.ok_or_else(|| io::Error::other("could not determine cache directory"))?;
|
|
||||||
fs::create_dir_all(cache_dir)?;
|
|
||||||
|
|
||||||
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
|
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
|
||||||
let mut file = fs::File::create(&self.file_name)?;
|
let mut file = fs::File::create(&self.file_name)?;
|
||||||
file.write_all(json.as_bytes())?;
|
file.write_all(json.as_bytes())?;
|
||||||
|
|
||||||
trace!("successfully saved AppState");
|
trace!("successfully saved AppState");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(self) -> io::Result<()> {
|
pub fn clear(self) -> io::Result<()> {
|
||||||
fs::remove_file(self.file_name)?;
|
fs::remove_file(self.file_name)?;
|
||||||
|
|
||||||
trace!("successfully cleared AppState");
|
trace!("successfully cleared AppState");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/main.rs
10
src/main.rs
|
|
@ -23,19 +23,21 @@ fn main() {
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
let state = AppState::try_resume(&args)
|
let state = AppState::try_resume(&args)
|
||||||
.and_then(|state| {
|
.map(|state| {
|
||||||
info!("loaded state: {state:?}");
|
info!("loaded state: {state:?}");
|
||||||
|
|
||||||
Some(state)
|
state
|
||||||
})
|
})
|
||||||
.or_else(|| AppState::from_args(args, &agent))
|
.or_else(|| AppState::from_args(&args, &agent))
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
|
error!("could not create new state from cli arguments: {args:?}");
|
||||||
|
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
info!("continuing with state: {state:?}");
|
info!("continuing with state: {state:?}");
|
||||||
state.save().unwrap();
|
state.save().unwrap();
|
||||||
// state.delete().unwrap();
|
// state.clear().unwrap();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue