passlane 3.1.0

A password manager and authenticator for the command line
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
use crate::actions::ItemType;

#[derive(Debug, PartialEq)]
pub enum ReplCommand {
    Show { item_type: ItemType, grep: Option<String> },
    Add { item_type: ItemType },
    Edit { item_type: ItemType, grep: Option<String> },
    Delete { item_type: ItemType, grep: Option<String> },
    Gen,
    Import { file_path: Option<String> },
    Export { item_type: ItemType, file_path: Option<String> },
    Lock,
    Unlock { totp: bool },
    Status,
    Completions,
    Help { command: Option<String> },
    Quit,
    Empty,
    Unknown(String),
}

/// Known command names for completion
pub const COMMAND_NAMES: &[&str] = &[
    "show", "add", "edit", "delete", "gen", "import", "export",
    "unlock", "lock", "status", "completions", "help", "quit", "exit",
];

/// Known type names for completion (second token)
pub const TYPE_NAMES: &[&str] = &[
    "creds", "cred", "credentials",
    "cards", "card", "payments",
    "notes", "note",
    "otp", "totp",
];

fn parse_item_type(token: &str) -> Option<ItemType> {
    match token {
        "creds" | "cred" | "credentials" => Some(ItemType::Credential),
        "cards" | "card" | "payments" => Some(ItemType::Payment),
        "notes" | "note" => Some(ItemType::Note),
        "otp" | "totp" => Some(ItemType::Totp),
        _ => None,
    }
}

pub fn parse_input(line: &str) -> ReplCommand {
    let trimmed = line.trim();
    if trimmed.is_empty() {
        return ReplCommand::Empty;
    }

    let tokens: Vec<&str> = trimmed.split_whitespace().collect();
    let command = tokens[0].to_lowercase();
    let rest = &tokens[1..];

    match command.as_str() {
        "show" => {
            let (item_type, grep) = parse_type_and_arg(rest, ItemType::Credential);
            // For credentials with no pattern, default to show all
            let grep = match (&item_type, &grep) {
                (ItemType::Credential, None) => Some(".*".to_string()),
                _ => grep,
            };
            ReplCommand::Show { item_type, grep }
        }
        "add" => {
            let item_type = if let Some(token) = rest.first() {
                parse_item_type(&token.to_lowercase()).unwrap_or(ItemType::Credential)
            } else {
                ItemType::Credential
            };
            ReplCommand::Add { item_type }
        }
        "edit" => {
            let (item_type, grep) = parse_type_and_arg(rest, ItemType::Credential);
            ReplCommand::Edit { item_type, grep }
        }
        "delete" => {
            let (item_type, grep) = parse_type_and_arg(rest, ItemType::Credential);
            ReplCommand::Delete { item_type, grep }
        }
        "gen" => ReplCommand::Gen,
        "import" => {
            let file_path = rest.first().map(|s| s.to_string());
            ReplCommand::Import { file_path }
        }
        "export" => {
            // export [type] <file>
            if rest.is_empty() {
                ReplCommand::Export { item_type: ItemType::Credential, file_path: None }
            } else if rest.len() == 1 {
                // Could be a type or a file path
                let token = rest[0].to_lowercase();
                if let Some(item_type) = parse_item_type(&token) {
                    ReplCommand::Export { item_type, file_path: None }
                } else {
                    ReplCommand::Export { item_type: ItemType::Credential, file_path: Some(rest[0].to_string()) }
                }
            } else {
                let token = rest[0].to_lowercase();
                if let Some(item_type) = parse_item_type(&token) {
                    ReplCommand::Export { item_type, file_path: Some(rest[1].to_string()) }
                } else {
                    ReplCommand::Export { item_type: ItemType::Credential, file_path: Some(rest[0].to_string()) }
                }
            }
        }
        "lock" => ReplCommand::Lock,
        "unlock" => {
            let totp = rest.first().map_or(false, |t| {
                let lower = t.to_lowercase();
                lower == "otp" || lower == "totp"
            });
            ReplCommand::Unlock { totp }
        }
        "status" => ReplCommand::Status,
        "completions" => ReplCommand::Completions,
        "help" => {
            let command = rest.first().map(|s| s.to_lowercase());
            ReplCommand::Help { command }
        }
        "quit" | "exit" => ReplCommand::Quit,
        _ => ReplCommand::Unknown(command),
    }
}

/// Parse an optional type token and an optional argument from the remaining tokens.
/// If the second token is a known type, use it; otherwise treat it as the argument
/// and default to the provided default_type.
fn parse_type_and_arg(tokens: &[&str], default_type: ItemType) -> (ItemType, Option<String>) {
    if tokens.is_empty() {
        return (default_type, None);
    }

    let first_lower = tokens[0].to_lowercase();
    if let Some(item_type) = parse_item_type(&first_lower) {
        // Second token was a type, rest is the argument
        let arg = if tokens.len() > 1 {
            Some(tokens[1..].join(" "))
        } else {
            None
        };
        (item_type, arg)
    } else {
        // Not a type — treat as argument, use default type
        (default_type, Some(tokens.join(" ")))
    }
}

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

    #[test]
    fn test_empty_input() {
        assert_eq!(parse_input(""), ReplCommand::Empty);
        assert_eq!(parse_input("   "), ReplCommand::Empty);
    }

    #[test]
    fn test_quit_variants() {
        assert_eq!(parse_input("quit"), ReplCommand::Quit);
        assert_eq!(parse_input("exit"), ReplCommand::Quit);
        assert_eq!(parse_input("QUIT"), ReplCommand::Quit);
    }

    #[test]
    fn test_gen() {
        assert_eq!(parse_input("gen"), ReplCommand::Gen);
        assert_eq!(parse_input("GEN"), ReplCommand::Gen);
    }

    #[test]
    fn test_show_defaults_to_all_credentials() {
        match parse_input("show") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Credential);
                assert_eq!(grep, Some(".*".to_string()));
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_show_with_pattern() {
        match parse_input("show github") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Credential);
                assert_eq!(grep, Some("github".to_string()));
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_show_with_type() {
        match parse_input("show cards") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Payment);
                assert_eq!(grep, None);
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_show_with_type_and_pattern() {
        match parse_input("show creds github") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Credential);
                assert_eq!(grep, Some("github".to_string()));
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_show_otp() {
        match parse_input("show otp") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Totp);
                assert_eq!(grep, None);
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_show_otp_with_pattern() {
        match parse_input("show otp github") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Totp);
                assert_eq!(grep, Some("github".to_string()));
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_add_default() {
        match parse_input("add") {
            ReplCommand::Add { item_type } => {
                assert_eq!(item_type, ItemType::Credential);
            }
            _ => panic!("Expected Add command"),
        }
    }

    #[test]
    fn test_add_card() {
        match parse_input("add card") {
            ReplCommand::Add { item_type } => {
                assert_eq!(item_type, ItemType::Payment);
            }
            _ => panic!("Expected Add command"),
        }
    }

    #[test]
    fn test_add_note() {
        match parse_input("add note") {
            ReplCommand::Add { item_type } => {
                assert_eq!(item_type, ItemType::Note);
            }
            _ => panic!("Expected Add command"),
        }
    }

    #[test]
    fn test_add_otp() {
        match parse_input("add otp") {
            ReplCommand::Add { item_type } => {
                assert_eq!(item_type, ItemType::Totp);
            }
            _ => panic!("Expected Add command"),
        }
    }

    #[test]
    fn test_edit_with_pattern() {
        match parse_input("edit github") {
            ReplCommand::Edit { item_type, grep } => {
                assert_eq!(item_type, ItemType::Credential);
                assert_eq!(grep, Some("github".to_string()));
            }
            _ => panic!("Expected Edit command"),
        }
    }

    #[test]
    fn test_edit_card() {
        match parse_input("edit card") {
            ReplCommand::Edit { item_type, grep } => {
                assert_eq!(item_type, ItemType::Payment);
                assert_eq!(grep, None);
            }
            _ => panic!("Expected Edit command"),
        }
    }

    #[test]
    fn test_delete_with_pattern() {
        match parse_input("delete github") {
            ReplCommand::Delete { item_type, grep } => {
                assert_eq!(item_type, ItemType::Credential);
                assert_eq!(grep, Some("github".to_string()));
            }
            _ => panic!("Expected Delete command"),
        }
    }

    #[test]
    fn test_import_with_path() {
        match parse_input("import /path/to/file.csv") {
            ReplCommand::Import { file_path } => {
                assert_eq!(file_path, Some("/path/to/file.csv".to_string()));
            }
            _ => panic!("Expected Import command"),
        }
    }

    #[test]
    fn test_import_without_path() {
        match parse_input("import") {
            ReplCommand::Import { file_path } => {
                assert_eq!(file_path, None);
            }
            _ => panic!("Expected Import command"),
        }
    }

    #[test]
    fn test_export_with_type_and_path() {
        match parse_input("export cards cards.csv") {
            ReplCommand::Export { item_type, file_path } => {
                assert_eq!(item_type, ItemType::Payment);
                assert_eq!(file_path, Some("cards.csv".to_string()));
            }
            _ => panic!("Expected Export command"),
        }
    }

    #[test]
    fn test_export_with_path_only() {
        match parse_input("export credentials.csv") {
            ReplCommand::Export { item_type, file_path } => {
                assert_eq!(item_type, ItemType::Credential);
                assert_eq!(file_path, Some("credentials.csv".to_string()));
            }
            _ => panic!("Expected Export command"),
        }
    }

    #[test]
    fn test_lock() {
        assert_eq!(parse_input("lock"), ReplCommand::Lock);
    }

    #[test]
    fn test_unlock() {
        match parse_input("unlock") {
            ReplCommand::Unlock { totp } => assert!(!totp),
            _ => panic!("Expected Unlock command"),
        }
    }

    #[test]
    fn test_unlock_otp() {
        match parse_input("unlock otp") {
            ReplCommand::Unlock { totp } => assert!(totp),
            _ => panic!("Expected Unlock command"),
        }
    }

    #[test]
    fn test_status() {
        assert_eq!(parse_input("status"), ReplCommand::Status);
    }

    #[test]
    fn test_help_general() {
        match parse_input("help") {
            ReplCommand::Help { command } => assert_eq!(command, None),
            _ => panic!("Expected Help command"),
        }
    }

    #[test]
    fn test_help_specific() {
        match parse_input("help show") {
            ReplCommand::Help { command } => assert_eq!(command, Some("show".to_string())),
            _ => panic!("Expected Help command"),
        }
    }

    #[test]
    fn test_unknown_command() {
        match parse_input("foobar") {
            ReplCommand::Unknown(cmd) => assert_eq!(cmd, "foobar"),
            _ => panic!("Expected Unknown command"),
        }
    }

    #[test]
    fn test_completions() {
        assert_eq!(parse_input("completions"), ReplCommand::Completions);
    }

    #[test]
    fn test_completions_case_insensitive() {
        assert_eq!(parse_input("COMPLETIONS"), ReplCommand::Completions);
    }

    #[test]
    fn test_case_insensitivity() {
        match parse_input("SHOW Cards") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Payment);
                assert_eq!(grep, None);
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_type_aliases() {
        // credentials aliases
        match parse_input("show creds") {
            ReplCommand::Show { item_type, .. } => assert_eq!(item_type, ItemType::Credential),
            _ => panic!("Expected Show"),
        }
        match parse_input("show cred") {
            ReplCommand::Show { item_type, .. } => assert_eq!(item_type, ItemType::Credential),
            _ => panic!("Expected Show"),
        }
        match parse_input("show credentials") {
            ReplCommand::Show { item_type, .. } => assert_eq!(item_type, ItemType::Credential),
            _ => panic!("Expected Show"),
        }
        // payment aliases
        match parse_input("show payments") {
            ReplCommand::Show { item_type, .. } => assert_eq!(item_type, ItemType::Payment),
            _ => panic!("Expected Show"),
        }
        match parse_input("add payments") {
            ReplCommand::Add { item_type } => assert_eq!(item_type, ItemType::Payment),
            _ => panic!("Expected Add"),
        }
        // totp aliases
        match parse_input("show totp") {
            ReplCommand::Show { item_type, .. } => assert_eq!(item_type, ItemType::Totp),
            _ => panic!("Expected Show"),
        }
    }

    #[test]
    fn test_ambiguous_type_vs_argument() {
        // "github" is not a type, so it's treated as a search argument
        match parse_input("show github") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Credential);
                assert_eq!(grep, Some("github".to_string()));
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_show_notes() {
        match parse_input("show notes") {
            ReplCommand::Show { item_type, grep } => {
                assert_eq!(item_type, ItemType::Note);
                assert_eq!(grep, None);
            }
            _ => panic!("Expected Show command"),
        }
    }

    #[test]
    fn test_delete_card() {
        match parse_input("delete card") {
            ReplCommand::Delete { item_type, grep } => {
                assert_eq!(item_type, ItemType::Payment);
                assert_eq!(grep, None);
            }
            _ => panic!("Expected Delete command"),
        }
    }

    #[test]
    fn test_edit_otp() {
        match parse_input("edit otp") {
            ReplCommand::Edit { item_type, grep } => {
                assert_eq!(item_type, ItemType::Totp);
                assert_eq!(grep, None);
            }
            _ => panic!("Expected Edit command"),
        }
    }
}