//! Land rows recovered into a local SQLite file on the object, sealed //! (`cotton_mac::seal_land`), run on the Mac that is an approved vault device //! of the object. The file is never written; progress goes to its //! own file, so a stopped run resumes. //! //! cargo run -p cotton-mac --bin seal-land -- --base --device-id \ //! --token-file [--key-file ] --file [--progress ] \ //! [--tables a,b] [--batch 500] [--allow-open] //! //! --base the object's address (the dev Worker first; the live one only by the coordinator) //! --device-id this Mac's device id: its vault key is the login Keychain item of that id //! --token-file a file holding this Mac's DEVICE credential (never a `ctn_` token); never printed //! --file the recovered rows: `rows(table_id, id, data, created_at, updated_at, deleted)` //! --progress where landed rows are kept (default: .landed.db beside it) //! --tables the tables to land; every table the file holds when absent //! --batch rows per import_rows call (at most 500) //! --allow-open land rows of a table the object does not seal, as they are //! //! stdout is the run's summary as JSON: counts and row ids, never content. //! Exit 0 when every table landed and read back whole; 1 otherwise. use std::path::PathBuf; use std::process::ExitCode; use cotton_mac::object::ObjectClient; use cotton_mac::seal_land::{land, open_file, open_progress, report, LandOptions}; use cotton_mac::seal_migrate::{data_key_row, ObjectLine}; use cotton_mac::vault::{tool_keyring, ImporterVault}; const USAGE: &str = "seal-land --base --device-id --token-file [--key-file ] --file [--progress ] [--tables a,b] [--batch n] [--allow-open]"; struct Args { base: String, device_id: String, token_file: PathBuf, key_file: Option, file: PathBuf, progress: Option, options: LandOptions, } fn parse(argv: &[String]) -> Result { let (mut base, mut device_id, mut token_file, mut file, mut progress) = (None, None, None, None, None); let mut options = LandOptions::default(); let mut key_file = None; let mut at = 0; while at < argv.len() { let flag = argv[at].as_str(); if flag == "--allow-open" { options.allow_open = true; at += 1; continue; } let value = argv.get(at + 1).filter(|v| !v.starts_with("--")).ok_or_else(|| format!("{flag} needs a value"))?.clone(); match flag { "--base" => base = Some(value), "--device-id" => device_id = Some(value), "--token-file" => token_file = Some(PathBuf::from(value)), "--key-file" => key_file = Some(PathBuf::from(value)), "--file" => file = Some(PathBuf::from(value)), "--progress" => progress = Some(PathBuf::from(value)), "--tables" => options.tables = value.split(',').filter(|t| !t.is_empty()).map(str::to_string).collect(), "--batch" => options.batch = value.parse().map_err(|_| format!("--batch takes a number, not {value}"))?, other => return Err(format!("unknown argument {other}")), } at += 2; } Ok(Args { base: base.ok_or("--base is needed")?, device_id: device_id.ok_or("--device-id is needed")?, token_file: token_file.ok_or("--token-file is needed")?, key_file, file: file.ok_or("--file is needed")?, progress, options, }) } fn go(args: Args) -> Result { let token = std::fs::read_to_string(&args.token_file).map_err(|e| format!("{} could not be read: {e}", args.token_file.display()))?.trim().to_string(); let object = ObjectClient::new(args.base, Some(token)).map_err(|e| e.to_string())?; let file = open_file(&args.file)?; let progress_path = args.progress.unwrap_or_else(|| { let mut p = args.file.clone().into_os_string(); p.push(".landed.db"); PathBuf::from(p) }); let progress = open_progress(&progress_path)?; let database_id = object.database_id().map_err(|e| format!("the object's database id could not be read: {e}"))?; let keyring = tool_keyring(&args.device_id, args.key_file.as_deref()); let vault = ImporterVault::new(keyring.as_ref(), &args.device_id, ""); let open = vault .open_only(&object) .map_err(|e| e.to_string())? .ok_or("vault_locked: this Mac's vault is not open (no key in the Keychain, or the phone has not approved this Mac in Keys › Devices)")?; let dk = open.data_key(&data_key_row(&mut ObjectLine::new(&object))?).map_err(|e| format!("{}: the data key did not open ({e})", e.code()))?; drop(open); let runs = land(&object, &file, &progress, &database_id, &dk, &args.options)?; println!("{}", serde_json::to_string_pretty(&report(&runs)).unwrap_or_default()); Ok(runs.iter().all(cotton_mac::seal_land::Landed::done)) } fn main() -> ExitCode { let argv: Vec = std::env::args().skip(1).collect(); let args = match parse(&argv) { Ok(args) => args, Err(why) => { eprintln!("{why}\n{USAGE}"); return ExitCode::from(2); } }; match go(args) { Ok(true) => ExitCode::SUCCESS, Ok(false) => ExitCode::from(1), Err(why) => { eprintln!("{why}"); ExitCode::from(1) } } }