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, } #[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, /// Note body text #[arg(long)] body: Option, /// Read body from file (`-` = stdin) #[arg(long = "body-file")] body_file: Option, /// Positional title fallback #[arg(value_name = "TITLE")] positional_title: Option, }, } #[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, body_file: Option) -> Result { 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(()) }