clync 0.4.0

Encrypted sync for Claude Code across machines
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
use anyhow::{Context, Result, bail};
use std::path::PathBuf;

use crate::config::{self, Config, EncryptionConfig, StorageConfig, SyncConfig};
use crate::crypto::Cipher;
use crate::io::InputSource;
use crate::scanner::ScanFilter;
use crate::store::git::GitStore;
use crate::{extras, manifest, memories, repo_meta, sync};

pub fn cmd_init(
    repo: Option<PathBuf>,
    op_ref: Option<String>,
    no_encrypt: bool,
    storage_type: &str,
    input: &dyn InputSource,
) -> Result<()> {
    let config_path = Config::config_path()?;
    if config_path.exists() {
        bail!(
            "config already exists at {}. Remove it to reinitialize.",
            config_path.display()
        );
    }

    let interactive = repo.is_none() && op_ref.is_none() && !no_encrypt && storage_type == "git";

    if interactive {
        return cmd_init_interactive(input);
    }

    let repo = repo.unwrap_or_else(|| {
        config::home_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join(".clync")
            .join("data")
    });

    let enc_override = if no_encrypt {
        Some(EncryptionConfig::None)
    } else {
        None
    };

    let storage = match storage_type {
        "folder" => StorageConfig::Folder { path: repo },
        #[cfg(feature = "s3")]
        "s3" => {
            bail!(
                "S3 storage requires configuration fields (bucket, region, etc.) that cannot be set via CLI flags. Use interactive init or edit config.toml directly."
            );
        }
        #[cfg(not(feature = "s3"))]
        "s3" => {
            bail!("S3 storage is not available. Rebuild with: cargo install clync --features s3");
        }
        "git" => StorageConfig::Git {
            path: repo,
            auto_push: true,
            lfs_threshold: config::default_lfs_threshold(),
        },
        other => {
            bail!("unknown storage type: {other}. Valid options: git, folder, s3");
        }
    };

    init_with_options(storage, op_ref, enc_override, Default::default())
}

fn cmd_init_interactive(input: &dyn InputSource) -> Result<()> {
    println!("clync setup\n");

    println!("storage backend:");
    println!("  1) git (default, syncs via git remote)");
    println!("  2) folder (local/network folder, NAS, Dropbox, USB)");
    let storage_choice = input.prompt_with_default("choice [1-2]", "1")?;

    let default_repo = config::home_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join(".clync")
        .join("data");
    let repo = input.prompt_with_default("sync path", &default_repo.to_string_lossy())?;
    let repo = config::expand_path(&PathBuf::from(&repo));

    let is_folder = storage_choice.trim() == "2";

    println!("\nencryption:");
    println!("  1) local key file (default, no dependencies)");
    println!("  2) passphrase (from env var, no key file needed)");
    println!("  3) 1Password CLI (op://)");
    println!("  4) Bitwarden CLI (bw)");
    println!("  5) pass (Unix password manager)");
    println!("  6) none (plain text, use private repo)");
    let enc_choice = input.prompt_with_default("choice [1-6]", "1")?;

    let (op_ref, enc_override) = match enc_choice.trim() {
        "2" => {
            let env_var =
                input.prompt_with_default("env var name for passphrase", "CLYNC_PASSPHRASE")?;
            println!("set {env_var} in your shell before running clync");
            (None, Some(EncryptionConfig::Passphrase { env_var }))
        }
        "3" => {
            let reference = input
                .prompt_with_default("1Password reference", "op://Personal/clync/age-secret-key")?;
            (Some(reference), None)
        }
        "4" => {
            let item_id = input.prompt("Bitwarden item ID or name")?;
            let field = input.prompt_with_default("field name", "notes")?;
            (None, Some(EncryptionConfig::Bitwarden { item_id, field }))
        }
        "5" => {
            let entry = input.prompt_with_default("pass entry path", "clync/age-key")?;
            (None, Some(EncryptionConfig::Pass { entry }))
        }
        "6" => {
            println!("no encryption - make sure your sync repo is private");
            (None, Some(EncryptionConfig::None))
        }
        _ => (None, None),
    };

    println!("\nwhat to sync (all on by default, disable what you don't want):");
    let settings = input.prompt_yn("  sync settings.json?", true)?;
    let commands = input.prompt_yn("  sync custom commands?", true)?;
    let skills = input.prompt_yn("  sync custom skills?", true)?;
    let claude_md = input.prompt_yn("  sync global CLAUDE.md?", true)?;

    let targets = config::SyncTargets {
        sessions: true,
        memories: true,
        settings,
        commands,
        skills,
        global_claude_md: claude_md,
    };

    let storage = if is_folder {
        StorageConfig::Folder { path: repo.clone() }
    } else {
        StorageConfig::Git {
            path: repo.clone(),
            auto_push: true,
            lfs_threshold: config::default_lfs_threshold(),
        }
    };

    println!();
    init_with_options(storage, op_ref, enc_override, targets)?;

    if !is_folder {
        println!();
        println!("git remote setup:");
        println!("  1) create a new private GitHub repo (needs gh CLI)");
        println!("  2) add an existing remote URL");
        println!("  3) skip (local only for now)");
        let remote_choice = input.prompt_with_default("choice [1-3]", "1")?;

        let git_store = GitStore::new(repo.clone());
        match remote_choice.trim() {
            "1" => {
                let repo_name = input.prompt_with_default("github repo name", "clync-data")?;
                let gh_result = std::process::Command::new("gh")
                    .args([
                        "repo",
                        "create",
                        &repo_name,
                        "--private",
                        "--description",
                        "Encrypted Claude Code sync (managed by clync)",
                    ])
                    .output();
                match gh_result {
                    Ok(output) if output.status.success() => {
                        let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
                        println!("created: {url}");
                        let ssh_url = format!(
                            "git@github.com:{}.git",
                            url.trim_start_matches("https://github.com/")
                        );
                        git_store.add_remote(&ssh_url)?;
                    }
                    Ok(output) => {
                        eprintln!(
                            "gh repo create failed: {}",
                            String::from_utf8_lossy(&output.stderr).trim()
                        );
                    }
                    Err(_) => {
                        eprintln!("gh CLI not found. Install with: brew install gh");
                    }
                }
            }
            "2" => {
                let remote_url =
                    input.prompt("remote url (e.g. git@github.com:you/clync-data.git)")?;
                if !remote_url.is_empty() {
                    git_store.add_remote(&remote_url)?;
                    println!("remote added: {remote_url}");
                }
            }
            _ => {
                println!("skipped. add a remote later with:");
                println!("  cd {} && git remote add origin <url>", repo.display());
            }
        }

        println!();
        let do_push = input.prompt_yn("do first push now?", true)?;
        if do_push {
            let config = Config::load()?;
            let keys = Cipher::from_config(&config.encryption)?;
            let filter = ScanFilter::default();

            if let Some(path) = config.storage_path() {
                repo_meta::RepoMeta::from_config(&config).save(path)?;
            }

            let result = sync::push(&config, &keys, &filter, &git_store)?;
            let extras = extras::push_extras(&config, &keys)?;
            let mem = memories::push_memories(&config, &keys)?;
            println!(
                "synced {} sessions, {} extras",
                result.pushed,
                extras.pushed + mem.pushed
            );

            let machine = manifest::get_machine_id();
            let total = result.pushed + extras.pushed + mem.pushed;
            git_store.commit(&format!("clync init ({total} files) from {machine}"))?;

            if git_store.has_remote() {
                let do_git_push = input.prompt_yn("git push to remote?", true)?;
                if do_git_push {
                    git_store.push_remote()?;
                    println!("pushed to remote");
                }
            }
        }
    }

    println!("\ndone. run `clync push` to sync anytime.");
    println!("add to Claude Code MCP config for in-session access:");
    println!("  clync config path  # shows config location");
    println!("  see `clync mcp` for MCP server setup");

    Ok(())
}

fn init_with_options(
    storage: StorageConfig,
    op_ref: Option<String>,
    enc_override: Option<EncryptionConfig>,
    targets: config::SyncTargets,
) -> Result<()> {
    let claude_dir = config::home_dir()
        .context("cannot determine home directory")?
        .join(".claude");

    let config_dir = Config::config_dir()?;
    std::fs::create_dir_all(&config_dir)?;
    let config_path = Config::config_path()?;

    let encryption = if let Some(enc) = enc_override {
        match &enc {
            EncryptionConfig::None => println!("encryption: disabled"),
            EncryptionConfig::Passphrase { env_var } => {
                println!("encryption: passphrase from ${env_var}")
            }
            EncryptionConfig::Bitwarden { item_id, .. } => {
                use crate::crypto::Keys;
                let keys = Keys::generate();
                eprintln!("generated age key pair (will not be shown again)");
                eprintln!("  public:  {}", keys.public_key());
                eprintln!("  secret:  {}", keys.secret_key());
                eprintln!();
                eprintln!("store the secret key in Bitwarden item: {item_id}");
            }
            EncryptionConfig::Pass { entry } => {
                use crate::crypto::Keys;
                let keys = Keys::generate();
                let secret = keys.secret_key();
                eprintln!("generated age key pair (will not be shown again)");
                eprintln!("  secret:  {secret}");
                eprintln!();
                eprintln!("store with: echo '{secret}' | pass insert -m {entry}");
            }
            _ => {}
        }
        enc
    } else if let Some(reference) = op_ref {
        use crate::crypto::Keys;
        let keys = Keys::generate();
        eprintln!("generated age key pair (will not be shown again)");
        eprintln!("  public:  {}", keys.public_key());
        eprintln!("  secret:  {}", keys.secret_key());
        eprintln!();
        eprintln!("store the secret key in 1Password at: {reference}");
        eprintln!("then verify with: op read \"{reference}\"");
        EncryptionConfig::OnePassword { reference }
    } else {
        use crate::crypto::Keys;
        let keys = Keys::generate();
        let key_path = config_dir.join("key.txt");
        write_secret_file(&key_path, &format!("{}\n", keys.secret_key()))?;

        println!("age key saved to {}", key_path.display());
        println!("  public key: {}", keys.public_key());
        EncryptionConfig::KeyFile { path: key_path }
    };

    if let Some(path) = storage.local_path() {
        std::fs::create_dir_all(path.join("sessions"))?;
    }

    match &storage {
        StorageConfig::Git { path, .. } => {
            if !path.join(".git").exists() {
                GitStore::init_repo(path)?;
            }
        }
        StorageConfig::Folder { path } => {
            std::fs::create_dir_all(path)?;
        }
        #[cfg(feature = "s3")]
        StorageConfig::S3 { .. } => {}
    }

    let config = Config {
        sync: SyncConfig {
            claude_dir,
            include_companion_dirs: false,
            storage,
        },
        encryption,
        targets,
    };

    config.save(&config_path)?;
    println!("config saved to {}", config_path.display());

    ensure_repo_readme(&config)?;

    if let Some(path) = config.storage_path() {
        println!("sync store ready at {}", path.display());
    } else {
        println!("sync store ready");
    }
    Ok(())
}

pub fn cmd_reset(keep_repo: bool, yes: bool, input: &dyn InputSource) -> Result<()> {
    let config_path = Config::config_path()?;
    if !config_path.exists() {
        println!("no clync config found, nothing to reset");
        return Ok(());
    }

    let config = Config::load()?;
    let config_dir = Config::config_dir()?;

    println!("this will remove:");
    println!("  config: {}", config_dir.display());
    if let Some(repo_path) = config.storage_path()
        && !keep_repo
        && repo_path.exists()
    {
        println!("  sync store: {}", repo_path.display());
    }
    println!();
    println!("sessions in ~/.claude will NOT be touched");

    if !yes {
        let confirm = input.prompt_yn("continue?", false)?;
        if !confirm {
            println!("cancelled");
            return Ok(());
        }
    }

    if let Some(repo_path) = config.storage_path()
        && !keep_repo
        && repo_path.exists()
    {
        std::fs::remove_dir_all(repo_path)
            .with_context(|| format!("could not remove {}", repo_path.display()))?;
        println!("removed {}", repo_path.display());
    }

    std::fs::remove_dir_all(&config_dir)
        .with_context(|| format!("could not remove {}", config_dir.display()))?;
    println!("removed {}", config_dir.display());
    println!("reset complete. run `clync init` or `clync join` to set up again.");

    Ok(())
}

pub(crate) fn prompt_manual_key(
    input: &dyn InputSource,
    config_dir: &std::path::Path,
) -> Result<EncryptionConfig> {
    println!("this repo requires an age key to decrypt");
    println!("provide the same key used on the other machine");
    let key = input.prompt("paste age secret key (AGE-SECRET-KEY-...)")?;
    let key_path = config_dir.join("key.txt");
    write_secret_file(&key_path, &format!("{key}\n"))?;
    Ok(EncryptionConfig::KeyFile { path: key_path })
}

pub(crate) fn write_secret_file(path: &std::path::Path, content: &str) -> Result<()> {
    #[cfg(unix)]
    {
        use std::io::Write;
        use std::os::unix::fs::OpenOptionsExt;
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .mode(0o600)
            .open(path)?;
        file.write_all(content.as_bytes())?;
    }
    #[cfg(not(unix))]
    {
        std::fs::write(path, content)?;
    }
    Ok(())
}

pub(crate) fn ensure_repo_readme(config: &Config) -> Result<()> {
    let path = match config.storage_path() {
        Some(p) => p.join("README.md"),
        None => return Ok(()),
    };
    if path.exists() {
        return Ok(());
    }
    let enc_note = match &config.encryption {
        EncryptionConfig::None => "Files are stored in plain text.",
        EncryptionConfig::Passphrase { .. } => "Files are encrypted with age (passphrase-based).",
        _ => "Files are encrypted with age (key-based).",
    };

    let setup_note = if config.sync.storage.is_git() {
        let git_store = GitStore::new(
            config
                .storage_path()
                .unwrap_or(std::path::Path::new("."))
                .to_path_buf(),
        );
        let ssh_url = git_store
            .get_remote_url()
            .unwrap_or_else(|| "<this-repo-url>".to_string());
        let https_url = ssh_to_https(&ssh_url);
        format!(
            "## Setup on another machine\n\n\
             ```bash\n\
             cargo install clync\n\n\
             # SSH\n\
             clync join {ssh_url}\n\n\
             # HTTPS\n\
             clync join {https_url}\n\
             ```"
        )
    } else {
        "## Setup on another machine\n\n\
         Mount this folder on the other machine, then:\n\n\
         ```bash\n\
         cargo install clync\n\
         clync init --storage folder --repo /path/to/this/folder\n\
         ```"
        .to_string()
    };

    std::fs::write(
        &path,
        format!(
            "# clync sync repo\n\n\
             This repo is managed by [clync](https://github.com/Saturate/clync) \
             and contains synced Claude Code data.\n\n\
             {enc_note}\n\n\
             {setup_note}\n\n\
             See `clync.toml` for sync configuration.\n"
        ),
    )?;
    Ok(())
}

fn ssh_to_https(url: &str) -> String {
    if let Some(rest) = url.strip_prefix("git@") {
        let converted = rest.replacen(':', "/", 1);
        return format!("https://{converted}");
    }
    url.to_string()
}