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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "dsc")]
#[command(about = "Discourse CLI", long_about = None)]
pub struct Cli {
    /// Path to the config file. If omitted, `dsc` consults `$DSC_CONFIG`,
    /// then searches `./dsc.toml`, `$DSC_CONFIG_HOME/dsc.toml`
    /// (default `~/.config/dsc/dsc.toml`), then system locations.
    /// Errors if the given file does not exist (no silent fallthrough).
    /// See `dsc config` for the active selection.
    #[arg(long, short = 'c')]
    pub config: Option<PathBuf>,
    /// Describe destructive actions without sending them. Read-only commands
    /// ignore the flag.
    #[arg(long, short = 'n', global = true)]
    pub dry_run: bool,
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
    /// List configured Discourses.
    #[command(visible_alias = "ls")]
    List {
        /// Output format for the listing.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: OutputFormat,
        /// Filter by tags (comma/semicolon separated, match-any).
        #[arg(long, value_name = "tag1,tag2")]
        tags: Option<String>,
        /// Open each listed Discourse base URL in a browser tab/window.
        #[arg(long, short = 'o')]
        open: bool,
        /// Include empty results and verbose listing details where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
        #[command(subcommand)]
        command: Option<ListCommand>,
    },
    /// Add one or more Discourses to the config.
    #[command(visible_alias = "a")]
    Add {
        /// Comma-separated discourse names to add.
        names: String,
        /// Prompt for additional optional fields while adding.
        #[arg(long, short = 'i')]
        interactive: bool,
    },
    /// Import Discourses from a file or stdin.
    #[command(visible_alias = "imp")]
    Import {
        /// Path to import input (text/CSV). Reads stdin when omitted.
        path: Option<PathBuf>,
    },
    /// Run remote OS + Discourse update workflow for one or all Discourses.
    #[command(visible_alias = "up")]
    Update {
        /// Discourse name, or 'all' to update every configured Discourse.
        name: String,
        /// Parallel update mode for `dsc update all`.
        #[arg(long, short = 'p')]
        parallel: bool,
        /// Maximum workers when parallel mode is enabled (default: 3).
        #[arg(long, short = 'm')]
        max: Option<usize>,
        /// Disable changelog posting (posting prompt is on by default).
        #[arg(long = "no-changelog", action = ArgAction::SetFalse, default_value_t = true)]
        post_changelog: bool,
        /// Auto-confirm changelog posting prompt (non-interactive mode).
        #[arg(long, short = 'y')]
        yes: bool,
    },
    /// Manage custom emoji.
    #[command(visible_alias = "em")]
    Emoji {
        #[command(subcommand)]
        command: EmojiCommand,
    },
    /// Pull/push/sync topics as local Markdown.
    #[command(visible_alias = "t")]
    Topic {
        #[command(subcommand)]
        command: TopicCommand,
    },
    /// List/copy/pull/push categories.
    #[command(visible_alias = "cat")]
    Category {
        #[command(subcommand)]
        command: CategoryCommand,
    },
    /// List/inspect/copy groups.
    #[command(visible_alias = "grp")]
    Group {
        #[command(subcommand)]
        command: GroupCommand,
    },
    /// Operations that act from a user's perspective.
    #[command(visible_alias = "usr")]
    User {
        #[command(subcommand)]
        command: UserCommand,
    },
    /// Send invites — single or bulk from a file.
    #[command(visible_alias = "inv")]
    Invite {
        #[command(subcommand)]
        command: InviteCommand,
    },
    /// Manage API keys (admin scope).
    #[command(visible_alias = "ak")]
    ApiKey {
        #[command(subcommand)]
        command: ApiKeyCommand,
    },
    /// Send and list private messages.
    #[command(visible_alias = "msg")]
    Pm {
        #[command(subcommand)]
        command: PmCommand,
    },
    /// Create/list/restore backups.
    #[command(visible_alias = "bk")]
    Backup {
        #[command(subcommand)]
        command: BackupCommand,
    },
    /// List/pull/push color palettes.
    #[command(visible_alias = "pal")]
    Palette {
        #[command(subcommand)]
        command: PaletteCommand,
    },
    /// List/install/remove plugins.
    #[command(visible_alias = "plg")]
    Plugin {
        #[command(subcommand)]
        command: PluginCommand,
    },
    /// List/install/remove/pull/push/duplicate themes.
    #[command(visible_alias = "th")]
    Theme {
        #[command(subcommand)]
        command: ThemeCommand,
    },
    /// Update site settings.
    #[command(visible_alias = "set")]
    Setting {
        #[command(subcommand)]
        command: SettingCommand,
    },
    /// Manage the tag taxonomy: list/pull/push tags and tag groups.
    #[command(visible_alias = "tg")]
    Tag {
        #[command(subcommand)]
        command: TagCommand,
    },
    /// Post-level operations: edit / delete / move.
    #[command(visible_alias = "po")]
    Post {
        #[command(subcommand)]
        command: PostCommand,
    },
    /// Open a Discourse in the default browser.
    #[command(visible_alias = "o")]
    Open {
        /// Discourse name.
        discourse: String,
    },
    /// Harden a fresh Ubuntu server reachable via `ssh root@host`.
    ///
    /// **Stage 1 (current):** creates a non-root sudo user, installs the
    /// given pubkey to their authorized_keys, and verifies the new-user
    /// SSH login works. Does NOT yet tighten sshd_config, install Docker
    /// / fail2ban / etc — those come in follow-up releases.
    ///
    /// Defaults can be overridden in the `[harden]` block of dsc.toml;
    /// the flags below override that block on a per-run basis.
    #[command(visible_alias = "hd")]
    Harden {
        /// Target hostname or IP (reachable via SSH).
        host: String,
        /// Username to SSH in as initially. Defaults to `root`, which is
        /// what a fresh cloud-provisioned box typically has.
        #[arg(long, default_value = "root")]
        ssh_user: String,
        /// Username for the new sudo-enabled non-root account. Overrides
        /// `[harden].new_user` from dsc.toml. Built-in default: `discourse`.
        #[arg(long)]
        new_user: Option<String>,
        /// SSH port to move the daemon to in stage 2. Overrides
        /// `[harden].ssh_port`. Built-in default: 2227. Parsed now so the
        /// CLI is stable; not yet applied in stage 1.
        #[arg(long)]
        ssh_port: Option<u16>,
        /// Path to an SSH public key file whose contents will be added to
        /// the new user's authorized_keys. A typical value is
        /// `~/.ssh/<hostname>.pub` — the per-server keypair pattern in
        /// the Bawmedical hardening playbook.
        #[arg(long)]
        pubkey_file: PathBuf,
    },
    /// Community-health analytics — growth, activity, and health metrics
    /// for a Discourse, with optional period-over-period comparison.
    ///
    /// See `spec/analytics.md` for the full spec. v1 ships every metric
    /// that maps onto a single `/admin/reports/{id}.json` endpoint;
    /// derivation-heavy ones (e.g. lost regulars, top-10 share) print
    /// `— (n/i)` until follow-up implementation lands.
    #[command(visible_alias = "stats")]
    Analytics {
        /// Discourse name.
        discourse: String,
        /// Window to report on. Same syntax as `dsc user activity --since`
        /// (e.g. `7d`, `24h`, `1m`, ISO-8601). Ignored when `--snapshot`
        /// is set. Default: 30d.
        #[arg(long, short = 's', default_value = "30d")]
        since: String,
        /// Also fetch the immediately preceding window of equal length and
        /// show a delta column. Mutually exclusive with `--snapshot`.
        #[arg(long, short = 'c', conflicts_with = "snapshot")]
        compare: bool,
        /// Multi-window snapshot mode. Reports each metric across several
        /// preset windows (`--periods`) so you see growth/health trends
        /// at a glance. Replaces `--since` + `--compare`.
        #[arg(long)]
        snapshot: bool,
        /// Comma-separated periods for `--snapshot`. Default: `24h,7d,30d,1y`.
        #[arg(long, requires = "snapshot")]
        periods: Option<String>,
        /// Restrict output to one section.
        #[arg(long, value_enum, default_value = "all")]
        section: SectionArg,
        /// Output format. `table` is DuckDB-style box-drawing; falls
        /// through to `text` automatically when stdout isn't a TTY.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: AnalyticsFormat,
    },
    /// Search topics on a Discourse.
    #[command(visible_alias = "s")]
    Search {
        /// Discourse name.
        discourse: String,
        /// Search query (passed through verbatim, including any
        /// Discourse filter syntax like `category:foo` or `@user`).
        query: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Upload a file. Prints the resulting upload:// short URL by default.
    #[command(visible_alias = "u")]
    Upload {
        /// Discourse name.
        discourse: String,
        /// Path to the file to upload.
        file: PathBuf,
        /// Discourse upload context. Default `composer` is correct for
        /// embedding in posts; other values include `avatar`,
        /// `profile_background`, `card_background`, `custom_emoji`.
        #[arg(long, short = 't', default_value = "composer")]
        upload_type: String,
        /// Output format. Text mode prints just the short URL.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Inspect and validate configuration.
    #[command(visible_alias = "cfg")]
    Config {
        #[command(subcommand)]
        command: Option<ConfigCommand>,
    },
    /// Generate shell completion scripts.
    #[command(visible_alias = "comp")]
    Completions {
        /// Target shell.
        #[arg(value_enum)]
        shell: CompletionShell,
        /// Output directory. Prints to stdout when omitted.
        #[arg(long, short = 'd')]
        dir: Option<PathBuf>,
    },
    /// Generate man pages for `dsc` and every subcommand.
    ///
    /// Writes one ROFF-formatted file per (sub)command (e.g. `dsc.1`,
    /// `dsc-tag-pull.1`) into the given directory. Distro packagers
    /// install these into section 1 of the man path. Run `gzip -9` on
    /// the output if your packaging convention expects compressed pages.
    #[command(visible_alias = "manpages")]
    Man {
        /// Output directory. Required - this command always writes to disk.
        #[arg(long, short = 'd')]
        dir: PathBuf,
    },
    /// Print the dsc version.
    #[command(visible_alias = "ver")]
    Version,
}

#[derive(Subcommand)]
pub enum ConfigCommand {
    /// Probe each configured Discourse: API auth and (optionally) SSH reachability.
    #[command(visible_alias = "ck")]
    Check {
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Skip the SSH reachability probe.
        #[arg(long)]
        skip_ssh: bool,
    },
}

#[derive(Subcommand)]
pub enum ListCommand {
    /// Sort discourse entries by name and rewrite config in-place.
    /// Also inserts placeholder values for unset template keys.
    #[command(visible_alias = "ty")]
    Tidy,
}

#[derive(Subcommand)]
pub enum EmojiCommand {
    /// Pull all custom emoji from a Discourse into a local directory.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Local directory to save emoji images into.
        output_dir: PathBuf,
    },
    /// Push (upload) one emoji file, or bulk-upload from a directory (alias: add).
    #[command(visible_alias = "ps", alias = "add")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Local file or directory path.
        emoji_path: PathBuf,
        /// Optional emoji name (file uploads only).
        emoji_name: Option<String>,
    },

    /// List custom emojis on a Discourse.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Include additional fields where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
        /// Render inline images when terminal protocol support is available.
        #[arg(long, short = 'i')]
        inline: bool,
    },
}

#[derive(Subcommand)]
pub enum TopicCommand {
    /// Pull a topic to a local Markdown file.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Topic ID.
        topic_id: u64,
        /// Destination file or directory (auto-derived when omitted).
        local_path: Option<PathBuf>,
        /// Pull the entire thread (every post) as a single Markdown file
        /// with YAML frontmatter and per-post headings. Default behaviour
        /// (no `--full`) writes only the OP, which is what `topic push`
        /// expects.
        #[arg(long, short = 'F')]
        full: bool,
    },
    /// Push a local Markdown file to a topic.
    #[command(visible_alias = "ps")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Topic ID.
        topic_id: u64,
        /// Local Markdown file path.
        local_path: PathBuf,
    },
    /// Sync a topic and local Markdown file using newest timestamp.
    #[command(visible_alias = "sy")]
    Sync {
        /// Discourse name.
        discourse: String,
        /// Topic ID.
        topic_id: u64,
        /// Local Markdown file path.
        local_path: PathBuf,
        /// Skip sync confirmation prompt.
        #[arg(long, short = 'y')]
        yes: bool,
    },
    /// Reply to a topic with content from a file or stdin.
    #[command(visible_alias = "r")]
    Reply {
        /// Discourse name.
        discourse: String,
        /// Topic ID.
        topic_id: u64,
        /// Input file path. Reads stdin when omitted or `-`.
        local_path: Option<PathBuf>,
    },
    /// Create a new topic in a category, body from a file or stdin.
    #[command(visible_alias = "n")]
    New {
        /// Discourse name.
        discourse: String,
        /// Target category ID.
        category_id: u64,
        /// Topic title.
        #[arg(long, short = 't')]
        title: String,
        /// Input file path. Reads stdin when omitted or `-`.
        local_path: Option<PathBuf>,
    },
    /// Add a tag to a topic.
    Tag {
        /// Discourse name.
        discourse: String,
        /// Topic ID.
        topic_id: u64,
        /// Tag to add.
        tag: String,
    },
    /// Remove a tag from a topic.
    Untag {
        /// Discourse name.
        discourse: String,
        /// Topic ID.
        topic_id: u64,
        /// Tag to remove.
        tag: String,
    },
}

#[derive(Subcommand)]
pub enum CategoryCommand {
    /// List categories.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Include additional fields where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
        /// Show category hierarchy tree.
        #[arg(long)]
        tree: bool,
    },
    /// Copy a category to another Discourse.
    #[command(visible_alias = "cp")]
    Copy {
        /// Source discourse name.
        discourse: String,
        /// Target discourse name (defaults to source when omitted).
        #[arg(long, short = 't')]
        target: Option<String>,
        /// Category ID or slug.
        category: String,
    },
    /// Pull all topics from a category into local Markdown files.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Category ID or slug.
        category: String,
        /// Destination directory (auto-derived when omitted).
        local_path: Option<PathBuf>,
    },
    /// Push local Markdown files into a category.
    #[command(visible_alias = "ps")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Category ID or slug.
        category: String,
        /// Local directory containing Markdown files.
        local_path: PathBuf,
    },
}

#[derive(Subcommand)]
pub enum GroupCommand {
    /// List groups.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Include additional fields where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
    },
    /// Show group details.
    #[command(visible_alias = "i")]
    Info {
        /// Discourse name.
        discourse: String,
        /// Group ID.
        group: u64,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "json")]
        format: StructuredFormat,
    },
    /// List members of a group.
    #[command(visible_alias = "m")]
    Members {
        /// Discourse name.
        discourse: String,
        /// Group ID.
        group: u64,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Copy a group to another Discourse.
    #[command(visible_alias = "cp")]
    Copy {
        /// Source discourse name.
        discourse: String,
        /// Target discourse name (defaults to source when omitted).
        #[arg(long, short = 't')]
        target: Option<String>,
        /// Group ID.
        group: u64,
    },
    /// Bulk add members to a group from a file (or stdin) of email addresses.
    #[command(visible_alias = "a")]
    Add {
        /// Discourse name.
        discourse: String,
        /// Group ID.
        group: u64,
        /// Path to a file of email addresses (one per line; blank
        /// lines and `#` comments are ignored). Reads stdin when
        /// omitted or `-`.
        local_path: Option<PathBuf>,
        /// Send Discourse notifications to added users.
        #[arg(long)]
        notify: bool,
    },
}

#[derive(Subcommand)]
pub enum BackupCommand {
    /// Create a new backup.
    #[command(visible_alias = "cr")]
    Create {
        /// Discourse name.
        discourse: String,
    },
    /// List backups.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: OutputFormat,
        /// Include additional fields where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
    },
    /// Pull (download) a backup to a local file.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Backup filename on the server (from `dsc backup list`).
        backup_filename: String,
        /// Local output path. Defaults to the backup filename in the current directory.
        local_path: Option<PathBuf>,
    },
    /// Push (restore) a backup on the server (alias: restore).
    #[command(visible_alias = "ps", alias = "restore")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Backup filename/path on the target system.
        backup_path: String,
    },
}

#[derive(Subcommand)]
pub enum PaletteCommand {
    /// List color palettes.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Include additional fields where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
    },
    /// Pull a palette to local JSON.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Palette ID.
        palette_id: u64,
        /// Destination file path (auto-derived when omitted).
        local_path: Option<PathBuf>,
    },
    /// Push local JSON to create or update a palette.
    #[command(visible_alias = "ps")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Local JSON file path.
        local_path: PathBuf,
        /// Palette ID to update (creates a new palette when omitted).
        palette_id: Option<u64>,
    },
}

#[derive(Subcommand)]
pub enum PluginCommand {
    /// List installed plugins.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Include additional fields where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
    },
    /// Install a plugin from URL.
    #[command(visible_alias = "i")]
    Install {
        /// Discourse name.
        discourse: String,
        /// Plugin repository URL.
        url: String,
    },
    /// Remove a plugin by name.
    #[command(visible_alias = "rm")]
    Remove {
        /// Discourse name.
        discourse: String,
        /// Plugin name.
        name: String,
    },
}

#[derive(Subcommand)]
pub enum ThemeCommand {
    /// List installed themes.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Include additional fields where supported.
        #[arg(long, short = 'v')]
        verbose: bool,
    },
    /// Install a theme from URL.
    #[command(visible_alias = "i")]
    Install {
        /// Discourse name.
        discourse: String,
        /// Theme repository URL.
        url: String,
    },
    /// Remove a theme by name.
    #[command(visible_alias = "rm")]
    Remove {
        /// Discourse name.
        discourse: String,
        /// Theme name.
        name: String,
    },
    /// Pull a theme to a local JSON file.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Theme ID (from `dsc theme list`).
        theme_id: u64,
        /// Destination file path (auto-derived from theme name when omitted).
        local_path: Option<PathBuf>,
    },
    /// Push a local JSON file to create or update a theme.
    #[command(visible_alias = "ps")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Local JSON file path.
        local_path: PathBuf,
        /// Theme ID to update (creates a new theme when omitted).
        theme_id: Option<u64>,
    },
    /// Duplicate a theme and print the new theme ID.
    #[command(visible_alias = "dup")]
    Duplicate {
        /// Discourse name.
        discourse: String,
        /// Theme ID to duplicate (from `dsc theme list`).
        theme_id: u64,
    },
}

#[derive(Subcommand)]
pub enum PmCommand {
    /// Send a private message.
    #[command(visible_alias = "s")]
    Send {
        /// Discourse name.
        discourse: String,
        /// Recipient(s) — comma-separated usernames or group names.
        recipients: String,
        /// PM title / subject.
        #[arg(long, short = 't')]
        title: String,
        /// Input file path. Reads stdin when omitted or `-`.
        local_path: Option<PathBuf>,
    },
    /// List PMs for a user.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Username whose PMs to list.
        username: String,
        /// Direction / view: inbox | sent | archive | unread | new.
        #[arg(long, short = 'd', default_value = "inbox")]
        direction: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
}

#[derive(Subcommand)]
pub enum ApiKeyCommand {
    /// List API keys.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Create a new API key. The secret is only shown at creation time —
    /// capture it from the output.
    #[command(visible_alias = "cr")]
    Create {
        /// Discourse name.
        discourse: String,
        /// Description / label for the key (shown in admin UI).
        description: String,
        /// Username the key acts as. Omit for a global all-users key.
        #[arg(long, short = 'u')]
        username: Option<String>,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Revoke an API key by ID.
    #[command(visible_alias = "rm")]
    Revoke {
        /// Discourse name.
        discourse: String,
        /// API key ID (from `dsc api-key list`).
        key_id: u64,
    },
}

#[derive(Subcommand)]
pub enum InviteCommand {
    /// Invite a single email address.
    #[command(visible_alias = "s")]
    Send {
        /// Discourse name.
        discourse: String,
        /// Email address to invite.
        email: String,
        /// Add invitee to one or more groups on accept (repeatable).
        #[arg(long, short = 'g')]
        group: Vec<u64>,
        /// Land the invitee on a specific topic on accept.
        #[arg(long, short = 't')]
        topic: Option<u64>,
        /// Custom invitation message.
        #[arg(long, short = 'm')]
        message: Option<String>,
    },
    /// Bulk-invite from a file (or stdin) of email addresses.
    #[command(visible_alias = "b")]
    Bulk {
        /// Discourse name.
        discourse: String,
        /// Path to a file of email addresses (one per line; blank lines and
        /// `#` comments ignored). Reads stdin when omitted or `-`.
        local_path: Option<PathBuf>,
        /// Add every invitee to one or more groups on accept (repeatable).
        #[arg(long, short = 'g')]
        group: Vec<u64>,
        /// Land every invitee on a specific topic on accept.
        #[arg(long, short = 't')]
        topic: Option<u64>,
        /// Custom invitation message attached to each invite.
        #[arg(long, short = 'm')]
        message: Option<String>,
    },
}

#[derive(Subcommand)]
pub enum UserCommand {
    /// List users via the admin users endpoint.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Listing type: active | new | staff | suspended | silenced | staged.
        #[arg(long, short = 'l', default_value = "active")]
        listing: String,
        /// Page number (Discourse paginates 100 per page).
        #[arg(long, short = 'p', default_value_t = 1)]
        page: u32,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Show detailed info for a user.
    #[command(visible_alias = "i")]
    Info {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Suspend a user.
    #[command(visible_alias = "sus")]
    Suspend {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
        /// When the suspension ends. ISO-8601 timestamp (e.g.
        /// `2026-12-31T00:00:00Z`) or `forever`.
        #[arg(long, short = 'u', default_value = "forever")]
        until: String,
        /// Reason shown to the user and in the audit log.
        #[arg(long, short = 'r', default_value = "")]
        reason: String,
    },
    /// Remove a suspension from a user.
    #[command(visible_alias = "uns")]
    Unsuspend {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
    },
    /// Silence a user (prevents posting; less visible than suspend).
    #[command(visible_alias = "sil")]
    Silence {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
        /// When the silence ends. ISO-8601 timestamp; empty means
        /// indefinite.
        #[arg(long, short = 'u', default_value = "")]
        until: String,
        /// Reason shown to the user and in the audit log.
        #[arg(long, short = 'r', default_value = "")]
        reason: String,
    },
    /// Lift a silence on a user.
    #[command(visible_alias = "unsil")]
    Unsilence {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
    },
    /// Grant the user the admin or moderator role.
    #[command(visible_alias = "pr")]
    Promote {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
        /// Role to grant.
        #[arg(long, short = 'r', value_enum)]
        role: RoleArg,
    },
    /// Revoke the user's admin or moderator role.
    #[command(visible_alias = "de")]
    Demote {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
        /// Role to revoke.
        #[arg(long, short = 'r', value_enum)]
        role: RoleArg,
    },
    /// Create a new user. `--approve` also marks the account approved
    /// (needed when site requires manual approval). Password is either
    /// supplied via stdin (`--password-stdin`) or omitted — in the
    /// latter case the user will have to set one via the reset flow.
    #[command(visible_alias = "cr")]
    Create {
        /// Discourse name.
        discourse: String,
        /// New user's email address.
        email: String,
        /// New user's username.
        username: String,
        /// Display name (optional).
        #[arg(long, short = 'N')]
        name: Option<String>,
        /// Read the password from stdin instead of auto-reset.
        #[arg(long)]
        password_stdin: bool,
        /// Also mark the user approved (for sites with manual approval).
        #[arg(long)]
        approve: bool,
    },
    /// Trigger Discourse's password-reset email flow for a user.
    #[command(name = "password-reset", visible_aliases = ["pwreset", "pw-reset"])]
    PasswordReset {
        /// Discourse name.
        discourse: String,
        /// Username or email.
        username: String,
    },
    /// Set a user's primary email address. Requires admin scope.
    #[command(name = "email-set", visible_alias = "email")]
    EmailSet {
        /// Discourse name.
        discourse: String,
        /// Username.
        username: String,
        /// New email address.
        email: String,
    },
    /// Show a user's recent public activity (topics + replies by default).
    ///
    /// Built for the "archive my own activity to a journal forum" loop —
    /// pipe the markdown output straight into `dsc topic reply`/`topic new`.
    #[command(visible_alias = "act")]
    Activity {
        /// Discourse name (the *source* forum to read activity from).
        discourse: String,
        /// Username whose activity to read.
        username: String,
        /// How far back to look. Accepts `7d`, `24h`, `30m`, `1w`, `90s`, or
        /// an ISO-8601 timestamp / date. Omit to fetch everything available.
        #[arg(long, short = 's')]
        since: Option<String>,
        /// Action types to include, comma-separated. Default: topics,replies.
        /// Also recognises: mentions, quotes, likes, edits, responses.
        #[arg(long, short = 't', default_value = "topics,replies")]
        types: String,
        /// Hard cap on number of items returned.
        #[arg(long, short = 'L')]
        limit: Option<u32>,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "markdown")]
        format: ActivityFormatArg,
    },
    /// Manage a user's group memberships.
    #[command(visible_alias = "g")]
    Groups {
        #[command(subcommand)]
        command: UserGroupsCommand,
    },
}

#[derive(ValueEnum, Clone, Copy)]
pub enum SectionArg {
    All,
    Growth,
    Activity,
    Health,
}

#[derive(ValueEnum, Clone, Copy)]
pub enum AnalyticsFormat {
    /// Plain text (default). Fixed-width columns, no borders.
    Text,
    /// DuckDB-style box-drawing table. Falls through to `text` when
    /// stdout isn't a TTY.
    Table,
    /// Pretty JSON.
    Json,
    /// YAML.
    #[value(alias = "yml")]
    Yaml,
    /// Markdown bullet list per section.
    #[value(alias = "md")]
    Markdown,
    /// Markdown table per section.
    #[value(alias = "md-table", name = "markdown-table")]
    MarkdownTable,
    /// CSV — one row per metric.
    Csv,
}

#[derive(ValueEnum, Clone, Copy)]
pub enum ActivityFormatArg {
    Text,
    Json,
    #[value(alias = "yml")]
    Yaml,
    #[value(alias = "md")]
    Markdown,
    Csv,
}

#[derive(ValueEnum, Clone, Copy)]
pub enum RoleArg {
    Admin,
    Moderator,
}

#[derive(Subcommand)]
pub enum UserGroupsCommand {
    /// List the groups a user belongs to.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Target username.
        username: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Add a user to a group.
    #[command(visible_alias = "a")]
    Add {
        /// Discourse name.
        discourse: String,
        /// Target username.
        username: String,
        /// Group ID.
        group_id: u64,
        /// Send Discourse notification to the user.
        #[arg(long)]
        notify: bool,
    },
    /// Remove a user from a group.
    #[command(visible_alias = "rm")]
    Remove {
        /// Discourse name.
        discourse: String,
        /// Target username.
        username: String,
        /// Group ID.
        group_id: u64,
    },
}

#[derive(Subcommand)]
pub enum PostCommand {
    /// Pull a post's raw Markdown to a local file.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Post ID.
        post_id: u64,
        /// Output file path. Prints to stdout when omitted.
        local_path: Option<PathBuf>,
    },
    /// Push a local file to update a post (alias: edit).
    #[command(visible_alias = "ps", alias = "edit")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Post ID.
        post_id: u64,
        /// Input file path. Reads stdin when omitted or `-`.
        local_path: Option<PathBuf>,
    },
    /// Delete a post by ID.
    #[command(visible_alias = "rm")]
    Delete {
        /// Discourse name.
        discourse: String,
        /// Post ID.
        post_id: u64,
    },
    /// Move a post to a different topic.
    #[command(visible_alias = "mv")]
    Move {
        /// Discourse name.
        discourse: String,
        /// Post ID to move.
        post_id: u64,
        /// Destination topic ID.
        #[arg(long = "to-topic", short = 't')]
        to_topic: u64,
    },
}

#[derive(Subcommand)]
pub enum TagCommand {
    /// List every tag on the Discourse.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
    /// Pull the tag taxonomy (tags + tag groups) to a local file.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Output file (default: tags.yaml). Extension determines format (.yaml/.json).
        #[arg(default_value = "tags.yaml")]
        local_path: PathBuf,
    },
    /// Push a local taxonomy file to the server (upsert; optionally prune).
    #[command(visible_alias = "ps")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Input taxonomy file.
        local_path: PathBuf,
        /// Delete server tags/groups absent from the file.
        #[arg(long)]
        prune: bool,
    },
    /// Rename a tag, preserving topic associations.
    ///
    /// Discourse rewrites every topic's tag list in-place, so this avoids
    /// the delete-and-recreate pattern that loses topic membership.
    #[command(visible_alias = "rn")]
    Rename {
        /// Discourse name.
        discourse: String,
        /// Current tag name.
        old_name: String,
        /// New tag name.
        new_name: String,
    },
}

#[derive(Subcommand)]
pub enum SettingCommand {
    /// Set a site setting on a Discourse (or all tagged Discourses).
    ///
    /// Usage:
    ///   dsc setting set <discourse> <setting> <value>
    ///   dsc setting set --tags <tag1,tag2> <setting> <value>
    #[command(visible_alias = "s")]
    Set {
        /// Discourse name. Required unless `--tags` is provided.
        discourse: Option<String>,
        /// Setting key. Required.
        setting: Option<String>,
        /// Setting value. Required.
        value: Option<String>,
        /// Tag filter (comma/semicolon separated, match-any). Apply across all
        /// Discourses matching any of the tags. When set, omit `<discourse>`
        /// and pass `<setting> <value>` as the only positionals.
        #[arg(long, value_name = "tag1,tag2")]
        tags: Option<String>,
    },

    /// Get the current value of a site setting.
    #[command(visible_alias = "g")]
    Get {
        /// Discourse name.
        discourse: String,
        /// Setting key.
        setting: String,
    },

    /// List all site settings.
    #[command(visible_alias = "ls")]
    List {
        /// Discourse name.
        discourse: String,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
        /// Show output even when list is empty.
        #[arg(long, short = 'v')]
        verbose: bool,
    },

    /// Snapshot all site settings (with metadata) to a local file.
    ///
    /// See spec/setting-sync.md for the full schema and workflow. The
    /// generated file is a self-documenting YAML (or JSON) including each
    /// setting's default, type, category, and description.
    #[command(visible_alias = "pl")]
    Pull {
        /// Discourse name.
        discourse: String,
        /// Output path. Format detected by extension (.json → JSON,
        /// otherwise YAML). Defaults to `settings.yaml`.
        #[arg(default_value = "settings.yaml")]
        local_path: PathBuf,
        /// Only include settings whose value differs from default. Produces
        /// a manageable file (~50-100 entries) suitable for version control.
        #[arg(long, short = 'c')]
        changed_only: bool,
        /// Limit to settings in this category (e.g. `required`, `email`,
        /// `security`).
        #[arg(long)]
        category: Option<String>,
    },

    /// Apply a settings snapshot file to a Discourse (idempotent).
    ///
    /// Compares each setting in the file against the server and PUTs only
    /// values that differ. Combine with `--dry-run` to preview the plan.
    #[command(visible_alias = "ph")]
    Push {
        /// Discourse name.
        discourse: String,
        /// Path to the settings snapshot file (YAML or JSON).
        local_path: PathBuf,
        /// For settings present on the server but absent from the file,
        /// reset them to their default value. Off by default (file describes
        /// only the values you care about).
        #[arg(long)]
        reset_unlisted: bool,
    },

    /// Compare site settings between two sources.
    ///
    /// Each source can be a Discourse name (live fetch) or a path to a
    /// snapshot file produced by `dsc setting pull`. Sources are detected
    /// by whether the argument refers to an existing file on disk; if not,
    /// it is treated as a Discourse name.
    #[command(visible_alias = "df")]
    Diff {
        /// First source: Discourse name or snapshot file path.
        source: String,
        /// Second source: Discourse name or snapshot file path.
        target: String,
        /// Filter to settings where at least one source differs from default.
        /// Reduces noise when most settings on both sides are still default.
        #[arg(long, short = 'c')]
        changed_only: bool,
        /// Limit to settings in this category (e.g. `required`, `email`).
        /// Only effective when both sources carry category metadata.
        #[arg(long)]
        category: Option<String>,
        /// Output format.
        #[arg(long, short = 'f', value_enum, default_value = "text")]
        format: ListFormat,
    },
}

#[derive(ValueEnum, Clone, Copy)]
pub enum CompletionShell {
    /// Bash shell.
    Bash,
    /// Zsh shell.
    Zsh,
    /// Fish shell.
    Fish,
}

impl From<CompletionShell> for Shell {
    fn from(value: CompletionShell) -> Self {
        match value {
            CompletionShell::Bash => Shell::Bash,
            CompletionShell::Zsh => Shell::Zsh,
            CompletionShell::Fish => Shell::Fish,
        }
    }
}

#[derive(ValueEnum, Clone)]
pub enum OutputFormat {
    /// Plain text.
    #[value(alias = "plaintext")]
    Text,
    /// Markdown list.
    Markdown,
    /// Markdown table.
    MarkdownTable,
    /// Pretty JSON.
    Json,
    /// YAML.
    #[value(alias = "yml")]
    Yaml,
    /// CSV.
    Csv,
    /// One base URL per line (pipe-friendly).
    #[value(alias = "url")]
    Urls,
}

#[derive(ValueEnum, Clone, Copy)]
pub enum ListFormat {
    /// Plain text.
    Text,
    /// Pretty JSON.
    Json,
    /// YAML.
    #[value(alias = "yml")]
    Yaml,
}

#[derive(ValueEnum, Clone, Copy)]
pub enum StructuredFormat {
    /// Pretty JSON.
    Json,
    /// YAML.
    #[value(alias = "yml")]
    Yaml,
}