summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..dbd2136
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,51 @@
+use std::path::PathBuf;
+
+#[derive(Debug, Clone)]
+pub struct Paths {
+ pub config_dir: PathBuf,
+ pub profile_dir: PathBuf,
+ pub marker_path: PathBuf,
+}
+
+impl Paths {
+ pub fn resolve() -> Self {
+ let config_dir = std::env::var_os("ICLOUD_NOTES_CONFIG")
+ .map(PathBuf::from)
+ .unwrap_or_else(|| {
+ dirs::config_dir()
+ .unwrap_or_else(|| PathBuf::from(".").join(".config"))
+ .join("icloud-notes")
+ });
+ Self {
+ profile_dir: config_dir.join("browser-profile"),
+ marker_path: config_dir.join("session.ok"),
+ config_dir,
+ }
+ }
+
+ pub fn ensure(&self) -> std::io::Result<()> {
+ std::fs::create_dir_all(&self.config_dir)?;
+ std::fs::create_dir_all(&self.profile_dir)?;
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ let _ = std::fs::set_permissions(&self.config_dir, std::fs::Permissions::from_mode(0o700));
+ let _ = std::fs::set_permissions(&self.profile_dir, std::fs::Permissions::from_mode(0o700));
+ }
+ Ok(())
+ }
+
+ pub fn has_session(&self) -> bool {
+ self.marker_path.is_file()
+ || self.profile_dir.join("Default").is_dir()
+ || self.profile_dir.join("Cookies").is_file()
+ }
+}
+
+pub fn dry_run() -> bool {
+ matches!(std::env::var("ICLOUD_NOTES_DRY_RUN").as_deref(), Ok("1"))
+}
+
+pub fn notes_url() -> &'static str {
+ "https://www.icloud.com/notes/"
+}