dsc-rs 0.10.15

Discourse CLI tool for managing multiple Discourse forums: track installs, run upgrades over SSH, manage emojis, sync topics and categories as Markdown, and more.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
use crate::api::{DiscourseClient, SiteSettingDetail};
use crate::cli::ListFormat;
use crate::commands::common::{ensure_api_credentials, parse_tags, select_discourse};
use crate::config::{Config, DiscourseConfig};
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

/// Set a site setting. If `discourse_name` is given, only that discourse is updated.
/// Otherwise all discourses matching `tags` are updated.
pub fn set_site_setting(
    config: &Config,
    discourse_name: Option<&str>,
    setting: &str,
    value: &str,
    tags: Option<&str>,
    dry_run: bool,
) -> Result<()> {
    if let Some(name) = discourse_name {
        let discourse = select_discourse(config, Some(name))?;
        ensure_api_credentials(discourse)?;
        if dry_run {
            println!(
                "[dry-run] {}: would set {} = {}",
                discourse.name, setting, value
            );
            return Ok(());
        }
        let client = DiscourseClient::new(discourse)?;
        client.update_site_setting(setting, value)?;
        println!("{}: updated {}", discourse.name, setting);
        return Ok(());
    }

    // No specific discourse - use tag filter across all discourses.
    let filter = tags.map(parse_tags).unwrap_or_default();
    let matches_filter = |disc: &DiscourseConfig| {
        if filter.is_empty() {
            return true;
        }
        let disc_tags = disc.tags.as_ref().map(|t| {
            t.iter()
                .map(|tag| tag.to_ascii_lowercase())
                .collect::<Vec<_>>()
        });
        let Some(disc_tags) = disc_tags else {
            return false;
        };
        filter.iter().any(|tag| {
            let tag = tag.to_ascii_lowercase();
            disc_tags.iter().any(|t| t == &tag)
        })
    };

    let mut matched = 0;
    for discourse in config.discourse.iter().filter(|d| matches_filter(d)) {
        matched += 1;
        ensure_api_credentials(discourse)?;
        if dry_run {
            println!(
                "[dry-run] {}: would set {} = {}",
                discourse.name, setting, value
            );
            continue;
        }
        let client = DiscourseClient::new(discourse)?;
        client.update_site_setting(setting, value)?;
        println!("{}: updated {}", discourse.name, setting);
    }

    if matched == 0 {
        return Err(anyhow!("no discourses matched the tag filter"));
    }

    Ok(())
}

/// Get the current value of a single site setting.
pub fn get_site_setting(config: &Config, discourse_name: &str, setting: &str) -> Result<()> {
    let discourse = select_discourse(config, Some(discourse_name))?;
    ensure_api_credentials(discourse)?;
    let client = DiscourseClient::new(discourse)?;
    let value = client.fetch_site_setting(setting)?;
    println!("{}", value);
    Ok(())
}

#[derive(Debug, Serialize)]
struct SettingEntry {
    setting: String,
    value: String,
    category: String,
}

/// List all site settings.
pub fn list_site_settings(
    config: &Config,
    discourse_name: &str,
    format: ListFormat,
    verbose: bool,
) -> Result<()> {
    let discourse = select_discourse(config, Some(discourse_name))?;
    ensure_api_credentials(discourse)?;
    let client = DiscourseClient::new(discourse)?;
    let raw = client.list_site_settings()?;

    let settings_arr = raw
        .get("site_settings")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();

    let entries: Vec<SettingEntry> = settings_arr
        .into_iter()
        .map(|entry| {
            let setting = entry
                .get("setting")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let value = match entry
                .get("value")
                .cloned()
                .unwrap_or(serde_json::Value::Null)
            {
                serde_json::Value::String(s) => s,
                serde_json::Value::Null => String::new(),
                other => other.to_string(),
            };
            let category = entry
                .get("category")
                .and_then(|v| v.as_str())
                .unwrap_or("uncategorized")
                .to_string();
            SettingEntry {
                setting,
                value,
                category,
            }
        })
        .collect();

    match format {
        ListFormat::Text => {
            if entries.is_empty() && !verbose {
                println!("No settings found.");
                return Ok(());
            }
            for e in &entries {
                println!("{} = {}", e.setting, e.value);
            }
        }
        ListFormat::Json => {
            println!("{}", serde_json::to_string_pretty(&entries)?);
        }
        ListFormat::Yaml => {
            print!("{}", serde_yaml::to_string(&entries)?);
        }
    }

    Ok(())
}

// ─── Pull (snapshot) ──────────────────────────────────────────────────────────

/// On-disk settings snapshot file (schema version 1).
///
/// Spec: `spec/setting-sync.md`. The file is self-documenting: it carries
/// each setting's default, type, category, and description so that a human
/// (or LLM) reading the file can understand each entry without consulting
/// the API. On `push`, only `name` and `value` are honoured.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SettingsFile {
    pub version: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub discourse_version: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pulled_at: Option<String>,
    #[serde(default)]
    pub settings: Vec<SettingsEntry>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SettingsEntry {
    pub name: String,
    pub value: serde_json::Value,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default: Option<serde_json::Value>,
    /// Renamed to avoid the Rust keyword. Serializes as `type`.
    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
    pub setting_type: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// Settings excluded from `pull` because they are computed/read-only on the
/// server. Keep the list small; unknown read-only settings are handled
/// gracefully by `push` (server returns 422, we warn and continue).
const READONLY_SETTINGS: &[&str] = &[];

/// Pull all site settings to a local file.
pub fn pull_settings(
    config: &Config,
    discourse_name: &str,
    local_path: &Path,
    changed_only: bool,
    category: Option<&str>,
) -> Result<()> {
    let discourse = select_discourse(config, Some(discourse_name))?;
    ensure_api_credentials(discourse)?;
    let client = DiscourseClient::new(discourse)?;

    let server = client.list_site_settings_detailed()?;
    let discourse_version = client.fetch_version().ok().flatten();

    let mut entries: Vec<SettingsEntry> = server
        .into_iter()
        .filter(|s| !READONLY_SETTINGS.contains(&s.setting.as_str()))
        .filter(|s| match category {
            Some(cat) => s.category.eq_ignore_ascii_case(cat),
            None => true,
        })
        .filter(|s| {
            if !changed_only {
                return true;
            }
            !values_equal(&s.value, &s.default)
        })
        .map(detail_to_entry)
        .collect();

    // Sort by category, then by name for stable diffs.
    entries.sort_by(|a, b| {
        let ca = a.category.as_deref().unwrap_or("");
        let cb = b.category.as_deref().unwrap_or("");
        ca.cmp(cb).then_with(|| a.name.cmp(&b.name))
    });

    let pulled_at = chrono::Utc::now()
        .format("%Y-%m-%dT%H:%M:%SZ")
        .to_string();

    let file = SettingsFile {
        version: 1,
        discourse_version,
        pulled_at: Some(pulled_at),
        settings: entries,
    };

    let content = if is_json_path(local_path) {
        serde_json::to_string_pretty(&file).context("serializing settings as JSON")?
    } else {
        serde_yaml::to_string(&file).context("serializing settings as YAML")?
    };

    fs::write(local_path, &content)
        .with_context(|| format!("writing {}", local_path.display()))?;

    println!(
        "Wrote {} setting{} to {}",
        file.settings.len(),
        if file.settings.len() == 1 { "" } else { "s" },
        local_path.display()
    );
    Ok(())
}

fn detail_to_entry(d: SiteSettingDetail) -> SettingsEntry {
    SettingsEntry {
        name: d.setting,
        value: d.value,
        default: if d.default.is_null() {
            None
        } else {
            Some(d.default)
        },
        setting_type: empty_to_none(d.setting_type),
        category: empty_to_none(d.category),
        description: empty_to_none(d.description),
    }
}

fn empty_to_none(s: String) -> Option<String> {
    if s.is_empty() { None } else { Some(s) }
}

fn values_equal(a: &serde_json::Value, b: &serde_json::Value) -> bool {
    // Discourse returns numeric and boolean settings as JSON values; comparing
    // the parsed Value handles all simple types. For string-typed values that
    // happen to differ only in whitespace, leave the strict comparison; users
    // who hit edge cases can edit the snapshot directly.
    a == b
}

fn is_json_path(p: &Path) -> bool {
    p.extension()
        .and_then(|e| e.to_str())
        .map(|e| e.eq_ignore_ascii_case("json"))
        .unwrap_or(false)
}

// ─── Push (apply) ─────────────────────────────────────────────────────────────

/// Apply a settings snapshot file to a Discourse.
///
/// Idempotent: only PUTs values that differ from the server. Settings present
/// in the file but unknown on the server are skipped with a warning. With
/// `--reset-unlisted`, settings present on the server but absent from the
/// file are reset to their `default` value.
pub fn push_settings(
    config: &Config,
    discourse_name: &str,
    local_path: &Path,
    reset_unlisted: bool,
    dry_run: bool,
) -> Result<()> {
    let discourse = select_discourse(config, Some(discourse_name))?;
    ensure_api_credentials(discourse)?;
    let client = DiscourseClient::new(discourse)?;

    let raw = fs::read_to_string(local_path)
        .with_context(|| format!("reading {}", local_path.display()))?;
    let file: SettingsFile = if is_json_path(local_path) {
        serde_json::from_str(&raw).context("parsing settings file as JSON")?
    } else {
        serde_yaml::from_str(&raw).context("parsing settings file as YAML")?
    };

    if file.version != 1 {
        return Err(anyhow!(
            "unsupported settings file schema version {} (expected 1)",
            file.version
        ));
    }

    let server = client.list_site_settings_detailed()?;
    let server_by_name: std::collections::HashMap<&str, &SiteSettingDetail> = server
        .iter()
        .map(|s| (s.setting.as_str(), s))
        .collect();

    let mut plan: Vec<PushAction> = Vec::new();

    // File → server: change or unchanged.
    for entry in &file.settings {
        let Some(srv) = server_by_name.get(entry.name.as_str()) else {
            plan.push(PushAction::UnknownOnServer(entry.name.clone()));
            continue;
        };
        let desired = value_to_send_string(&entry.value);
        let current = value_to_send_string(&srv.value);
        if desired == current {
            plan.push(PushAction::Unchanged(entry.name.clone()));
        } else {
            plan.push(PushAction::Change {
                name: entry.name.clone(),
                from: current,
                to: desired,
            });
        }
    }

    // Server → file: reset_unlisted.
    if reset_unlisted {
        let in_file: std::collections::HashSet<&str> =
            file.settings.iter().map(|e| e.name.as_str()).collect();
        for srv in &server {
            if in_file.contains(srv.setting.as_str()) {
                continue;
            }
            if READONLY_SETTINGS.contains(&srv.setting.as_str()) {
                continue;
            }
            let current = value_to_send_string(&srv.value);
            let default = value_to_send_string(&srv.default);
            if current == default {
                continue;
            }
            plan.push(PushAction::Reset {
                name: srv.setting.clone(),
                from: current,
                to: default,
            });
        }
    }

    // Stable order for display.
    plan.sort_by(|a, b| a.name().cmp(b.name()));

    print_plan(&plan, &discourse.name, dry_run);

    if dry_run {
        return Ok(());
    }

    // Apply.
    let mut applied = 0;
    let mut failed = 0;
    for action in &plan {
        match action {
            PushAction::Change { name, to, .. } | PushAction::Reset { name, to, .. } => {
                match client.update_site_setting(name, to) {
                    Ok(()) => {
                        applied += 1;
                    }
                    Err(err) => {
                        failed += 1;
                        eprintln!("  ! {}: failed: {}", name, err);
                    }
                }
            }
            PushAction::Unchanged(_) | PushAction::UnknownOnServer(_) => {}
        }
    }

    println!(
        "{}: applied {} setting{}{}",
        discourse.name,
        applied,
        if applied == 1 { "" } else { "s" },
        if failed > 0 {
            format!(", {} failed", failed)
        } else {
            String::new()
        }
    );
    if failed > 0 {
        return Err(anyhow!("{} setting(s) failed to apply", failed));
    }
    Ok(())
}

#[derive(Debug)]
enum PushAction {
    Change {
        name: String,
        from: String,
        to: String,
    },
    Reset {
        name: String,
        from: String,
        to: String,
    },
    Unchanged(String),
    UnknownOnServer(String),
}

impl PushAction {
    fn name(&self) -> &str {
        match self {
            PushAction::Change { name, .. }
            | PushAction::Reset { name, .. }
            | PushAction::Unchanged(name)
            | PushAction::UnknownOnServer(name) => name,
        }
    }
}

fn print_plan(plan: &[PushAction], discourse: &str, dry_run: bool) {
    let prefix = if dry_run { "[dry-run] " } else { "" };
    let changes = plan
        .iter()
        .filter(|a| matches!(a, PushAction::Change { .. } | PushAction::Reset { .. }))
        .count();
    let unchanged = plan
        .iter()
        .filter(|a| matches!(a, PushAction::Unchanged(_)))
        .count();
    let unknown = plan
        .iter()
        .filter(|a| matches!(a, PushAction::UnknownOnServer(_)))
        .count();

    println!(
        "{}Setting push plan for {}: {} change{}, {} unchanged, {} unknown",
        prefix,
        discourse,
        changes,
        if changes == 1 { "" } else { "s" },
        unchanged,
        unknown,
    );
    for action in plan {
        match action {
            PushAction::Change { name, from, to } => {
                println!("  ~ {}: {} → {}", name, quote(from), quote(to));
            }
            PushAction::Reset { name, from, to } => {
                println!(
                    "  - {}: {} → {} (reset to default)",
                    name,
                    quote(from),
                    quote(to)
                );
            }
            PushAction::Unchanged(name) => {
                println!("  = {}: (unchanged)", name);
            }
            PushAction::UnknownOnServer(name) => {
                println!("  ? {}: skipped (not found on server)", name);
            }
        }
    }
}

fn quote(s: &str) -> String {
    if s.is_empty() {
        "\"\"".to_string()
    } else {
        format!("\"{}\"", s)
    }
}

/// Convert a `serde_json::Value` to the string form expected by Discourse's
/// `PUT /admin/site_settings/{name}.json` endpoint. Discourse accepts strings
/// and coerces internally.
fn value_to_send_string(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::Null => String::new(),
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Number(n) => n.to_string(),
        // Discourse list-type settings are pipe-separated strings on the wire.
        // If a user wrote a YAML/JSON array, join with "|" for compatibility.
        serde_json::Value::Array(arr) => arr
            .iter()
            .map(value_to_send_string)
            .collect::<Vec<_>>()
            .join("|"),
        serde_json::Value::Object(_) => v.to_string(),
    }
}

// ─── Diff (compare two sources) ───────────────────────────────────────────────

/// A canonical, source-agnostic snapshot of settings used by `diff_settings`.
struct DiffSource {
    label: String,
    entries: std::collections::HashMap<String, SettingsEntry>,
}

/// Compare site settings between two sources. Each source can be a Discourse
/// name (live fetch) or a path to a snapshot file produced by `pull`.
pub fn diff_settings(
    config: &Config,
    source: &str,
    target: &str,
    changed_only: bool,
    category: Option<&str>,
    format: ListFormat,
) -> Result<()> {
    let a = load_diff_source(config, source)?;
    let b = load_diff_source(config, target)?;

    // Union of keys.
    let mut names: std::collections::BTreeSet<String> = a.entries.keys().cloned().collect();
    names.extend(b.entries.keys().cloned());

    let mut rows: Vec<DiffRow> = Vec::new();
    for name in names {
        let ea = a.entries.get(&name);
        let eb = b.entries.get(&name);
        let va = ea.map(|e| value_to_send_string(&e.value));
        let vb = eb.map(|e| value_to_send_string(&e.value));
        if va == vb {
            continue;
        }
        // Category filter (uses whichever side has metadata).
        if let Some(cat) = category {
            let row_cat = ea
                .and_then(|e| e.category.as_deref())
                .or_else(|| eb.and_then(|e| e.category.as_deref()))
                .unwrap_or("");
            if !row_cat.eq_ignore_ascii_case(cat) {
                continue;
            }
        }
        // changed-only: filter to rows where at least one side has a value
        // that differs from its default. Treat an absent setting as "at
        // default" - this avoids drowning the diff in entries that one side
        // simply omitted because the snapshot was --changed-only.
        if changed_only {
            // Borrow the default from whichever side has metadata.
            let shared_default = ea
                .and_then(|e| e.default.as_ref())
                .or_else(|| eb.and_then(|e| e.default.as_ref()));
            let a_changed = match ea {
                Some(e) => shared_default.map(|d| &e.value != d).unwrap_or(true),
                None => false,
            };
            let b_changed = match eb {
                Some(e) => shared_default.map(|d| &e.value != d).unwrap_or(true),
                None => false,
            };
            if !a_changed && !b_changed {
                continue;
            }
        }
        rows.push(DiffRow {
            name,
            value_a: va,
            value_b: vb,
        });
    }

    print_diff(&rows, &a.label, &b.label, format)
}

#[derive(Debug, Serialize)]
struct DiffRow {
    name: String,
    #[serde(rename = "a")]
    value_a: Option<String>,
    #[serde(rename = "b")]
    value_b: Option<String>,
}

/// Resolve a source string to a canonical settings snapshot. Treats the
/// argument as a file path if it points to an existing file or has a
/// `.yaml`/`.yml`/`.json` extension; otherwise treats it as a Discourse name.
fn load_diff_source(config: &Config, src: &str) -> Result<DiffSource> {
    let path = Path::new(src);
    let looks_like_file = path.is_file()
        || matches!(
            path.extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase),
            Some(ref ext) if ext == "yaml" || ext == "yml" || ext == "json"
        );
    if looks_like_file {
        let raw = fs::read_to_string(path)
            .with_context(|| format!("reading {}", path.display()))?;
        let file: SettingsFile = if is_json_path(path) {
            serde_json::from_str(&raw).context("parsing settings file as JSON")?
        } else {
            serde_yaml::from_str(&raw).context("parsing settings file as YAML")?
        };
        let entries: std::collections::HashMap<String, SettingsEntry> = file
            .settings
            .into_iter()
            .map(|e| (e.name.clone(), e))
            .collect();
        return Ok(DiffSource {
            label: path.display().to_string(),
            entries,
        });
    }
    // Treat as discourse name.
    let discourse = select_discourse(config, Some(src))?;
    ensure_api_credentials(discourse)?;
    let client = DiscourseClient::new(discourse)?;
    let server = client.list_site_settings_detailed()?;
    let entries: std::collections::HashMap<String, SettingsEntry> = server
        .into_iter()
        .map(|d| {
            let entry = detail_to_entry(d);
            (entry.name.clone(), entry)
        })
        .collect();
    Ok(DiffSource {
        label: discourse.name.clone(),
        entries,
    })
}

fn print_diff(rows: &[DiffRow], label_a: &str, label_b: &str, format: ListFormat) -> Result<()> {
    match format {
        ListFormat::Text => {
            if rows.is_empty() {
                println!("{} and {}: no differences.", label_a, label_b);
                return Ok(());
            }
            println!(
                "{} differing setting{} between {} and {}:",
                rows.len(),
                if rows.len() == 1 { "" } else { "s" },
                label_a,
                label_b
            );
            for row in rows {
                println!("  {}", row.name);
                println!("    {}: {}", label_a, fmt_diff_value(&row.value_a));
                println!("    {}: {}", label_b, fmt_diff_value(&row.value_b));
            }
        }
        ListFormat::Json => {
            let payload = serde_json::json!({
                "a": label_a,
                "b": label_b,
                "differences": rows,
            });
            println!("{}", serde_json::to_string_pretty(&payload)?);
        }
        ListFormat::Yaml => {
            let payload = serde_json::json!({
                "a": label_a,
                "b": label_b,
                "differences": rows,
            });
            print!("{}", serde_yaml::to_string(&payload)?);
        }
    }
    Ok(())
}

fn fmt_diff_value(v: &Option<String>) -> String {
    match v {
        Some(s) if s.is_empty() => "\"\"".to_string(),
        Some(s) => format!("\"{}\"", s),
        None => "(absent)".to_string(),
    }
}