Skip to main content

ows_lib/
migrate.rs

1use std::path::PathBuf;
2
3/// Migrate the vault directory from `~/.lws` to `~/.ows` if needed.
4///
5/// This is a one-time upgrade path for users who installed `lws` before the
6/// rename to `ows`. It also updates shell RC files to point PATH at `.ows/bin`.
7pub fn migrate_vault_if_needed() {
8    let Some(home) = std::env::var("HOME").ok() else {
9        return;
10    };
11
12    let old_dir = PathBuf::from(&home).join(".lws");
13    let new_dir = PathBuf::from(&home).join(".ows");
14
15    if old_dir.exists() && !new_dir.exists() {
16        // Attempt atomic rename (same filesystem)
17        if let Err(e) = std::fs::rename(&old_dir, &new_dir) {
18            eprintln!(
19                "warning: failed to migrate {} to {}: {e}",
20                old_dir.display(),
21                new_dir.display()
22            );
23            return;
24        }
25
26        // Update vault_path in config.json if present
27        let config_path = new_dir.join("config.json");
28        if config_path.exists() {
29            if let Ok(contents) = std::fs::read_to_string(&config_path) {
30                let updated = contents.replace(".lws", ".ows");
31                let _ = std::fs::write(&config_path, updated);
32            }
33        }
34
35        // Update PATH in shell rc files
36        let rc_files = [
37            PathBuf::from(&home).join(".zshrc"),
38            PathBuf::from(&home).join(".bashrc"),
39            PathBuf::from(&home).join(".bash_profile"),
40            PathBuf::from(&home).join(".config/fish/config.fish"),
41        ];
42
43        for rc in &rc_files {
44            if rc.exists() {
45                if let Ok(contents) = std::fs::read_to_string(rc) {
46                    if contents.contains(".lws/bin") {
47                        let updated = contents.replace(".lws/bin", ".ows/bin");
48                        let _ = std::fs::write(rc, updated);
49                    }
50                }
51            }
52        }
53
54        eprintln!("Migrated wallet vault from ~/.lws to ~/.ows");
55    } else if old_dir.exists() && new_dir.exists() {
56        eprintln!(
57            "warning: Both ~/.lws and ~/.ows exist. Using ~/.ows. Remove ~/.lws manually if no longer needed."
58        );
59    }
60}