summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: dbd2136b565bfee4e035d8d538492a8fc4c59385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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/"
}