1use crate::lino_database_input::{DEFAULT_IMPORT_DEPTH, MAX_IMPORT_DEPTH};
4use crate::protocol::ArityRange;
5use anyhow::{bail, Result};
6use std::env;
7use std::ffi::OsString;
8
9const DEFAULT_DATABASE_FILENAME: &str = "db.links";
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Cli {
13 pub db: String,
14 pub query: Option<String>,
15 pub query_arg: Option<String>,
16 pub trace: bool,
17 pub indexed_lookups: bool,
18 pub auto_create_missing_references: bool,
19 pub structure: Option<u32>,
20 pub before: bool,
21 pub changes: bool,
22 pub after: bool,
23 pub lino_input: Option<String>,
24 pub lino_input_max_depth: usize,
25 pub lino_output: Option<String>,
26 pub binary_input: Option<String>,
27 pub binary_output: Option<String>,
28 pub always: bool,
29 pub once: bool,
30 pub never: bool,
31 pub triggers: bool,
32 pub triggers_file: Option<String>,
33 pub embed_triggers: bool,
34 pub transactions: bool,
35 pub transactions_file: Option<String>,
36 pub commit_mode: Option<String>,
37 pub retention: Option<String>,
38 pub vc: bool,
39 pub vc_file: Option<String>,
40 pub branch: Option<String>,
41 pub branch_from: Option<i64>,
42 pub checkout: Option<String>,
43 pub tag: Option<String>,
44 pub list_branches: bool,
45 pub list_tags: bool,
46 pub show_log: bool,
47 pub serve: Option<String>,
50 pub serve_also: Vec<String>,
52 pub forward: Option<String>,
55 pub graphql: bool,
57 pub jwt_secret: Option<String>,
59 pub graphql_admin_secret: Option<String>,
61 pub deep_packages: Vec<String>,
63 pub connect: Option<String>,
64 pub protocol: Option<String>,
65 pub external_references: bool,
66 pub arity: ArityRange,
67 pub packed_widths: bool,
68 pub triplets: bool,
70 pub permissions: Option<String>,
72 pub subject: Option<String>,
74 pub tokens: Option<String>,
76 pub grant_token: Option<String>,
77 pub revoke_token: Option<String>,
78 pub token: Option<String>,
80 pub lock: Option<String>,
83 pub lock_timeout: Option<String>,
84 pub lock_status: bool,
85}
86
87impl Default for Cli {
88 fn default() -> Self {
89 Self {
90 db: DEFAULT_DATABASE_FILENAME.to_string(),
91 query: None,
92 query_arg: None,
93 trace: false,
94 indexed_lookups: false,
95 auto_create_missing_references: false,
96 structure: None,
97 before: false,
98 changes: false,
99 after: false,
100 lino_input: None,
101 lino_input_max_depth: DEFAULT_IMPORT_DEPTH,
102 lino_output: None,
103 binary_input: None,
104 binary_output: None,
105 always: false,
106 once: false,
107 never: false,
108 triggers: false,
109 triggers_file: None,
110 embed_triggers: false,
111 transactions: false,
112 transactions_file: None,
113 commit_mode: None,
114 retention: None,
115 vc: false,
116 vc_file: None,
117 branch: None,
118 branch_from: None,
119 checkout: None,
120 tag: None,
121 list_branches: false,
122 list_tags: false,
123 show_log: false,
124 serve: None,
125 serve_also: Vec::new(),
126 forward: None,
127 graphql: false,
128 jwt_secret: None,
129 graphql_admin_secret: None,
130 deep_packages: Vec::new(),
131 connect: None,
132 protocol: None,
133 external_references: false,
134 arity: ArityRange::DOUBLETS,
135 packed_widths: false,
136 triplets: false,
137 permissions: None,
138 subject: None,
139 tokens: None,
140 grant_token: None,
141 revoke_token: None,
142 token: None,
143 lock: None,
144 lock_timeout: None,
145 lock_status: false,
146 }
147 }
148}
149
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub enum CliCommand {
152 Run(Box<Cli>),
153 Help,
154 Version,
155}
156
157impl Cli {
158 pub fn transactions_requested(&self) -> bool {
160 self.transactions
161 || self.transactions_file.is_some()
162 || self.commit_mode.is_some()
163 || self.retention.is_some()
164 || self.show_log
165 || self.vc_requested()
166 }
167
168 pub fn trigger_command_count(&self) -> usize {
171 usize::from(self.always) + usize::from(self.once) + usize::from(self.never)
172 }
173
174 pub fn persistent_transformations_requested(&self) -> bool {
180 self.always
181 || self.once
182 || self.never
183 || self.triggers
184 || self.embed_triggers
185 || self.triggers_file.is_some()
186 }
187
188 pub fn serve_endpoints(&self) -> Vec<&str> {
190 self.serve
191 .iter()
192 .chain(&self.serve_also)
193 .map(String::as_str)
194 .collect()
195 }
196
197 fn add_serve(&mut self, endpoint: String) {
198 if self.serve.is_none() {
199 self.serve = Some(endpoint);
200 } else {
201 self.serve_also.push(endpoint);
202 }
203 }
204
205 pub fn vc_requested(&self) -> bool {
207 self.vc
208 || self.vc_file.is_some()
209 || self.branch.is_some()
210 || self.branch_from.is_some()
211 || self.checkout.is_some()
212 || self.tag.is_some()
213 || self.list_branches
214 || self.list_tags
215 }
216
217 pub fn parse() -> Result<CliCommand> {
218 lino_arguments::init();
219 Self::parse_from(env::args_os())
220 }
221
222 pub fn parse_from<I, T>(args: I) -> Result<CliCommand>
223 where
224 I: IntoIterator<Item = T>,
225 T: Into<OsString>,
226 {
227 let mut cli = Cli::default();
228 let mut args = args
229 .into_iter()
230 .map(|arg| arg.into().to_string_lossy().into_owned())
231 .peekable();
232
233 let _program = args.next();
234
235 while let Some(arg) = args.next() {
236 if let Some(value) = inline_value(&arg, &["--db", "--data-source", "--data"]) {
237 cli.db = value.to_string();
238 continue;
239 }
240 if let Some(value) = inline_value(&arg, &["--query", "--apply", "--do"]) {
241 cli.query = Some(value.to_string());
242 continue;
243 }
244 if let Some(value) = inline_value(&arg, &["--structure"]) {
245 cli.structure = Some(parse_link_id("--structure", value)?);
246 continue;
247 }
248 if let Some(value) = inline_value(&arg, &["--indexed-lookups"]) {
249 cli.indexed_lookups = parse_bool("--indexed-lookups", value)?;
250 continue;
251 }
252 if let Some(value) = inline_value(&arg, &["--trace"]) {
253 cli.trace = parse_bool("--trace", value)?;
254 continue;
255 }
256 if let Some(value) = inline_value(&arg, &["--auto-create-missing-references"]) {
257 cli.auto_create_missing_references =
258 parse_bool("--auto-create-missing-references", value)?;
259 continue;
260 }
261 if let Some(value) = inline_value(&arg, &["--before"]) {
262 cli.before = parse_bool("--before", value)?;
263 continue;
264 }
265 if let Some(value) = inline_value(&arg, &["--changes"]) {
266 cli.changes = parse_bool("--changes", value)?;
267 continue;
268 }
269 if let Some(value) = inline_value(&arg, &["--after", "--links"]) {
270 cli.after = parse_bool("--after", value)?;
271 continue;
272 }
273 if let Some(value) = inline_value(&arg, &["--out", "--lino-output", "--export"]) {
274 cli.lino_output = Some(value.to_string());
275 continue;
276 }
277 if let Some(value) = inline_value(&arg, &["--in", "--lino-input", "--import"]) {
278 cli.lino_input = Some(value.to_string());
279 continue;
280 }
281 if let Some(value) = inline_value(&arg, &["--in-max-depth"]) {
282 cli.lino_input_max_depth = parse_import_depth(value)?;
283 continue;
284 }
285 if let Some(value) = inline_value(
286 &arg,
287 &["--export-binary", "--binary-output", "--binary-out"],
288 ) {
289 cli.binary_output = Some(value.to_string());
290 continue;
291 }
292 if let Some(value) =
293 inline_value(&arg, &["--import-binary", "--binary-input", "--binary-in"])
294 {
295 cli.binary_input = Some(value.to_string());
296 continue;
297 }
298 if let Some(value) = inline_value(&arg, &["--always"]) {
299 cli.always = parse_bool("--always", value)?;
300 continue;
301 }
302 if let Some(value) = inline_value(&arg, &["--once"]) {
303 cli.once = parse_bool("--once", value)?;
304 continue;
305 }
306 if let Some(value) = inline_value(&arg, &["--never"]) {
307 cli.never = parse_bool("--never", value)?;
308 continue;
309 }
310 if let Some(value) = inline_value(&arg, &["--triggers"]) {
311 cli.triggers = parse_bool("--triggers", value)?;
312 continue;
313 }
314 if let Some(value) = inline_value(&arg, &["--triggers-file"]) {
315 cli.triggers_file = Some(value.to_string());
316 continue;
317 }
318 if let Some(value) = inline_value(&arg, &["--embed-triggers"]) {
319 cli.embed_triggers = parse_bool("--embed-triggers", value)?;
320 continue;
321 }
322 if let Some(value) = inline_value(&arg, &["--transactions"]) {
323 cli.transactions = parse_bool("--transactions", value)?;
324 continue;
325 }
326 if let Some(value) = inline_value(&arg, &["--transactions-file"]) {
327 cli.transactions_file = Some(value.to_string());
328 continue;
329 }
330 if let Some(value) = inline_value(&arg, &["--commit-mode"]) {
331 cli.commit_mode = Some(value.to_string());
332 continue;
333 }
334 if let Some(value) = inline_value(&arg, &["--retention"]) {
335 cli.retention = Some(value.to_string());
336 continue;
337 }
338 if let Some(value) = inline_value(&arg, &["--vc"]) {
339 cli.vc = parse_bool("--vc", value)?;
340 continue;
341 }
342 if let Some(value) = inline_value(&arg, &["--vc-file"]) {
343 cli.vc_file = Some(value.to_string());
344 continue;
345 }
346 if let Some(value) = inline_value(&arg, &["--branch"]) {
347 cli.branch = Some(value.to_string());
348 continue;
349 }
350 if let Some(value) = inline_value(&arg, &["--branch-from"]) {
351 cli.branch_from = Some(parse_seq("--branch-from", value)?);
352 continue;
353 }
354 if let Some(value) = inline_value(&arg, &["--checkout"]) {
355 cli.checkout = Some(value.to_string());
356 continue;
357 }
358 if let Some(value) = inline_value(&arg, &["--tag"]) {
359 cli.tag = Some(value.to_string());
360 continue;
361 }
362 if let Some(value) = inline_value(&arg, &["--list-branches"]) {
363 cli.list_branches = parse_bool("--list-branches", value)?;
364 continue;
365 }
366 if let Some(value) = inline_value(&arg, &["--list-tags"]) {
367 cli.list_tags = parse_bool("--list-tags", value)?;
368 continue;
369 }
370 if let Some(value) = inline_value(&arg, &["--log"]) {
371 cli.show_log = parse_bool("--log", value)?;
372 continue;
373 }
374 if let Some(value) = inline_value(&arg, &["--jwt-secret"]) {
375 cli.jwt_secret = Some(value.to_string());
376 continue;
377 }
378 if let Some(value) = inline_value(&arg, &["--graphql-admin-secret"]) {
379 cli.graphql_admin_secret = Some(value.to_string());
380 continue;
381 }
382 if let Some(value) = inline_value(&arg, &["--deep-package"]) {
383 cli.deep_packages.push(value.to_string());
384 continue;
385 }
386 if let Some(value) = inline_value(&arg, &["--serve"]) {
387 cli.add_serve(value.to_string());
388 continue;
389 }
390 if let Some(value) = inline_value(&arg, &["--forward"]) {
391 cli.forward = Some(value.to_string());
392 continue;
393 }
394 if let Some(value) = inline_value(&arg, &["--connect"]) {
395 cli.connect = Some(value.to_string());
396 continue;
397 }
398 if let Some(value) = inline_value(&arg, &["--protocol"]) {
399 cli.protocol = Some(value.to_string());
400 continue;
401 }
402 if let Some(value) = inline_value(&arg, &["--external-references"]) {
403 cli.external_references = parse_bool("--external-references", value)?;
404 continue;
405 }
406 if let Some(value) = inline_value(&arg, &["--arity"]) {
407 cli.arity = parse_arity(value)?;
408 continue;
409 }
410 if let Some(value) = inline_value(&arg, &["--packed-widths"]) {
411 cli.packed_widths = parse_bool("--packed-widths", value)?;
412 continue;
413 }
414 if let Some(value) = inline_value(&arg, &["--triplets"]) {
415 cli.triplets = parse_bool("--triplets", value)?;
416 continue;
417 }
418 if let Some(value) = inline_value(&arg, &["--permissions"]) {
419 cli.permissions = Some(value.to_string());
420 continue;
421 }
422 if let Some(value) = inline_value(&arg, &["--subject"]) {
423 cli.subject = Some(value.to_string());
424 continue;
425 }
426 if let Some(value) = inline_value(&arg, &["--tokens"]) {
427 cli.tokens = Some(value.to_string());
428 continue;
429 }
430 if let Some(value) = inline_value(&arg, &["--grant-token"]) {
431 cli.grant_token = Some(value.to_string());
432 continue;
433 }
434 if let Some(value) = inline_value(&arg, &["--revoke-token"]) {
435 cli.revoke_token = Some(value.to_string());
436 continue;
437 }
438 if let Some(value) = inline_value(&arg, &["--lock"]) {
439 cli.lock = Some(value.to_string());
440 continue;
441 }
442 if let Some(value) = inline_value(&arg, &["--lock-timeout"]) {
443 cli.lock_timeout = Some(value.to_string());
444 continue;
445 }
446 if let Some(value) = inline_value(&arg, &["--lock-status"]) {
447 cli.lock_status = parse_bool("--lock-status", value)?;
448 continue;
449 }
450 if let Some(value) = inline_value(&arg, &["--token"]) {
451 cli.token = Some(value.to_string());
452 continue;
453 }
454
455 match arg.as_str() {
456 "-h" | "--help" => return Ok(CliCommand::Help),
457 "-V" | "--version" => return Ok(CliCommand::Version),
458 "-d" | "--db" | "--data-source" | "--data" => {
459 cli.db = next_value(&mut args, &arg)?;
460 }
461 "-q" | "--query" | "--apply" | "--do" => {
462 cli.query = Some(next_value(&mut args, &arg)?);
463 }
464 "--indexed-lookups" => {
465 cli.indexed_lookups = next_bool_value(&mut args, true)?;
466 }
467 "-t" | "--trace" => {
468 cli.trace = next_bool_value(&mut args, true)?;
469 }
470 "--auto-create-missing-references" => {
471 cli.auto_create_missing_references = next_bool_value(&mut args, true)?;
472 }
473 "-s" | "--structure" => {
474 let value = next_value(&mut args, &arg)?;
475 cli.structure = Some(parse_link_id(&arg, &value)?);
476 }
477 "-b" | "--before" => {
478 cli.before = next_bool_value(&mut args, true)?;
479 }
480 "-c" | "--changes" => {
481 cli.changes = next_bool_value(&mut args, true)?;
482 }
483 "-a" | "--after" | "--links" => {
484 cli.after = next_bool_value(&mut args, true)?;
485 }
486 "--out" | "--lino-output" | "--export" => {
487 cli.lino_output = Some(next_value(&mut args, &arg)?);
488 }
489 "--in" | "--lino-input" | "--import" => {
490 cli.lino_input = Some(next_value(&mut args, &arg)?);
491 }
492 "--in-max-depth" => {
493 cli.lino_input_max_depth = parse_import_depth(&next_value(&mut args, &arg)?)?;
494 }
495 "--export-binary" | "--binary-output" | "--binary-out" => {
496 cli.binary_output = Some(next_value(&mut args, &arg)?);
497 }
498 "--import-binary" | "--binary-input" | "--binary-in" => {
499 cli.binary_input = Some(next_value(&mut args, &arg)?);
500 }
501 "--always" => {
502 cli.always = next_bool_value(&mut args, true)?;
503 }
504 "--once" => {
505 cli.once = next_bool_value(&mut args, true)?;
506 }
507 "--never" => {
508 cli.never = next_bool_value(&mut args, true)?;
509 }
510 "--triggers" => {
511 cli.triggers = next_bool_value(&mut args, true)?;
512 }
513 "--triggers-file" => {
514 cli.triggers_file = Some(next_value(&mut args, &arg)?);
515 }
516 "--embed-triggers" => {
517 cli.embed_triggers = next_bool_value(&mut args, true)?;
518 }
519 "--transactions" => {
520 cli.transactions = next_bool_value(&mut args, true)?;
521 }
522 "--transactions-file" => {
523 cli.transactions_file = Some(next_value(&mut args, &arg)?);
524 }
525 "--commit-mode" => {
526 cli.commit_mode = Some(next_value(&mut args, &arg)?);
527 }
528 "--retention" => {
529 cli.retention = Some(next_value(&mut args, &arg)?);
530 }
531 "--vc" => {
532 cli.vc = next_bool_value(&mut args, true)?;
533 }
534 "--vc-file" => {
535 cli.vc_file = Some(next_value(&mut args, &arg)?);
536 }
537 "--branch" => {
538 cli.branch = Some(next_value(&mut args, &arg)?);
539 }
540 "--branch-from" => {
541 let value = next_value(&mut args, &arg)?;
542 cli.branch_from = Some(parse_seq(&arg, &value)?);
543 }
544 "--checkout" => {
545 cli.checkout = Some(next_value(&mut args, &arg)?);
546 }
547 "--tag" => {
548 cli.tag = Some(next_value(&mut args, &arg)?);
549 }
550 "--list-branches" => {
551 cli.list_branches = next_bool_value(&mut args, true)?;
552 }
553 "--list-tags" => {
554 cli.list_tags = next_bool_value(&mut args, true)?;
555 }
556 "--log" => {
557 cli.show_log = next_bool_value(&mut args, true)?;
558 }
559 "--graphql" => {
560 cli.graphql = next_bool_value(&mut args, true)?;
561 }
562 "--jwt-secret" => {
563 cli.jwt_secret = Some(next_value(&mut args, &arg)?);
564 }
565 "--graphql-admin-secret" => {
566 cli.graphql_admin_secret = Some(next_value(&mut args, &arg)?);
567 }
568 "--deep-package" => {
569 cli.deep_packages.push(next_value(&mut args, &arg)?);
570 }
571 "--serve" => {
572 let value = next_value(&mut args, &arg)?;
573 cli.add_serve(value);
574 }
575 "--forward" => {
576 cli.forward = Some(next_value(&mut args, &arg)?);
577 }
578 "--connect" => {
579 cli.connect = Some(next_value(&mut args, &arg)?);
580 }
581 "--protocol" => {
582 cli.protocol = Some(next_value(&mut args, &arg)?);
583 }
584 "--external-references" => {
585 cli.external_references = next_bool_value(&mut args, true)?;
586 }
587 "--arity" => {
588 cli.arity = parse_arity(&next_value(&mut args, &arg)?)?;
589 }
590 "--packed-widths" => {
591 cli.packed_widths = next_bool_value(&mut args, true)?;
592 }
593 "--triplets" => {
594 cli.triplets = next_bool_value(&mut args, true)?;
595 }
596 "--permissions" => {
597 cli.permissions = Some(next_value(&mut args, &arg)?);
598 }
599 "--subject" => {
600 cli.subject = Some(next_value(&mut args, &arg)?);
601 }
602 "--tokens" => {
603 cli.tokens = Some(next_value(&mut args, &arg)?);
604 }
605 "--grant-token" => {
606 cli.grant_token = Some(next_value(&mut args, &arg)?);
607 }
608 "--revoke-token" => {
609 cli.revoke_token = Some(next_value(&mut args, &arg)?);
610 }
611 "--token" => {
612 cli.token = Some(next_value(&mut args, &arg)?);
613 }
614 "--lock" => cli.lock = Some(next_value(&mut args, &arg)?),
615 "--no-lock" => cli.lock = Some("none".into()),
616 "--lock-timeout" => cli.lock_timeout = Some(next_value(&mut args, &arg)?),
617 "--lock-status" => cli.lock_status = next_bool_value(&mut args, true)?,
618 "--" => {
619 for value in args.by_ref() {
620 set_positional_query(&mut cli, value)?;
621 }
622 break;
623 }
624 value if value.starts_with('-') => {
625 bail!("unknown option '{value}'");
626 }
627 value => {
628 set_positional_query(&mut cli, value.to_string())?;
629 }
630 }
631 }
632
633 Ok(CliCommand::Run(Box::new(cli)))
634 }
635
636 pub fn print_help() {
637 print!("{}", Self::help_text());
638 }
639
640 pub fn help_text() -> &'static str {
641 concat!(
642 "LiNo CLI Tool for managing links data store\n\n",
643 "Usage: clink [OPTIONS] [QUERY]\n\n",
644 "Arguments:\n",
645 " [QUERY] LiNo query for CRUD operation\n\n",
646 "Options:\n",
647 " -d, --db <DB>, --data-source <DB>, --data <DB>\n",
648 " Path to the links database file [default: db.links]\n",
649 " -q, --query <QUERY>, --apply <QUERY>, --do <QUERY>\n",
650 " LiNo query for CRUD operation\n",
651 " --indexed-lookups\n",
652 " Use optional lookup-index decorators for local links and names (Rust only)\n",
653 " -t, --trace\n",
654 " Enable trace (verbose output), including each transport event of\n",
655 " --serve, --connect and --forward (as CLINK_IPC_TRACE=1 does)\n",
656 " --auto-create-missing-references\n",
657 " Create missing numeric and named references as self-referential point links\n",
658 " -s, --structure <STRUCTURE>\n",
659 " ID of the link to format its structure\n",
660 " -b, --before\n",
661 " Print the state of the database before applying changes\n",
662 " -c, --changes\n",
663 " Print the changes applied by the query\n",
664 " -a, --after, --links\n",
665 " Print the state of the database after applying changes\n",
666 " --in <IN>, --lino-input <IN>, --import <IN>\n",
667 " Read and import a LiNo file into the database\n",
668 " --in-max-depth <N>\n",
669 " LiNo import nesting limit (1 to 128; default: 64)\n",
670 " --out <OUT>, --lino-output <OUT>, --export <OUT>\n",
671 " Write the complete database as a LiNo file\n",
672 " --import-binary <PATH>, --binary-input <PATH>, --binary-in <PATH>\n",
673 " Read and import a store archive in binary links notation\n",
674 " --export-binary <PATH>, --binary-output <PATH>, --binary-out <PATH>\n",
675 " Write the complete database, names included, as a store archive in\n",
676 " binary links notation\n",
677 " --always\n",
678 " Store the query as an always-on persistent transformation trigger\n",
679 " --once\n",
680 " Store the query as a persistent transformation trigger that deletes\n",
681 " itself after it fires\n",
682 " --never\n",
683 " Remove stored persistent transformation triggers matching the query\n",
684 " --triggers\n",
685 " Enable persistent transformation triggers for this command\n",
686 " --triggers-file <FILE>\n",
687 " Path to the persistent transformation trigger links database\n",
688 " (default: <db>.triggers.links)\n",
689 " --embed-triggers\n",
690 " Store persistent transformation triggers directly in the main links\n",
691 " database\n",
692 " --transactions\n",
693 " Enable the transactions layer (default log path: <db>.transitions.log)\n",
694 " --transactions-file <FILE>\n",
695 " Path to the transitions log store (implies --transactions)\n",
696 " --commit-mode <MODE>\n",
697 " Choose 'sync' or 'async' commits (default: sync, implies --transactions)\n",
698 " --retention <SPEC>\n",
699 " Log retention policy: 'infinite', 'sized:<n>', or 'chunked:<n>:<dir>'\n",
700 " (implies --transactions)\n",
701 " --vc\n",
702 " Enable the version-control decorator (implies --transactions)\n",
703 " --vc-file <FILE>\n",
704 " Path to the version-control branches store\n",
705 " (default: <db>.versioncontrol.links)\n",
706 " --branch <NAME>\n",
707 " Switch to a branch (creating it if --branch-from is also passed).\n",
708 " Implies --vc.\n",
709 " --branch-from <SEQ>\n",
710 " When creating a branch with --branch, fork from this sequence point\n",
711 " --checkout <POINT>\n",
712 " Time-travel to a specific transition sequence or named tag.\n",
713 " Implies --vc.\n",
714 " --tag <NAME[=POINT]>\n",
715 " Create a tag at current head or at a sequence/existing tag.\n",
716 " Uses the first '=' whose complete suffix resolves.\n",
717 " Implies --vc.\n",
718 " --list-branches\n",
719 " List version-control branches and exit\n",
720 " --list-tags\n",
721 " List version-control tags and exit\n",
722 " --log\n",
723 " Print the transitions log and exit (implies --transactions)\n",
724 " --serve <ADDR>\n",
725 " Serve the database on an endpoint until stopped: a TCP address\n",
726 " (e.g. 127.0.0.1:7878) or KIND:ADDRESS for another transport, such as\n",
727 " unix:/tmp/links.sock, pipe:NAME, fifo:DIR, shm:DIR, mmap:DIR, file:DIR,\n",
728 " signal:DIR, mq:DIR, sysvmq:DIR, udp:HOST:PORT, stdio, mailslot:DIR,\n",
729 " copydata:DIR or alpc:DIR (see docs/protocol/ipc-transports-v1.md);\n",
730 " repeat it to serve one store on several endpoints at once. Each\n",
731 " message may use the text or the binary LiNo protocol\n",
732 " --forward <ADDR>\n",
733 " With --serve: relay every connection, byte for byte, to this\n",
734 " endpoint instead of serving a database (stacks transports)\n",
735 " --graphql\n",
736 " With --serve: speak Hasura's GraphQL over HTTP and WebSocket for Deep\n",
737 " --jwt-secret <SECRET>\n",
738 " Signs the tokens of --graphql (default: random for each run)\n",
739 " --graphql-admin-secret <SECRET>\n",
740 " The x-hasura-admin-secret that bypasses --permissions in --graphql\n",
741 " --deep-package <FILE>\n",
742 " Load a Deep package (JSON rows of links) before --graphql serves\n",
743 " --connect <ADDR>\n",
744 " Send the query to a clink server on a --serve endpoint (e.g.\n",
745 " 127.0.0.1:7878 or shm:/tmp/links) instead of opening a database;\n",
746 " an empty query reads every link\n",
747 " --protocol <PROTOCOL>\n",
748 " 'text' or 'binary' for --connect (default: text); 'text', 'binary'\n",
749 " or 'any' for --serve (default: any)\n",
750 " --external-references\n",
751 " Binary protocol: send numbers and characters as external references\n",
752 " --arity <RANGE>\n",
753 " Binary protocol: link lengths to use, 'n', 'min..max' or 'min..'\n",
754 " (default: 2, doublets only; e.g. 2..3 adds triplets, 1.. any length)\n",
755 " --packed-widths\n",
756 " Binary protocol: give each section of links the narrowest reference\n",
757 " width it needs instead of one width for the whole packet\n",
758 " --triplets\n",
759 " With --serve: read every request as triplets, (create: (type from to)),\n",
760 " (each: (index type from to)) and three-part substitutions, over this\n",
761 " store of doublets; without it, every request is about doublets\n",
762 " --permissions <RULES>\n",
763 " Check every read and write against permission rules stored as links:\n",
764 " 'self' (rules in the data) or a links file of rules; without it,\n",
765 " nothing is checked\n",
766 " --subject <LINK>\n",
767 " The subject (a link index or name) queries run as with --permissions,\n",
768 " and the subject of --serve connections before (auth: token)\n",
769 " (default: 0, a guest)\n",
770 " --tokens <TOKENS>\n",
771 " The token table of (auth: token) for --serve, --grant-token and\n",
772 " --revoke-token: 'self' (tokens in the data) or a links file\n",
773 " --grant-token <TOKEN>\n",
774 " Let a token authenticate as --subject in the --tokens table, then exit\n",
775 " --revoke-token <TOKEN>\n",
776 " Forget a token of the --tokens table, then exit\n",
777 " --token <TOKEN>\n",
778 " With --connect: authenticate with (auth: token) before sending the query\n",
779 " --lock <MODE>\n",
780 " How concurrent invocations on one store are serialized: 'flock'\n",
781 " (default; an OS lock on <db>.lock, released when the holder exits),\n",
782 " 'pid' (the directory <db>.lock.d, broken once its owner process is\n",
783 " gone) or 'none'. Writers lock exclusively, read-only runs share it\n",
784 " --no-lock\n",
785 " Same as --lock none, for callers that serialize access themselves\n",
786 " --lock-timeout <MS>\n",
787 " How long to wait for a locked store before failing with the name of\n",
788 " its owner (default: 30000; 0 fails at once)\n",
789 " --lock-status\n",
790 " Print who holds the locks of the store, then exit\n",
791 " -h, --help\n",
792 " Print help\n",
793 " -V, --version\n",
794 " Print version\n",
795 )
796 }
797
798 pub fn version_text() -> String {
799 format!("clink {}", env!("CARGO_PKG_VERSION"))
800 }
801}
802
803fn parse_arity(value: &str) -> Result<ArityRange> {
804 value
805 .trim()
806 .parse()
807 .map_err(|error| anyhow::anyhow!("invalid value for '--arity': {error}"))
808}
809
810fn parse_import_depth(value: &str) -> Result<usize> {
811 value
812 .parse()
813 .ok()
814 .filter(|depth| (1..=MAX_IMPORT_DEPTH).contains(depth))
815 .ok_or_else(|| {
816 anyhow::anyhow!(
817 "invalid value for '--in-max-depth': expected an integer from 1 to {MAX_IMPORT_DEPTH}"
818 )
819 })
820}
821
822fn inline_value<'a>(arg: &'a str, names: &[&str]) -> Option<&'a str> {
823 names.iter().find_map(|name| {
824 arg.strip_prefix(name)
825 .and_then(|rest| rest.strip_prefix('='))
826 })
827}
828
829fn next_value<I>(args: &mut I, option: &str) -> Result<String>
830where
831 I: Iterator<Item = String>,
832{
833 args.next()
834 .ok_or_else(|| anyhow::anyhow!("missing value for option '{option}'"))
835}
836
837fn next_bool_value<I>(args: &mut std::iter::Peekable<I>, default: bool) -> Result<bool>
838where
839 I: Iterator<Item = String>,
840{
841 if let Some(value) = args.peek().and_then(|value| bool_literal(value)) {
842 args.next();
843 Ok(value)
844 } else {
845 Ok(default)
846 }
847}
848
849fn parse_bool(option: &str, value: &str) -> Result<bool> {
850 bool_literal(value)
851 .ok_or_else(|| anyhow::anyhow!("invalid boolean value '{value}' for {option}"))
852}
853
854fn bool_literal(value: &str) -> Option<bool> {
855 match value.to_ascii_lowercase().as_str() {
856 "true" | "1" | "yes" | "on" => Some(true),
857 "false" | "0" | "no" | "off" => Some(false),
858 _ => None,
859 }
860}
861
862fn parse_link_id(option: &str, value: &str) -> Result<u32> {
863 value
864 .parse()
865 .map_err(|_| anyhow::anyhow!("invalid link id '{value}' for {option}"))
866}
867
868fn parse_seq(option: &str, value: &str) -> Result<i64> {
869 value
870 .parse()
871 .map_err(|_| anyhow::anyhow!("invalid sequence value '{value}' for {option}"))
872}
873
874fn set_positional_query(cli: &mut Cli, value: String) -> Result<()> {
875 if cli.query_arg.is_some() {
876 bail!("unexpected extra positional argument '{value}'");
877 }
878
879 cli.query_arg = Some(value);
880 Ok(())
881}