semora-cli 0.7.0

Semora CLI: publish Markdown and HTML, and hear back from the page
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! `semora push`: publish a file or folder.

use std::collections::BTreeMap;
use std::path::Path;

use anyhow::{Context, bail};
use semora_proto::PushResponse;
use serde_json::{Map, Value};

use crate::args::PushArgs;
use crate::bundle;
use crate::checkout::{self, Record};
use crate::client::{Client, PushQuery, encode_path};
use crate::project_link::{self, Link};
use crate::remember::{self, Memo};
use crate::{Ctx, docref, exit, output};

/// What to send for `path`: body, content type, single-file name, warnings.
pub struct Payload {
    pub body: Vec<u8>,
    pub content_type: &'static str,
    pub filename: Option<String>,
    /// The sha256 of each file sent, by its path in the pushed folder (`""` for a single file).
    pub hashes: BTreeMap<String, String>,
}

pub fn payload(path: &Path) -> anyhow::Result<Payload> {
    let meta =
        std::fs::metadata(path).with_context(|| format!("cannot read {}", path.display()))?;
    if meta.is_dir() {
        let packed = bundle::pack_dir(path)?;
        for (p, why) in &packed.skipped {
            output::warn(format!("skipped {p} ({why})"));
        }
        if packed.files.is_empty() {
            bail!("{} has no files Semora can serve", path.display());
        }
        if !packed.files.iter().any(|f| bundle::is_page(f)) {
            bail!("{} has no Markdown or HTML page", path.display());
        }
        return Ok(Payload {
            body: packed.tar,
            content_type: "application/x-tar",
            filename: None,
            hashes: packed.hashes,
        });
    }
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    let lower = name.to_ascii_lowercase();
    let content_type = if lower.ends_with(".md") || lower.ends_with(".markdown") {
        "text/markdown; charset=utf-8"
    } else if lower.ends_with(".html") || lower.ends_with(".htm") {
        "text/html; charset=utf-8"
    } else {
        bail!("{name}: push a .md or .html file, or a folder");
    };
    let body = std::fs::read(path).with_context(|| format!("reading {}", path.display()))?;
    if content_type.starts_with("text/markdown") {
        let links = bundle::local_page_links(&String::from_utf8_lossy(&body));
        if !links.is_empty() {
            output::warn(format!(
                "{name} links to local pages ({}); push the folder to publish them together as a bundle",
                links.join(", ")
            ));
        }
    }
    Ok(Payload {
        hashes: [(String::new(), checkout::sha256(&body))].into(),
        body,
        content_type,
        filename: Some(name),
    })
}

/// `key=value` pairs; values that parse as JSON scalars keep their type.
pub fn parse_meta(pairs: &[String]) -> anyhow::Result<Option<String>> {
    if pairs.is_empty() {
        return Ok(None);
    }
    let mut m = Map::new();
    for p in pairs {
        let (k, v) = p
            .split_once('=')
            .with_context(|| format!("--meta `{p}` must be key=value"))?;
        let value = match serde_json::from_str::<Value>(v) {
            Ok(j @ (Value::Number(_) | Value::Bool(_) | Value::Null)) => j,
            _ => Value::String(v.to_string()),
        };
        m.insert(k.trim().to_string(), value);
    }
    Ok(Some(Value::Object(m).to_string()))
}

pub fn tags(args_tags: &[String], defaults: &[String]) -> Option<String> {
    let all: Vec<String> = args_tags
        .iter()
        .flat_map(|t| t.split(','))
        .chain(defaults.iter().map(String::as_str))
        .map(str::trim)
        .filter(|t| !t.is_empty())
        .map(str::to_string)
        .collect();
    (!all.is_empty()).then(|| all.join(","))
}

/// One line when `--id` or `--new` pushes outside the project that holds `path`.
pub fn note_outside_project(path: &Path, id: Option<&str>, new: bool) {
    if id.is_none() && !new {
        return;
    }
    let Ok(Some(link)) = project_link::find(path) else {
        return;
    };
    if id.is_some() {
        eprintln!(
            "note: --id pushes to that document directly, not into project {}.",
            link.slug
        );
    } else {
        eprintln!(
            "note: --new makes a document outside project {}.",
            link.slug
        );
    }
}

/// Adds a command's own way out of a `.semora` for another server: `flags` is `--id or --new`
/// for push, `--id` for watch. Other errors pass through.
pub fn hint_outside_project(err: anyhow::Error, flags: &str) -> anyhow::Error {
    if err.downcast_ref::<project_link::OtherServer>().is_some() {
        anyhow::anyhow!("{err} Or use {flags} to push outside the project.")
    } else {
        err
    }
}

/// An old server ignores the project parameters and makes a plain document.
pub fn check_filed(resp: &PushResponse, slug: &str) -> anyhow::Result<()> {
    if resp.project.is_none() {
        bail!(
            "the server did not file this under project `{slug}`; it may be older than this CLI. Update the server."
        );
    }
    Ok(())
}

/// A push the server refused because someone pushed after the revision this checkout has.
fn stale_push(
    client: &Client,
    link: &Link,
    key: &str,
    path: &Path,
    bundle: bool,
    err: &anyhow::Error,
) -> anyhow::Error {
    let shown = path.display();
    let url = client.url(&format!("/p/{}/{}", link.slug, encode_path(key)));
    let get = if bundle {
        format!("`semora show {url}` lists its files, `semora get {url} FILE` prints one")
    } else {
        format!("`semora get {url}` prints it")
    };
    exit::stale(format!(
        "{}. It changed on the server after this folder last pulled or pushed it. Merge the server's copy into {shown} ({get}) and push again with --force, or run `semora pull --force {shown}` to take the server's copy and drop your changes.",
        exit::message_of(err)
    ))
}

/// A push by key: the server updates the document at `key` in the project, or makes one. When
/// this checkout has a record of the document, the push names that revision, and the server
/// refuses it if someone pushed since; `force` leaves the revision out.
#[allow(clippy::too_many_arguments)]
async fn push_keyed(
    ctx: &Ctx,
    client: &Client,
    path: &Path,
    link: &Link,
    key: &str,
    p: Payload,
    mut q: PushQuery,
    force: bool,
) -> anyhow::Result<PushResponse> {
    let server = client.origin();
    q.project = Some(link.slug.clone());
    q.path = Some(key.to_string());
    q.origin = Some(project_link::origin_for(path));
    // An older document remembered for this path is carried into the project.
    q.adopt = remember::load(&ctx.settings.config_dir, path)
        .filter(|m| m.server == server)
        .map(|m| m.id);
    let known = if force {
        None
    } else {
        checkout::load(link)?.remove(key)
    };
    let if_match = known.map(|r| {
        q.base = Some(r.id);
        r.rev
    });
    let files: BTreeMap<String, String> = p
        .hashes
        .iter()
        .map(|(rel, hash)| {
            let local = if rel.is_empty() {
                key.to_string()
            } else {
                format!("{key}/{rel}")
            };
            (local, hash.clone())
        })
        .collect();
    let bundle = p.filename.is_none();
    let resp = match client
        .push(None, p.body, p.content_type, &q, if_match)
        .await
    {
        Ok(r) => r,
        Err(e) if exit::is_stale(&e) => {
            return Err(stale_push(client, link, key, path, bundle, &e));
        }
        Err(e) if exit::is_not_found(&e) => {
            return Err(exit::not_found(format!(
                "project `{}` was not found on {server}. Run `semora project init` to create it again, or you may no longer have access to this project.",
                link.slug
            )));
        }
        Err(e) if exit::is_conflict(&e) => bail!(
            "the server could not file this path in project `{}`: another push got there first, or you may no longer have access to this project. Try again.",
            link.slug
        ),
        Err(e) => return Err(e),
    };
    check_filed(&resp, &link.slug)?;
    let record = Record {
        id: resp.id.clone(),
        rev: resp.rev,
        kind: resp.kind,
        files,
    };
    if let Err(e) = checkout::update(link, |r| r.insert(key.to_string(), record)) {
        output::warn(format!(
            "could not record the pushed revision, so the next push of {key} can't check for newer ones: {e:#}"
        ));
    }
    Ok(resp)
}

/// Pushes `path`. Inside a project (a `.semora` above it) it goes by key, unless `--id` or
/// `--new` says otherwise; outside one it replaces the explicit or remembered document.
/// `force` pushes by key even when the server has a newer revision than this checkout.
pub async fn push_path(
    ctx: &Ctx,
    client: &Client,
    path: &Path,
    explicit_id: Option<&str>,
    force_new: bool,
    force: bool,
    query: &PushQuery,
) -> anyhow::Result<PushResponse> {
    // Decided before anything is read or packed, so a refusal costs nothing.
    let keyed = if explicit_id.is_none() && !force_new {
        match project_link::find(path)? {
            Some(link) => {
                link.check_server(client)?;
                let key = link.relative_path(path)?;
                Some((link, key))
            }
            None => None,
        }
    } else {
        None
    };
    let p = payload(path)?;
    let mut q = query.clone();
    q.filename = p.filename.clone();
    if let Some((link, key)) = &keyed {
        return push_keyed(ctx, client, path, link, key, p, q, force).await;
    }
    let server = client.origin();
    let config_dir = &ctx.settings.config_dir;
    let remembered = if force_new || explicit_id.is_some() {
        None
    } else {
        remember::load(config_dir, path)
            .filter(|m| m.server == server)
            .map(|m| m.id)
    };
    let explicit = match explicit_id {
        Some(s) => Some(docref::resolve(client, s).await?),
        None => None,
    };
    let target = explicit.clone().or(remembered.clone());
    let resp = match target {
        Some(id) => match client
            .push(Some(&id), p.body.clone(), p.content_type, &q, None)
            .await
        {
            Ok(r) => r,
            Err(e) if exit::is_not_found(&e) && explicit.is_none() => {
                output::warn(format!(
                    "the remembered document {id} is gone; pushing a new document"
                ));
                client.push(None, p.body, p.content_type, &q, None).await?
            }
            Err(e) => return Err(e),
        },
        None => client.push(None, p.body, p.content_type, &q, None).await?,
    };
    let memo = Memo {
        id: resp.id.clone(),
        server,
    };
    if let Err(e) = remember::save(config_dir, path, &memo) {
        output::warn(format!("could not remember the document id: {e:#}"));
    }
    Ok(resp)
}

pub fn print_push(ctx: &Ctx, r: &PushResponse) {
    for b in &r.broken_links {
        output::warn(format!("broken link in {}: {}", b.from, b.href));
    }
    for w in &r.warnings {
        output::warn(w);
    }
    if ctx.json {
        output::json(r);
        return;
    }
    let files = if r.files == 1 {
        "1 file".to_string()
    } else {
        format!("{} files", r.files)
    };
    println!(
        "Pushed \"{}\" · r{} · {} · {}",
        r.title, r.rev, r.kind, files
    );
    if let Some(u) = &r.path_url {
        println!("  path  {u}");
    }
    println!("  url   {}", r.url);
    println!("  ws    {}", r.ws_url);
    println!("  wait  semora wait {}", r.id);
}

pub async fn run(ctx: &Ctx, args: PushArgs) -> anyhow::Result<i32> {
    let client = ctx.client()?;
    note_outside_project(&args.path, args.id.as_deref(), args.new);
    let query = PushQuery {
        title: args.title.clone(),
        tags: tags(&args.tags, &ctx.settings.default_tags),
        message: args.message.clone(),
        entry: args.entry.clone(),
        meta: parse_meta(&args.meta)?,
        ..Default::default()
    };
    let resp = push_path(
        ctx,
        &client,
        &args.path,
        args.id.as_deref(),
        args.new,
        args.force,
        &query,
    )
    .await
    .map_err(|e| hint_outside_project(e, "--id or --new"))?;
    if args.wait {
        if !ctx.json {
            print_push(ctx, &resp);
        } else {
            // With --wait --json, stdout carries only the events.
            for b in &resp.broken_links {
                output::warn(format!("broken link in {}: {}", b.from, b.href));
            }
        }
        return crate::commands::wait::wait_for(ctx, &client, &resp.id, &args.wait_opts).await;
    }
    print_push(ctx, &resp);
    Ok(exit::OK)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn meta_values_keep_scalar_types() {
        let m = parse_meta(&[
            "a=1".into(),
            "b=true".into(),
            "c=hello world".into(),
            "d=x=y".into(),
        ])
        .unwrap()
        .unwrap();
        let v: Value = serde_json::from_str(&m).unwrap();
        assert_eq!(
            v,
            serde_json::json!({"a": 1, "b": true, "c": "hello world", "d": "x=y"})
        );
        assert!(parse_meta(&["novalue".into()]).is_err());
        assert_eq!(parse_meta(&[]).unwrap(), None);
    }

    #[test]
    fn an_old_server_without_projects_is_reported() {
        let mut r = PushResponse {
            id: "k3F9xQ2mLp".into(),
            rev: 1,
            kind: semora_proto::DocKind::Md,
            url: "u".into(),
            ws_url: "w".into(),
            files: 1,
            title: "T".into(),
            broken_links: vec![],
            warnings: vec![],
            project: None,
            path: None,
            path_url: None,
        };
        let err = check_filed(&r, "semora").unwrap_err().to_string();
        assert!(
            err.contains("older than this CLI") && err.contains("semora"),
            "{err}"
        );
        r.project = Some("semora".into());
        assert!(check_filed(&r, "semora").is_ok());
    }

    #[test]
    fn tags_merge_flags_and_defaults() {
        assert_eq!(
            tags(&["a,b".into(), "c".into()], &["proj".into()]).as_deref(),
            Some("a,b,c,proj")
        );
        assert_eq!(tags(&[], &[]), None);
    }
}