diff options
| author | Christopher Warner <cwarner@kernelcode.com> | 2026-10-10 18:47:24 -0400 |
|---|---|---|
| committer | Christopher Warner <cwarner@kernelcode.com> | 2026-10-10 18:47:24 -0400 |
| commit | 5c61196f27b9342e5f25414c11df5710f956f406 (patch) | |
| tree | c81f15f54c9baa35f61ce3c71b81f1c1e7b5f590 /src/main.rs | |
| parent | 78862ba855c6e66cac64a86c25dca6149d8fb2fd (diff) | |
| download | iphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.tar.gz iphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.tar.bz2 iphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.tar.xz iphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.zip | |
feat!: rewrite icloud-note CLI in Rust with chromiumoxide
Replace the Node/Playwright driver with a single Rust binary so create/status
stay headless without a visible browser. Move the previous implementation to
legacy/nodejs for reference.
BREAKING CHANGE: the supported CLI is the Rust `icloud-note` binary; the
Node entrypoint is no longer installed by setup.sh.
Co-authored-by: Cursor <cursoragent@cursor.com>
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ccd21e2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,176 @@ +use std::io::{self, Read}; +use std::path::PathBuf; +use std::process::ExitCode; + +use anyhow::{bail, Context, Result}; +use clap::{Parser, Subcommand}; + +use icloud_note::browser; +use icloud_note::config::{dry_run, Paths}; +use icloud_note::helpers::{format_note_text, require_create_content}; + +#[derive(Parser, Debug)] +#[command( + name = "icloud-note", + about = "Create Apple Notes from Linux via icloud.com/notes", + long_about = "create/status always run headless (no window).\n\ + login is the only command that opens a visible browser." +)] +struct Cli { + #[command(subcommand)] + cmd: Option<Command>, +} + +#[derive(Subcommand, Debug)] +enum Command { + /// Install Chromium deps + install this binary to ~/.local/bin + Setup, + /// Headless check that the saved session can open Notes + Status, + /// One-time Apple ID sign-in (visible browser) + Login, + /// Headless create a note + Create { + /// Note title (or positional) + #[arg(long)] + title: Option<String>, + /// Note body text + #[arg(long)] + body: Option<String>, + /// Read body from file (`-` = stdin) + #[arg(long = "body-file")] + body_file: Option<PathBuf>, + /// Positional title fallback + #[arg(value_name = "TITLE")] + positional_title: Option<String>, + }, +} + +#[tokio::main] +async fn main() -> ExitCode { + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")), + ) + .with_writer(std::io::stderr) + .init(); + + match run().await { + Ok(()) => ExitCode::SUCCESS, + Err(e) => { + eprintln!("{e:#}"); + ExitCode::FAILURE + } + } +} + +async fn run() -> Result<()> { + let cli = Cli::parse(); + let paths = Paths::resolve(); + + let Some(cmd) = cli.cmd else { + print_help(&paths); + return Ok(()); + }; + + match cmd { + Command::Setup => run_setup(), + Command::Status => { + if dry_run() { + if !paths.has_session() { + bail!("not ready: No saved session. Run: icloud-note login"); + } + println!("ok: dry-run status (would check Notes headless)"); + return Ok(()); + } + browser::cmd_status(&paths) + .await + .map_err(|e| anyhow::anyhow!("not ready: {e}")) + } + Command::Login => { + if dry_run() { + bail!("login dry-run: would open a visible browser (refusing)"); + } + browser::cmd_login(&paths).await + } + Command::Create { + title, + body, + body_file, + positional_title, + } => { + let title = title.or(positional_title).unwrap_or_default(); + let body_raw = read_body(body, body_file)?; + let (title, body) = require_create_content(&title, &body_raw) + .map_err(|e| anyhow::anyhow!(e))?; + if dry_run() { + let text = format_note_text(&title, &body); + println!( + "dry-run: would create note title={} chars={}", + serde_json::to_string(&title)?, + text.len() + ); + return Ok(()); + } + browser::cmd_create(&paths, &title, &body).await + } + } +} + +fn read_body(body: Option<String>, body_file: Option<PathBuf>) -> Result<String> { + if let Some(path) = body_file { + if path.as_os_str() == "-" { + let mut s = String::new(); + io::stdin().read_to_string(&mut s)?; + return Ok(s); + } + return Ok(std::fs::read_to_string(&path) + .with_context(|| format!("read body-file {}", path.display()))?); + } + if let Some(b) = body { + return Ok(b); + } + if !atty_stdin() { + let mut s = String::new(); + io::stdin().read_to_string(&mut s)?; + return Ok(s); + } + Ok(String::new()) +} + +fn atty_stdin() -> bool { + // avoid libc dep: treat non-tty as piped when `isatty` unavailable via std + std::io::IsTerminal::is_terminal(&io::stdin()) +} + +fn print_help(paths: &Paths) { + println!( + "Usage: + icloud-note setup + icloud-note status + icloud-note login # only command that opens a visible browser + icloud-note create --title TITLE [--body TEXT | --body-file PATH|-] + +Config: {} +Profile: {} +create/status always run headless (no window). +", + paths.config_dir.display(), + paths.profile_dir.display() + ); +} + +fn run_setup() -> Result<()> { + let script = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("scripts/setup.sh"); + if !script.is_file() { + bail!("setup.sh not found at {}", script.display()); + } + let status = std::process::Command::new(&script) + .status() + .with_context(|| format!("run {}", script.display()))?; + if !status.success() { + bail!("setup.sh failed with {status}"); + } + Ok(()) +} |
