pwr-cli 1.0.0

pwr: Lazy project archiver — client CLI and TUI for the pwr-server daemon
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! pwr — Lazy Project Archiver (client CLI).
//!
//! Archives projects to a pwr-server daemon on a NAS and restores
//! them on demand. Supports CLI mode and an optional TUI.

mod client;

use std::path::PathBuf;

use clap::{Parser, Subcommand};

/// pwr — Lazy Project Archiver
///
/// Move projects between your local machine and a NAS running pwr-server.
/// Archives projects you're not using and restores them on demand, with
/// transparent encryption and integrity verification.
#[derive(Parser)]
#[command(name = "pwr", version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize pwr configuration
    Init {
        /// Server hostname or IP
        #[arg(long)]
        server_host: Option<String>,

        /// Server port (default: 9742)
        #[arg(long)]
        server_port: Option<u16>,

        /// Hex-encoded pre-shared key
        #[arg(long)]
        psk: Option<String>,

        /// Local root directory for projects
        #[arg(long)]
        local_root: Option<String>,
    },

    /// Create a .project.toml to start tracking a project
    Create {
        /// Path to the project directory (defaults to current directory)
        path: Option<PathBuf>,

        /// Project name (defaults to directory name)
        #[arg(long)]
        name: Option<String>,
    },

    /// Archive a project to the server
    Archive {
        /// Path to the project directory
        path: PathBuf,

        /// Dry run — show what would happen without doing it
        #[arg(long, short = 'n')]
        dry_run: bool,
    },

    /// Restore a project from the server
    Restore {
        /// Path to the project directory (or placeholder)
        path: PathBuf,

        /// Dry run — show what would happen without doing it
        #[arg(long, short = 'n')]
        dry_run: bool,
    },

    /// Ensure a project is local (for shell wrapper integration)
    Ensure {
        /// Path to the project directory
        path: PathBuf,

        /// Suppress output
        #[arg(long, short = 'q')]
        quiet: bool,
    },

    /// Show status of all tracked projects
    Status {
        /// Search recursively for projects
        #[arg(long, short = 'r')]
        recursive: bool,
    },

    /// List all tracked projects with paths
    List {
        /// Search recursively for projects
        #[arg(long, short = 'r')]
        recursive: bool,
    },

    /// Generate shell integration script
    Shell {
        /// Shell: bash, zsh, or fish
        #[arg(default_value = "bash")]
        shell: String,

        /// Print initialization instructions
        #[arg(long)]
        init: bool,
    },

    /// Show transaction history
    Log {
        /// Filter by project name
        project: Option<String>,

        /// Show error details
        #[arg(long, short = 'e')]
        errors: bool,
    },

    /// Launch the terminal UI for interactive project management
    #[cfg(feature = "tui")]
    Tui,
}

fn main() {
    // Install the rustls crypto provider before any TLS code runs.
    // Required because the pwr-core → pwr-cli dependency chain pulls in
    // both ring and aws-lc-rs, which prevents rustls from auto-detecting.
    rustls::crypto::ring::default_provider()
        .install_default()
        .expect("Failed to install rustls ring crypto provider");

    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn"))
        .format_timestamp(None)
        .init();

    let cli = Cli::parse();

    let result = match cli.command {
        Commands::Init {
            server_host,
            server_port,
            psk,
            local_root,
        } => cmd_init(server_host, server_port, psk, local_root),

        Commands::Create { path, name } => cmd_create(path, name),

        Commands::Archive { path, dry_run } => cmd_archive(path, dry_run),

        Commands::Restore { path, dry_run } => cmd_restore(path, dry_run),

        Commands::Ensure { path, quiet } => cmd_ensure(path, quiet),

        Commands::Status { recursive } => cmd_status(recursive),

        Commands::List { recursive } => cmd_list(recursive),

        Commands::Shell { shell, init } => {
            if init {
                cmd_shell_init(&shell)
            } else {
                cmd_shell(&shell)
            }
        }

        Commands::Log { project, errors } => cmd_log(project, errors),

        #[cfg(feature = "tui")]
        Commands::Tui => cmd_tui(),
    };

    if let Err(err) = result {
        eprintln!("Error: {}", err);
        std::process::exit(1);
    }
}

// ---------------------------------------------------------------------------
// TUI entry point
// ---------------------------------------------------------------------------

#[cfg(feature = "tui")]
mod tui;

#[cfg(feature = "tui")]
fn cmd_tui() -> Result<(), String> {
    tui::run()
}

// ---------------------------------------------------------------------------
// Command implementations
// ---------------------------------------------------------------------------

fn cmd_init(
    server_host: Option<String>,
    server_port: Option<u16>,
    psk: Option<String>,
    local_root: Option<String>,
) -> Result<(), String> {
    use pwr_core::config::{self, PwrConfig};

    if config::config_exists() {
        println!("Config already exists at {}", config::config_path().display());
        return Ok(());
    }

    let host = server_host.unwrap_or_else(|| "nas".to_string());
    let port = server_port.unwrap_or(9742);
    let key = psk.unwrap_or_else(|| {
        let psk = pwr_core::crypto::generate_psk();
        pwr_core::crypto::psk_to_hex(&psk)
    });
    let root = local_root.unwrap_or_else(|| {
        let home = std::env::var("HOME").unwrap_or_else(|_| "/home/jacob".to_string());
        format!("{}/Projects", home)
    });

    let config = PwrConfig::new(host.clone(), port, key.clone(), root.clone());
    config::save_config(&config).map_err(|e| format!("{}", e))?;

    println!("Configuration saved to {}", config::config_path().display());
    println!("  Server: {}:{}", host, port);
    println!("  PSK: {} (save this for the server config)", key);
    println!("  Local root: {}", root);

    // Try to generate age identity
    match pwr_core::crypto::generate_age_identity() {
        Ok((_, pk)) => println!("  Age public key: {}", pk),
        Err(e) => println!("  Warning: could not generate age identity: {}", e),
    }

    Ok(())
}

fn cmd_create(path: Option<PathBuf>, name: Option<String>) -> Result<(), String> {
    use pwr_core::config::load_config;
    use pwr_core::project;

    let config = load_config().map_err(|e| format!("Load config: {}", e))?;

    // Determine the project directory
    let abs_path = if let Some(p) = path {
        p.canonicalize().map_err(|e| format!("Cannot resolve path: {}", e))?
    } else {
        std::env::current_dir().map_err(|e| format!("Current dir: {}", e))?
    };

    if !abs_path.is_dir() {
        return Err(format!("Not a directory: {}", abs_path.display()));
    }

    // Check if already tracked
    if project::is_tracked(&abs_path) {
        println!(
            "Project is already tracked: {}",
            abs_path.join(project::PROJECT_FILE).display()
        );
        return Ok(());
    }

    // Determine project name
    let project_name = name.unwrap_or_else(|| {
        abs_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unnamed")
            .to_string()
    });

    let remote_path = format!(
        "{}:{}/{}",
        config.server_addr(),
        config.server_host,
        project_name
    );

    let meta = pwr_core::metadata::ProjectMeta::new_local(
        project_name.clone(),
        abs_path.to_string_lossy().to_string(),
        remote_path,
    );

    project::write_project_file(&abs_path, &meta)
        .map_err(|e| format!("{}", e))?;

    println!("Created project '{}'", project_name);
    println!("  Path:  {}", abs_path.display());
    println!("  UUID:  {}", meta.uuid);
    println!("  State: local");
    println!();
    println!("To archive this project:");
    println!("  pwr archive {}", abs_path.display());

    Ok(())
}

fn cmd_archive(path: PathBuf, dry_run: bool) -> Result<(), String> {
    use pwr_core::config::load_config;
    use pwr_core::project;

    let config = load_config().map_err(|e| format!("{}", e))?;

    let abs_path = path
        .canonicalize()
        .map_err(|e| format!("Cannot resolve path: {}", e))?;

    if !abs_path.is_dir() {
        return Err(format!("Not a directory: {}", abs_path.display()));
    }

    let project_name = abs_path
        .file_name()
        .and_then(|n| n.to_str())
        .ok_or("Cannot determine project name")?;

    let mut meta = if let Some(existing) =
        project::read_project_file(&abs_path).map_err(|e| format!("{}", e))?
    {
        println!("Found existing project: {}", existing.name);
        existing
    } else {
        let remote_path = format!(
            "{}:{}/{}",
            config.server_addr(),
            config.server_host,
            project_name
        );
        pwr_core::metadata::ProjectMeta::new_local(
            project_name.to_string(),
            abs_path.to_string_lossy().to_string(),
            remote_path,
        )
    };

    let size = project::dir_size(&abs_path).map_err(|e| format!("{}", e))?;
    println!(
        "Archiving '{}' ({} bytes)",
        project_name,
        pwr_core::metadata::human_size(size)
    );

    if dry_run {
        println!("[DRY RUN] Would upload then remove local files");
        return Ok(());
    }

    // Load age identity for encryption
    let identity = pwr_core::crypto::load_age_identity()
        .map_err(|e| format!("Cannot load age identity: {}", e))?;
    let public_key = identity.to_public().to_string();

    // Create encrypted archive
    println!("Creating encrypted archive...");
    let (encrypted, hash) =
        pwr_core::archive::create_archive(&abs_path, &public_key)
            .map_err(|e| format!("Archive creation failed: {}", e))?;

    println!(
        "Encrypted archive: {} bytes (SHA-256: {})",
        pwr_core::metadata::human_size(encrypted.len() as u64),
        &hash[..16]
    );

    // Connect to server and upload with progress bar
    println!("Connecting to {}...", config.server_addr());
    let mut client = client::PwrClient::connect(&config, config.use_tls)
        .map_err(|e| format!("Connection failed: {}", e))?;

    let total = encrypted.len() as u64;
    let pb = indicatif::ProgressBar::new(total);
    pb.set_style(
        indicatif::ProgressStyle::default_bar()
            .template("Uploading {spinner:.green} [{bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
            .unwrap()
            .progress_chars("#>-"),
    );

    let pb_ref = &pb;
    client
        .archive_project_with_progress(
            &meta.uuid, project_name, &encrypted, &hash,
            Some(&|sent, _total| { pb_ref.set_position(sent); }),
        )
        .map_err(|e| format!("Archive failed: {}", e))?;
    pb.finish_and_clear();

    // Update local metadata and clean up
    let file_count = project::file_count(&abs_path).map_err(|e| format!("{}", e))?;
    meta.mark_archived(encrypted.len() as u64, file_count, true);
    project::write_project_file(&abs_path, &meta).map_err(|e| format!("{}", e))?;
    project::remove_dir_contents_except_project(&abs_path)
        .map_err(|e| format!("{}", e))?;

    println!("Archived '{}' successfully", project_name);
    println!(
        "  {} freed locally",
        pwr_core::metadata::human_size(size)
    );

    Ok(())
}

fn cmd_restore(path: PathBuf, dry_run: bool) -> Result<(), String> {
    use pwr_core::config::load_config;
    use pwr_core::project;

    let config = load_config().map_err(|e| format!("{}", e))?;

    let abs_path = path
        .canonicalize()
        .unwrap_or_else(|_| path.clone());

    if !project::is_archived_placeholder(&abs_path) {
        if abs_path.is_dir() && project::is_local_project(&abs_path) {
            println!("'{}' is already local", abs_path.display());
            return Ok(());
        }
        return Err(format!(
            "Not an archived project: {}",
            abs_path.display()
        ));
    }

    let meta = project::read_project_file(&abs_path)
        .map_err(|e| format!("{}", e))?
        .ok_or("No .project.toml found")?;

    println!("Restoring '{}' ({} bytes)...", meta.name, meta.size_human());

    if dry_run {
        println!("[DRY RUN] Would download and extract");
        return Ok(());
    }

    // Connect and download with progress bar
    let mut client = client::PwrClient::connect(&config, config.use_tls)
        .map_err(|e| format!("Connection failed: {}", e))?;

    let pb = indicatif::ProgressBar::new(meta.size_bytes);
    pb.set_style(
        indicatif::ProgressStyle::default_bar()
            .template("Downloading {spinner:.green} [{bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
            .unwrap()
            .progress_chars("#>-"),
    );

    let pb_ref = &pb;
    let encrypted = client
        .restore_project_with_progress(
            &meta.uuid,
            Some(&|received, _total| { pb_ref.set_position(received); }),
        )
        .map_err(|e| format!("Restore failed: {}", e))?;
    pb.finish_and_clear();

    // Decrypt and extract
    println!("Decrypting and extracting...");
    let identity = pwr_core::crypto::load_age_identity()
        .map_err(|e| format!("Cannot load age identity: {}", e))?;

    let hash = pwr_core::crypto::sha256_hex(&encrypted);
    pwr_core::archive::extract_archive(&encrypted, &identity, &abs_path, &hash)
        .map_err(|e| format!("Extraction failed: {}", e))?;

    // Update metadata
    let mut updated = meta.clone();
    let new_size = project::dir_size(&abs_path).map_err(|e| format!("{}", e))?;
    let new_count = project::file_count(&abs_path).map_err(|e| format!("{}", e))?;
    updated.mark_local(new_size, new_count);
    project::write_project_file(&abs_path, &updated)
        .map_err(|e| format!("{}", e))?;

    println!("Restored '{}' successfully", meta.name);
    Ok(())
}

fn cmd_ensure(path: PathBuf, quiet: bool) -> Result<(), String> {
    use pwr_core::project;

    let abs_path = if path.is_absolute() {
        path.clone()
    } else {
        std::env::current_dir()
            .map_err(|e| format!("{}", e))?
            .join(&path)
    };

    if abs_path.is_dir() && !project::is_archived_placeholder(&abs_path) {
        return Ok(()); // Already local
    }

    if project::is_archived_placeholder(&abs_path) {
        if !quiet {
            println!("Project archived. Restoring...");
        }
        return cmd_restore(abs_path, false);
    }

    Err(format!("No such project: {}", path.display()))
}

fn cmd_status(recursive: bool) -> Result<(), String> {
    use pwr_core::config::load_config;
    use pwr_core::project;

    let config = load_config().map_err(|e| format!("{}", e))?;
    let root = PathBuf::from(&config.local_root);

    if !root.is_dir() {
        println!("Local root '{}' does not exist.", root.display());
        return Ok(());
    }

    let projects = if recursive {
        project::find_projects_recursive(&root)
    } else {
        project::find_projects(&root)
    }
    .map_err(|e| format!("{}", e))?;

    if projects.is_empty() {
        println!("No tracked projects found.");
        return Ok(());
    }

    println!("{:<10} {:<20} {:<12} {:<12}", "STATUS", "NAME", "SIZE", "LAST SYNC");
    println!("{}", "-".repeat(60));

    for (_path, meta) in &projects {
        let status = if meta.is_archived() { "archived" } else { "local" };
        let last_sync = meta.last_sync.format("%Y-%m-%d").to_string();
        println!(
            "{:<10} {:<20} {:<12} {:<12}",
            status,
            meta.name,
            meta.size_human(),
            last_sync,
        );
    }

    let local = projects.iter().filter(|(_, m)| m.is_local()).count();
    let archived = projects.iter().filter(|(_, m)| m.is_archived()).count();
    println!(
        "\n{} local, {} archived ({} total)",
        local, archived, projects.len()
    );

    Ok(())
}

fn cmd_list(recursive: bool) -> Result<(), String> {
    cmd_status(recursive) // Same output format
}

fn cmd_shell(shell: &str) -> Result<(), String> {
    let script = match shell {
        "bash" => include_str!("../shell/pwr.bash"),
        "zsh" => include_str!("../shell/pwr.zsh"),
        "fish" => include_str!("../shell/pwr.fish"),
        _ => return Err(format!("Unsupported shell: {}", shell)),
    };
    println!("{}", script);
    Ok(())
}

fn cmd_shell_init(shell: &str) -> Result<(), String> {
    let home = std::env::var("HOME").unwrap_or_default();
    let rc_file = match shell {
        "bash" => format!("{}/.bashrc", home),
        "zsh" => format!("{}/.zshrc", home),
        "fish" => format!("{}/.config/fish/config.fish", home),
        _ => return Err(format!("Unsupported shell: {}", shell)),
    };
    println!("Add this to {}:\n", rc_file);
    println!("eval \"$(pwr shell {})\"", shell);
    Ok(())
}

fn cmd_log(project: Option<String>, show_errors: bool) -> Result<(), String> {
    use pwr_core::transaction;

    let transactions = transaction::read_transactions()
        .map_err(|e| format!("{}", e))?;

    let filtered: Vec<_> = if let Some(ref name) = project {
        transactions.into_iter().filter(|t| t.project_name == *name).collect()
    } else {
        transactions
    };

    if filtered.is_empty() {
        println!("No transactions found.");
        return Ok(());
    }

    for tx in &filtered {
        let status = match tx.status {
            pwr_core::transaction::TransactionStatus::Completed => "OK",
            pwr_core::transaction::TransactionStatus::Failed => "FAILED",
            pwr_core::transaction::TransactionStatus::Started => "INCOMPLETE",
        };
        println!(
            "{} {:8} {:20} {:>10} {:>8}",
            tx.timestamp.format("%Y-%m-%d %H:%M"),
            format!("{:?}", tx.operation).to_lowercase(),
            tx.project_name,
            pwr_core::metadata::human_size(tx.size_bytes),
            status,
        );
        if show_errors {
            if let Some(ref err) = tx.error {
                println!("  Error: {}", err);
            }
        }
    }

    Ok(())
}