this-me 0.1.7

Encrypted identity store CLI tool (this.me)
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
// this.me/crate/src/main.rs
// by suiGn

// ---- Only compile the CLI when the "sqlite" feature is enabled ----
#[cfg(feature = "sqlite")]
extern crate this_me;

#[cfg(feature = "sqlite")]
mod cli;

#[cfg(feature = "sqlite")]
use clap::Parser;

#[cfg(feature = "sqlite")]
use cli::{Cli, Commands};

#[cfg(feature = "sqlite")]
use this_me::me::{Me, MeError};

#[cfg(feature = "sqlite")]
use serde_json::{to_string_pretty, Value};

#[cfg(feature = "sqlite")]
use owo_colors::OwoColorize;
#[cfg(feature = "sqlite")]
use std::io::IsTerminal;
#[cfg(feature = "sqlite")]
use std::sync::Arc;
#[cfg(feature = "sqlite")]
use std::path::PathBuf;
#[cfg(feature = "sqlite")]
use std::fs;
#[cfg(feature = "sqlite")]
use std::time::SystemTime;
#[cfg(feature = "sqlite")]
use serde::{Serialize, Deserialize};

#[cfg(feature = "sqlite")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DigitalAsset {
    pub name: String,
    pub path: PathBuf,
    pub is_dir: bool,
    pub size: Option<u64>,
    pub modified: Option<SystemTime>,
}

#[cfg(feature = "sqlite")]
pub type AssetRef = Arc<DigitalAsset>;

#[cfg(feature = "sqlite")]
fn main() {
    let cli = Cli::parse();
    let colored_output = std::io::stdout().is_terminal();
    if colored_output {
        println!("{}", "
▄ ▄▄▄▄  ▗▞▀▚▖
  █ █ █ ▐▛▀▀▘
  █   █ ▝▚▄▄▖
             ".bright_green().bold());
    } else {
        println!(".me CLI.");
    }

    // If no subcommand/arguments, show styled multi-line header and list users
    if cli.command.is_none() {
        use owo_colors::OwoColorize;
        use std::fs;
        // Print ASCII logo block as before
        println!(
            "{}",
            "
   ┓   ┏┓
â”“â”â”â”Ŗâ”“â”â”“â”â”›
┗â”ģ┛┛┗┗┛â€ĸ
         "
                .bright_white()
                .bold()
        );

        // Read ~/.this/me and list user directories in a pretty table
        let base_path = dirs::home_dir().map(|p| p.join(".this/me"));
        let mut users: Vec<String> = Vec::new();
        if let Some(path) = base_path {
            if let Ok(entries) = fs::read_dir(&path) {
                for entry in entries.flatten() {
                    let dir_path = entry.path();
                    if dir_path.is_dir() {
                        if let Some(name) = entry.file_name().to_str() {
                            users.push(name.to_string());
                        }
                    }
                }
            }
        }
        if users.is_empty() {
            if colored_output {
                println!("{}", "📭 No identities found.".bright_black());
                println!("{}", "To create one:".bright_black());
                println!("{}", "me create --username <name> --password <pw>".bright_black());
                println!("{}", "See --help for more.".bright_black());
            } else {
                println!("📭 No identities found.");
                println!("To create one:");
                println!("me create --username <name> --password <pw>");
                println!("See --help for more.");
            }
        } else {
            // Calculate column width for the single "user" column
            let username_col_width = users
                .iter()
                .map(|name| name.len())
                .max()
                .unwrap_or(8)
                .max("user".len());
            // Print table header (single column)
            let top = format!(
                "┌{:─<w1$}┐",
                "",
                w1 = username_col_width
            );
            let header = format!(
                "│{:<w1$}│",
                "user".bright_white().bold(),
                w1 = username_col_width
            );
            let sep = format!(
                "├{:─<w1$}┤",
                "",
                w1 = username_col_width
            );
            let bottom = format!(
                "└{:─<w1$}┘",
                "",
                w1 = username_col_width
            );
            println!("{}", top);
            for name in &users {
                println!(
                    "│{:<w1$}│",
                    name.bright_cyan(),
                    w1 = username_col_width
                );
            }
            println!("{}", bottom);
            println!();
            println!(
                "{}",
                format!("{} user(s) found.", users.len()).bright_black()
            );
        }
        // Instructional message (always show after the users table or empty message)
        println!();
        println!("{}", "me --username example --password example verb predicate.".bright_black());
        println!("{}", "me create --username <name> --password <pw>".bright_black());
        println!("{}", "me help".bright_black());
        return;
    }

    match cli.command {
        Some(Commands::Create {}) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::new(&username, &password) {
                Ok(_) => println!("✅ Identity '{}' created.", username),
                Err(MeError::Io(ref err)) if err.kind() == std::io::ErrorKind::AlreadyExists => {
                    eprintln!("âš ī¸ Identity '{}' already exists.", username);
                }
                Err(e) => eprintln!("❌ Failed to create identity '{}': {}", username, e),
            }
        }
        Some(Commands::ChangePassword { old_password, new_password }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => match me.change_password(&old_password, &new_password) {
                    Ok(_) => println!("🔐 Password for '{}' changed successfully.", username),
                    Err(e) => eprintln!("❌ Failed to change password for '{}': {}", username, e),
                },
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::View { target }) => {
            if target.is_empty() {
                use std::env;
                let path = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
                println!(
                    "\n{}",
                    format!("👀 View: {}/", path.display()).bright_white().bold()
                );
                let mut assets: Vec<AssetRef> = Vec::new();
                match fs::read_dir(&path) {
                    Ok(entries) => {
                        for entry in entries.flatten() {
                            let meta = entry.metadata().ok();
                            let asset = Arc::new(DigitalAsset {
                                name: entry.file_name().to_string_lossy().to_string(),
                                path: entry.path(),
                                is_dir: meta.as_ref().map(|m| m.is_dir()).unwrap_or(false),
                                size: meta.as_ref().and_then(|m| if m.is_file() { Some(m.len()) } else { None }),
                                modified: meta.and_then(|m| m.modified().ok()),
                            });
                            assets.push(asset);
                        }
                        for asset in &assets {
                            println!("â€ĸ {}", asset.name);
                        }
                        println!("({} assets found)", assets.len());
                    }
                    Err(err) => eprintln!("❌ Failed to read directory: {}", err),
                }
                println!();
                return;
            }
            let object = target.join(" ");
            match serde_json::from_str::<Value>(&object) {
                Ok(val) => {
                    println!("\n{}", "👀 View:".bright_white().bold());
                    println!("{}", to_string_pretty(&val).unwrap_or(object));
                    println!();
                }
                Err(_) => {
                    println!("\n{}", "👀 View:".bright_white().bold());
                    println!("{}", object);
                    println!();
                }
            }
        }
        Some(Commands::Link { path }) => {
            let joined = path.join("/");
            println!("🔗 Linking to: {}", joined);
            // Save or update current .me context
            if let Err(err) = std::fs::write("/tmp/.me_context", &joined) {
                eprintln!("❌ Failed to save context: {}", err);
            } else {
                println!("✅ Context updated to '{}'.", joined);
            }
        }
        Some(Commands::Users) => {
            use std::fs;
            use owo_colors::OwoColorize;
            let base_path = dirs::home_dir()
                .map(|p| p.join(".this/me"))
                .expect("❌ Could not determine home directory.");
            match fs::read_dir(&base_path) {
                Ok(entries) => {
                    let mut users: Vec<String> = Vec::new();
                    for entry in entries.flatten() {
                        let path = entry.path();
                        if path.is_dir() {
                            if let Some(name) = entry.file_name().to_str() {
                                users.push(name.to_string());
                            }
                        }
                    }
                    if users.is_empty() {
                        println!("📭 No identities found.");
                        return;
                    }
                    // Calculate column width for the single "user" column
                    let username_col_width = users
                        .iter()
                        .map(|name| name.len())
                        .max()
                        .unwrap_or(8)
                        .max("user".len());
                    // Print table header (single column)
                    let top = format!(
                        "┌{:─<w1$}┐",
                        "",
                        w1 = username_col_width
                    );
                    let header = format!(
                        "│{:<w1$}│",
                        "user".bright_white().bold(),
                        w1 = username_col_width
                    );
                    let sep = format!(
                        "├{:─<w1$}┤",
                        "",
                        w1 = username_col_width
                    );
                    let bottom = format!(
                        "└{:─<w1$}┘",
                        "",
                        w1 = username_col_width
                    );
                    println!("đŸ‘Ĩ Found users:");
                    println!("{}", top);
                    println!("{}", header);
                    println!("{}", sep);
                    for name in &users {
                        println!(
                            "│{:<w1$}│",
                            name.bright_cyan(),
                            w1 = username_col_width
                        );
                    }
                    println!("{}", bottom);
                    println!();
                    println!(
                        "{}",
                        format!("{} user(s) found.", users.len()).bright_black()
                    );
                }
                Err(_) => println!("📭 No identities found."),
            }
        }
        Some(Commands::UsersMe) => {
            use std::fs;
            use owo_colors::OwoColorize;
            let base_path = dirs::home_dir()
                .map(|p| p.join(".this/me"))
                .expect("❌ Could not determine home directory.");
            match fs::read_dir(&base_path) {
                Ok(entries) => {
                    let mut users: Vec<String> = Vec::new();
                    for entry in entries.flatten() {
                        let dir_path = entry.path();
                        if dir_path.is_dir() {
                            if let Some(name) = entry.file_name().to_str() {
                                users.push(name.to_string());
                            }
                        }
                    }
                    if users.is_empty() {
                        println!("📭 No identities found.");
                        return;
                    }
                    // Calculate column width for the single "user" column
                    let username_col_width = users
                        .iter()
                        .map(|name| name.len())
                        .max()
                        .unwrap_or(8)
                        .max("user".len());
                    // Print table header (single column)
                    let top = format!(
                        "┌{:─<w1$}┐",
                        "",
                        w1 = username_col_width
                    );
                    let header = format!(
                        "│{:<w1$}│",
                        "user".bright_white().bold(),
                        w1 = username_col_width
                    );
                    let sep = format!(
                        "├{:─<w1$}┤",
                        "",
                        w1 = username_col_width
                    );
                    let bottom = format!(
                        "└{:─<w1$}┘",
                        "",
                        w1 = username_col_width
                    );
                    println!("đŸ‘Ĩ .me users:");
                    println!("{}", top);
                    println!("{}", header);
                    println!("{}", sep);
                    for name in &users {
                        println!(
                            "│{:<w1$}│",
                            name.bright_cyan(),
                            w1 = username_col_width
                        );
                    }
                    println!("{}", bottom);
                    println!();
                    println!(
                        "{}",
                        format!("{} user(s) found.", users.len()).bright_black()
                    );
                }
                Err(_) => println!("📭 No identities found."),
            }
        }
        Some(Commands::Be { value, context, key }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => {
                    let ctx = context.clone().unwrap_or_else(|| me.context_id.clone());
                    let key_str = key.as_deref().unwrap_or("_");
                    match me.be(&ctx, key_str, &value) {
                        Ok(_) => println!("✅ be('{}', '{}') applied successfully.", key_str, value),
                        Err(e) => eprintln!("❌ Failed to apply be('{}', '{}'): {}", key_str, value, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::Communicate { value, context, key }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => {
                    let ctx = context.clone().unwrap_or_else(|| me.context_id.clone());
                    let key_str = key.as_deref().unwrap_or("_");
                    match me.communicate(&ctx, key_str, &value) {
                        Ok(_) => println!("✅ communicate('{}', '{}') applied successfully.", key_str, value),
                        Err(e) => eprintln!("❌ Failed to apply communicate('{}', '{}'): {}", key_str, value, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::Do { value, context, key }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => {
                    let ctx = context.clone().unwrap_or_else(|| me.context_id.clone());
                    let key_str = key.as_deref().unwrap_or("_");
                    match me.do_(&ctx, key_str, &value) {
                        Ok(_) => println!("✅ do('{}', '{}') applied successfully.", key_str, value),
                        Err(e) => eprintln!("❌ Failed to apply do('{}', '{}'): {}", key_str, value, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::Have { value, context, key }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => {
                    let ctx = context.clone().unwrap_or_else(|| me.context_id.clone());
                    let key_str = key.as_deref().unwrap_or("_");
                    match me.have(&ctx, key_str, &value) {
                        Ok(_) => println!("✅ have('{}', '{}') applied successfully.", key_str, value),
                        Err(e) => eprintln!("❌ Failed to apply have('{}', '{}'): {}", key_str, value, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::At { value, context, key }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => {
                    let ctx = context.clone().unwrap_or_else(|| me.context_id.clone());
                    let key_str = key.as_deref().unwrap_or("_");
                    match me.at(&ctx, key_str, &value) {
                        Ok(_) => println!("✅ at('{}', '{}') applied successfully.", key_str, value),
                        Err(e) => eprintln!("❌ Failed to apply at('{}', '{}'): {}", key_str, value, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::Relate { value, context, key }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => {
                    let ctx = context.clone().unwrap_or_else(|| me.context_id.clone());
                    let key_str = key.as_deref().unwrap_or("_");
                    match me.relate(&ctx, key_str, &value) {
                        Ok(_) => println!("✅ relate('{}', '{}') applied successfully.", key_str, value),
                        Err(e) => eprintln!("❌ Failed to apply relate('{}', '{}'): {}", key_str, value, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::React { value, context, key }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(mut me) => {
                    let ctx = context.clone().unwrap_or_else(|| me.context_id.clone());
                    let key_str = key.as_deref().unwrap_or("_");
                    match me.react(&ctx, key_str, &value) {
                        Ok(_) => println!("✅ react('{}', '{}') applied successfully.", key_str, value),
                        Err(e) => eprintln!("❌ Failed to apply react('{}', '{}'): {}", key_str, value, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::Get { verb, context, key, value, limit, offset, since, until }) => {
            let username = cli.username.as_ref().expect("Missing username");
            let password = cli.password.as_ref().expect("Missing password");
            match Me::load(&username, &password) {
                Ok(me) => {
                    match me.get(
                        &verb,
                        context.as_deref(),
                        key.as_deref(),
                        value.as_deref(),
                        None,
                        limit,
                        offset,
                        since.as_deref(),
                        until.as_deref(),
                    ) {
                        Ok(results) => {
                            println!("đŸ“Ļ Retrieved {} entries:", verb);
                            if verb == "all" {
                                println!("â„šī¸  Note: '--limit' applies *per verb*.");
                            }
                            for (vrb, f, v, ts) in &results {
                                println!("â€ĸ {}('{}', '{}') @ {}", vrb, f, v, ts);
                            }
                            if let (Some(start), Some(end)) = (results.last(), results.first()) {
                                println!("âŗ Showing {} results from {} to {}", results.len(), start.3, end.3);
                            }
                        }
                        Err(e) => eprintln!("❌ Failed to get '{}': {}", verb, e),
                    }
                }
                Err(e) => eprintln!("❌ Failed to load identity '{}': {}", username, e),
            }
        }
        Some(Commands::Host) => {
            if let Err(err) = this_me::host::print_host_summary() {
                eprintln!("❌ Failed to inspect host: {}", err);
            }
        }
        Some(Commands::HostIp) => {
            if let Err(err) = this_me::host::print_network_summary() {
                eprintln!("❌ Failed to inspect network interfaces: {}", err);
            }
        }
        Some(Commands::Who) => {
            use owo_colors::OwoColorize;
            use std::fs;
            let base_path = dirs::home_dir()
                .map(|p| p.join(".this/me"))
                .expect("❌ Could not determine home directory.");
            println!("{}", ".me who?".bright_green().bold());
            println!("{}", "đŸ‘Ĩ Found users:".bright_white().bold());
            match fs::read_dir(&base_path) {
                Ok(entries) => {
                    let mut found = false;
                    for entry in entries.flatten() {
                        if entry.path().is_dir() {
                            if let Some(name) = entry.file_name().to_str() {
                                println!("â€ĸ {}", name);
                                found = true;
                            }
                        }
                    }
                    if !found {
                        println!("📭 No identities found.");
                    }
                    // Instructional print section
                    println!();
                    println!("{}", "To create a new .me identity, run:".bright_black());
                    println!("{}", "  this.me create --username <name> --password <pw>".bright_black());
                }
                Err(_) => println!("📭 No identities found."),
            }
        }
        None => {}
    }
}

#[cfg(not(feature = "sqlite"))]
fn main() {
    eprintln!("This binary requires the 'sqlite' feature enabled.");
}