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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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(())
}
|