pray-cli 1.5.1

pray CLI for Prayfile: resolve, lock, render, verify, and publish inference input packages
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
use crate::Command;
use pray_core::cli_suggest::unknown_command_message;
use pray_core::{PrayError, PrayResult};
use std::path::PathBuf;

pub(crate) fn parse_command(arguments: Vec<String>) -> PrayResult<Command> {
    let check = arguments.iter().any(|argument| argument == "--check");
    let strict = arguments.iter().any(|argument| argument == "--strict");
    let semantic = arguments.iter().any(|argument| argument == "--semantic");
    let mut iter = arguments.into_iter();
    let command = iter
        .next()
        .ok_or_else(|| PrayError::Usage("pray requires a command; run pray --help".to_string()))?;
    match command.as_str() {
        "manifest" => Ok(Command::Manifest),
        "init" => {
            let mut targets = Vec::new();
            while let Some(argument) = iter.next() {
                if argument == "--targets" {
                    if let Some(value) = iter.next() {
                        targets = value
                            .split(',')
                            .map(|entry| entry.trim().to_string())
                            .filter(|entry| !entry.is_empty())
                            .collect();
                    }
                }
            }
            Ok(Command::Init { targets })
        }
        "prayer" => parse_namespaced_init_command("prayer", iter, Command::PrayerInit),
        "repo" => parse_namespaced_init_command("repo", iter, Command::RepoInit),
        "install" => {
            let mut locked = false;
            let mut frozen = false;
            let mut offline = false;
            for argument in iter {
                match argument.as_str() {
                    "--locked" => locked = true,
                    "--frozen" => {
                        locked = true;
                        frozen = true;
                    }
                    "--offline" => offline = true,
                    other if other.starts_with("--") => {
                        return Err(PrayError::Unsupported(format!(
                            "unknown install flag: {other}"
                        )))
                    }
                    other => {
                        return Err(PrayError::Unsupported(format!(
                            "unexpected install argument: {other}"
                        )))
                    }
                }
            }
            Ok(Command::Install {
                locked,
                frozen,
                offline,
            })
        }
        "add" => parse_add_command(iter),
        "remove" => parse_remove_command(iter),
        "update" => parse_update_command(iter),
        "unlock" => parse_unlock_command(iter),
        "render" => Ok(Command::Render { check }),
        "plan" => parse_plan_command(iter),
        "apply" => Ok(Command::Apply),
        "verify" => Ok(Command::Verify { strict }),
        "drift" => Ok(Command::Drift { semantic }),
        "format" | "fmt" => Ok(Command::Format),
        "package" => Ok(Command::Package),
        "publish" => parse_publish_command(iter),
        "login" => parse_login_command(iter),
        #[cfg(feature = "auth")]
        "serve" => parse_serve_command(iter),
        #[cfg(not(feature = "auth"))]
        "serve" => Err(PrayError::Usage("unknown command: serve".to_string())),
        "confess" => parse_confess_command(iter),
        "list" => Ok(Command::List),
        "outdated" => parse_outdated_command(iter),
        "explain" => parse_explain_command(iter),
        "vendor" => Ok(Command::Vendor),
        "clean" => Ok(Command::Clean),
        "tree" => Ok(Command::Tree),
        "sync" => parse_sync_command(iter),
        "trust" => {
            let arguments: Vec<String> = iter.collect();
            Ok(Command::Trust { arguments })
        }
        "upgrade" => Ok(Command::Upgrade),
        "version" | "-V" | "--version" => Ok(Command::Version),
        other => Err(PrayError::Usage(unknown_command_message(other))),
    }
}

fn parse_namespaced_init_command(
    namespace: &str,
    mut arguments: std::vec::IntoIter<String>,
    command: Command,
) -> PrayResult<Command> {
    match arguments.next() {
        Some(subcommand) if subcommand == "init" => {
            if let Some(argument) = arguments.next() {
                return Err(PrayError::Unsupported(format!(
                    "unexpected {namespace} argument: {argument}"
                )));
            }
            Ok(command)
        }
        Some(other) => Err(PrayError::Unsupported(format!(
            "unknown {namespace} command: {other}"
        ))),
        None => Err(PrayError::Unsupported(format!("{namespace} requires init"))),
    }
}

fn parse_add_command(mut arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut name = None;
    let mut constraint = None;
    let mut path = None;
    while let Some(argument) = arguments.next() {
        match argument.as_str() {
            "--path" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "add requires a path after --path".to_string(),
                    ));
                };
                path = Some(value);
            }
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!("unknown add flag: {other}")))
            }
            other => {
                if name.is_none() {
                    name = Some(other.to_string());
                } else if constraint.is_none() {
                    constraint = Some(other.to_string());
                } else {
                    return Err(PrayError::Unsupported(format!(
                        "unexpected add argument: {other}"
                    )));
                }
            }
        }
    }
    let name =
        name.ok_or_else(|| PrayError::Unsupported("add requires a package name".to_string()))?;
    Ok(Command::Add {
        name,
        constraint,
        path,
    })
}

fn parse_remove_command(arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut name = None;
    for argument in arguments {
        match argument.as_str() {
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown remove flag: {other}"
                )))
            }
            other => {
                if name.is_none() {
                    name = Some(other.to_string());
                } else {
                    return Err(PrayError::Unsupported(format!(
                        "unexpected remove argument: {other}"
                    )));
                }
            }
        }
    }
    let name =
        name.ok_or_else(|| PrayError::Unsupported("remove requires a package name".to_string()))?;
    Ok(Command::Remove { name })
}

fn parse_update_command(arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut package = None;
    let mut major = false;
    let mut latest = false;
    let mut dry_run = false;
    let mut json = false;
    for argument in arguments {
        match argument.as_str() {
            "--major" => major = true,
            "--latest" => latest = true,
            "--dry-run" => dry_run = true,
            "--json" => json = true,
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown update flag: {other}"
                )))
            }
            other => {
                if package.is_none() {
                    package = Some(other.to_string());
                } else {
                    return Err(PrayError::Unsupported(format!(
                        "unexpected update argument: {other}"
                    )));
                }
            }
        }
    }
    Ok(Command::Update {
        package,
        major,
        latest,
        dry_run,
        json,
    })
}

fn parse_plan_command(arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut remote = false;
    for argument in arguments {
        match argument.as_str() {
            "--remote" => remote = true,
            other => {
                return Err(PrayError::Unsupported(format!(
                    "unknown plan flag: {other}"
                )))
            }
        }
    }
    Ok(Command::Plan { remote })
}

fn parse_outdated_command(arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut remote = false;
    for argument in arguments {
        match argument.as_str() {
            "--remote" => remote = true,
            other => {
                return Err(PrayError::Unsupported(format!(
                    "unknown outdated flag: {other}"
                )))
            }
        }
    }
    Ok(Command::Outdated { remote })
}

fn parse_unlock_command(arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut package = None;
    for argument in arguments {
        match argument.as_str() {
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown unlock flag: {other}"
                )))
            }
            other => {
                if package.is_none() {
                    package = Some(other.to_string());
                } else {
                    return Err(PrayError::Unsupported(format!(
                        "unexpected unlock argument: {other}"
                    )));
                }
            }
        }
    }
    let package = package
        .ok_or_else(|| PrayError::Unsupported("unlock requires a package name".to_string()))?;
    Ok(Command::Unlock { package })
}

fn parse_publish_command(mut arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut roots = Vec::new();
    let mut servers = Vec::new();
    let mut signing_key = None;
    while let Some(argument) = arguments.next() {
        match argument.as_str() {
            "--root" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "publish requires a path after --root".to_string(),
                    ));
                };
                roots.push(PathBuf::from(value));
            }
            "--server" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "publish requires a URL after --server".to_string(),
                    ));
                };
                servers.push(value);
            }
            "--signing-key" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "publish requires a path after --signing-key".to_string(),
                    ));
                };
                signing_key = Some(PathBuf::from(value));
            }
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown publish flag: {other}"
                )))
            }
            other => {
                return Err(PrayError::Unsupported(format!(
                    "unexpected publish argument: {other}"
                )))
            }
        }
    }
    if roots.is_empty() && servers.is_empty() {
        return Err(PrayError::Unsupported(
            "publish requires at least one --root PATH or --server URL".to_string(),
        ));
    }
    Ok(Command::Publish {
        roots,
        servers,
        signing_key,
    })
}

fn parse_login_command(mut arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut servers = Vec::new();
    let mut email = None;
    let mut credential_id = None;
    let mut passkey_key = None;
    let mut public_key = None;
    let mut ssh_agent = false;
    while let Some(argument) = arguments.next() {
        match argument.as_str() {
            "--server" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "login requires a URL after --server".to_string(),
                    ));
                };
                servers.push(value);
            }
            "--email" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "login requires an email after --email".to_string(),
                    ));
                };
                email = Some(value);
            }
            "--credential-id" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "login requires a credential id after --credential-id".to_string(),
                    ));
                };
                credential_id = Some(value);
            }
            "--passkey-key" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "login requires a path after --passkey-key".to_string(),
                    ));
                };
                passkey_key = Some(PathBuf::from(value));
            }
            "--public-key" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "login requires a path after --public-key".to_string(),
                    ));
                };
                public_key = Some(PathBuf::from(value));
            }
            "--ssh-agent" => ssh_agent = true,
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown login flag: {other}"
                )))
            }
            other => {
                return Err(PrayError::Unsupported(format!(
                    "unexpected login argument: {other}"
                )))
            }
        }
    }
    if servers.is_empty() {
        return Err(PrayError::Unsupported(
            "login requires at least one --server URL".to_string(),
        ));
    }
    let email = email
        .ok_or_else(|| PrayError::Unsupported("login requires --email ADDRESS".to_string()))?;
    if passkey_key.is_some() == ssh_agent || (passkey_key.is_none() && public_key.is_none()) {
        return Err(PrayError::Unsupported(
            "login requires exactly one authentication mode".to_string(),
        ));
    }
    if passkey_key.is_some() && credential_id.is_none() {
        return Err(PrayError::Unsupported(
            "passkey login requires --credential-id".to_string(),
        ));
    }
    if ssh_agent && public_key.is_none() {
        return Err(PrayError::Unsupported(
            "ssh-agent login requires --public-key".to_string(),
        ));
    }
    Ok(Command::Login {
        servers,
        email,
        credential_id,
        passkey_key,
        public_key,
        ssh_agent,
    })
}

fn parse_serve_command(mut arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut root = PathBuf::from(".");
    let mut host = "127.0.0.1".to_string();
    let mut port = 7429u16;
    let mut stdio = false;
    while let Some(argument) = arguments.next() {
        match argument.as_str() {
            "--root" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "serve requires a path after --root".to_string(),
                    ));
                };
                root = PathBuf::from(value);
            }
            "--host" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "serve requires a host after --host".to_string(),
                    ));
                };
                host = value;
            }
            "--port" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "serve requires a port after --port".to_string(),
                    ));
                };
                port = value
                    .parse::<u16>()
                    .map_err(|error| PrayError::Unsupported(error.to_string()))?;
            }
            "--stdio" => stdio = true,
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown serve flag: {other}"
                )))
            }
            other => {
                return Err(PrayError::Unsupported(format!(
                    "unexpected serve argument: {other}"
                )))
            }
        }
    }
    Ok(Command::Serve {
        root,
        host,
        port,
        stdio,
    })
}

fn parse_sync_command(mut arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut root = PathBuf::from(".");
    let mut peers = Vec::new();
    while let Some(argument) = arguments.next() {
        match argument.as_str() {
            "--root" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "sync requires a path after --root".to_string(),
                    ));
                };
                root = PathBuf::from(value);
            }
            "--peer" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "sync requires a URL after --peer".to_string(),
                    ));
                };
                peers.push(value);
            }
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown sync flag: {other}"
                )))
            }
            other => {
                return Err(PrayError::Unsupported(format!(
                    "unexpected sync argument: {other}"
                )))
            }
        }
    }
    Ok(Command::Sync { root, peers })
}

fn parse_confess_command(mut arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let mut package = None;
    let mut from_lock = None;
    let mut version = None;
    let mut accepted = false;
    let mut rejected = false;
    let mut note = None;
    let mut url = None;
    while let Some(argument) = arguments.next() {
        match argument.as_str() {
            "--from-lock" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "confess requires a lockfile span id after --from-lock".to_string(),
                    ));
                };
                from_lock = Some(value);
            }
            "--version" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "confess requires a version after --version".to_string(),
                    ));
                };
                version = Some(value);
            }
            "--note" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "confess requires a note after --note".to_string(),
                    ));
                };
                note = Some(value);
            }
            "--url" => {
                let Some(value) = arguments.next() else {
                    return Err(PrayError::Unsupported(
                        "confess requires a URL after --url".to_string(),
                    ));
                };
                url = Some(value);
            }
            "--accepted" => accepted = true,
            "--rejected" => rejected = true,
            other if other.starts_with("--") => {
                return Err(PrayError::Unsupported(format!(
                    "unknown confess flag: {other}"
                )))
            }
            other => {
                if package.is_none() && from_lock.is_none() {
                    package = Some(other.to_string());
                } else {
                    return Err(PrayError::Unsupported(format!(
                        "unexpected confess argument: {other}"
                    )));
                }
            }
        }
    }
    if package.is_some() == from_lock.is_some() {
        return Err(PrayError::Unsupported(
            "confess requires exactly one of a package name or --from-lock".to_string(),
        ));
    }
    if accepted == rejected {
        return Err(PrayError::Unsupported(
            "confess requires exactly one of --accepted or --rejected".to_string(),
        ));
    }
    Ok(Command::Confess {
        package,
        from_lock,
        version,
        accepted,
        rejected,
        note,
        url,
    })
}

fn parse_explain_command(mut arguments: std::vec::IntoIter<String>) -> PrayResult<Command> {
    let package = arguments
        .next()
        .ok_or_else(|| PrayError::Unsupported("explain requires a package name".to_string()))?;
    if let Some(argument) = arguments.next() {
        return Err(PrayError::Unsupported(format!(
            "unexpected explain argument: {argument}"
        )));
    }
    Ok(Command::Explain { package })
}