xdotter 0.1.0

A simple dotfile manager - single binary, no dependencies
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
use crate::cli::{Cli, Command};
use crate::config::{detect_format, validate_toml, Config};
use crate::expand_path;
use crate::permissions::{check_permission, fix_permission, get_required_permission};
use crate::symlink;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

const VERSION: &str = "0.4.0";

// Completion scripts are generated at build time into OUT_DIR
const BASH_COMPLETION: &str = include_str!(concat!(env!("OUT_DIR"), "/xd.bash"));
const ZSH_COMPLETION: &str = include_str!(concat!(env!("OUT_DIR"), "/_xd"));
const FISH_COMPLETION: &str = include_str!(concat!(env!("OUT_DIR"), "/xd.fish"));

pub fn log(cli: &Cli, level: &str, msg: &str) {
    if cli.quiet {
        return;
    }

    match level {
        "info" => {
            if cli.verbose || !cli.quiet {
                println!("{}", msg);
            }
        }
        "debug" => {
            if cli.verbose {
                println!("[DEBUG] {}", msg);
            }
        }
        "warning" => println!("\x1b[1;33m[WARNING] {}\x1b[0m", msg),
        "error" => eprintln!("\x1b[0;31m[ERROR] {}\x1b[0m", msg),
        _ => println!("{}", msg),
    }
}

pub fn dispatch(cli: &Cli) -> Result<(), String> {
    match cli.command.as_ref().unwrap_or(&Command::Deploy) {
        Command::Deploy => cmd_deploy(cli),
        Command::Undeploy => cmd_undeploy(cli),
        Command::Status => cmd_status(cli),
        Command::Validate { files } => cmd_validate(cli, files),
        Command::New => cmd_new(cli),
        Command::Completion { shell } => cmd_completion(shell),
        Command::Version => cmd_version(cli),
    }
}

fn cmd_version(_cli: &Cli) -> Result<(), String> {
    println!("xdotter {}", VERSION);
    Ok(())
}

fn cmd_completion(shell: &str) -> Result<(), String> {
    let script = match shell.to_lowercase().as_str() {
        "bash" => BASH_COMPLETION,
        "zsh" => ZSH_COMPLETION,
        "fish" => FISH_COMPLETION,
        _ => {
            return Err(format!(
                "Unsupported shell: {}. Supported: bash, zsh, fish",
                shell
            ))
        }
    };
    print!("{}", script);
    Ok(())
}

fn cmd_new(cli: &Cli) -> Result<(), String> {
    let config_path = Path::new("xdotter.toml");
    if config_path.exists() {
        return Err("xdotter.toml already exists".to_string());
    }

    let content = r#"# xdotter configuration file

[links]
# Format: "source_path" = "target_link"
# The source is your actual dotfile in the repo
# The target is where you want it symlinked (~ expands to home directory)

# ".config/nvim/init.lua" = "~/.config/nvim/init.lua"
# ".zshrc" = "~/.zshrc"
# ".gitconfig" = "~/.gitconfig"

[dependencies]
# Format: "name" = "relative_path"
# Subdirectories with their own xdotter.toml
# "go" = "testdata/go"
# "nvim" = "config/nvim"
"#;

    if cli.dry_run {
        log(cli, "info", "Would create xdotter.toml");
    } else {
        fs::write(config_path, content).map_err(|e| format!("Failed to write config: {}", e))?;
        log(cli, "info", "Created xdotter.toml");
    }

    Ok(())
}

fn validate_config_file(filepath: &Path) -> Result<(), String> {
    let content = fs::read_to_string(filepath).map_err(|e| format!("Cannot read file: {}", e))?;

    match detect_format(filepath) {
        Some("toml") => validate_toml(&content),
        _ => Err(format!("Unknown file format: {}. Only TOML is supported.", filepath.display())),
    }
}

fn cmd_status(cli: &Cli) -> Result<(), String> {
    let config_path = Path::new("xdotter.toml");
    if !config_path.exists() {
        return Err(format!("Config file not found: {}", config_path.display()));
    }

    let content =
        fs::read_to_string(config_path).map_err(|e| format!("Failed to read config: {}", e))?;

    let config = Config::from_toml(&content)?;

    let mut total = 0;
    let mut valid = 0;
    let mut broken = 0;
    let mut perm_issues = 0;

    for link in config.links.values() {
        total += 1;
        let link_path = expand_path(link);

        if link_path.is_symlink() {
            if let Ok(resolved) = fs::read_link(&link_path) {
                // Check if target exists
                let target_exists = expand_path(&resolved.to_string_lossy()).exists();
                if target_exists {
                    valid += 1;
                    // Check permissions
                    if let Ok(canonical) = link_path.canonicalize() {
                        if let Some((required_mode, description)) =
                            get_required_permission(&link_path)
                        {
                            if !check_permission(&canonical, required_mode) {
                                perm_issues += 1;
                                if !cli.quiet {
                                    log(
                                        cli,
                                        "warning",
                                        &format!(
                                            "{} -> {} ({}: expected {:03o})",
                                            link_path.display(),
                                            canonical.display(),
                                            description,
                                            required_mode
                                        ),
                                    );
                                }
                            } else if cli.verbose {
                                log(
                                    cli,
                                    "info",
                                    &format!(
                                        "✓ {} -> {} ({:03o})",
                                        link_path.display(),
                                        canonical.display(),
                                        required_mode
                                    ),
                                );
                            }
                        } else if cli.verbose {
                            log(
                                cli,
                                "info",
                                &format!("✓ {} -> {}", link_path.display(), canonical.display()),
                            );
                        }
                    }
                } else {
                    broken += 1;
                    log(
                        cli,
                        "warning",
                        &format!(
                            "✗ {} -> {} (broken: target missing)",
                            link_path.display(),
                            resolved.display()
                        ),
                    );
                }
            }
        } else if link_path.exists() {
            broken += 1;
            log(
                cli,
                "warning",
                &format!(
                    "✗ {} (not a symlink, regular file exists)",
                    link_path.display()
                ),
            );
        }
        // else: link doesn't exist, not deployed yet (not counted as broken)
    }

    if !cli.quiet {
        println!();
        println!("Status: {}/{} deployed", valid, total);
        if broken > 0 {
            println!("Broken links: {}", broken);
        }
        if perm_issues > 0 {
            println!("Permission issues: {}", perm_issues);
        }
    }

    if perm_issues > 0 {
        Err("Permission issues found. Use --fix-permissions to fix.".to_string())
    } else {
        Ok(())
    }
}

fn cmd_validate(cli: &Cli, files: &[PathBuf]) -> Result<(), String> {
    if files.is_empty() {
        let defaults = ["xdotter.toml"];
        let mut found = false;
        for f in &defaults {
            let path = Path::new(f);
            if path.exists() {
                if let Err(e) = validate_config_file(path) {
                    eprintln!("{}", e);
                    return Err("Validation failed".to_string());
                }
                log(cli, "info", &format!("✓ {} is Valid", f));
                found = true;
            }
        }
        if !found {
            return Err("No default config file found (xdotter.toml)".to_string());
        }
    } else {
        let mut all_valid = true;
        for filepath in files {
            if !filepath.exists() {
                log(
                    cli,
                    "error",
                    &format!("File not found: {}", filepath.display()),
                );
                all_valid = false;
                continue;
            }
            if let Err(e) = validate_config_file(filepath) {
                eprintln!("{}: {}", filepath.display(), e);
                all_valid = false;
            } else {
                log(cli, "info", &format!("✓ {} is Valid", filepath.display()));
            }
        }
        if !all_valid {
            return Err("Validation failed".to_string());
        }
    }

    Ok(())
}

fn cmd_deploy(cli: &Cli) -> Result<(), String> {
    log(cli, "info", "Deploying...");

    let config_path = Path::new("xdotter.toml");
    if !config_path.exists() {
        return Err(format!("Config file not found: {}", config_path.display()));
    }

    // Auto-validate unless --no-validate
    if !cli.no_validate {
        if let Err(e) = validate_config_file(config_path) {
            log(cli, "error", &e);
            return Err("Config validation failed".to_string());
        }
        log(cli, "debug", "Config validation passed");
    }

    let content =
        fs::read_to_string(config_path).map_err(|e| format!("Failed to read config: {}", e))?;

    let config = Config::from_toml(&content)?;

    log(
        cli,
        "debug",
        &format!("Deploying from {}", config_path.display()),
    );

    let mut success = true;

    for (actual_path, link) in &config.links {
        log(cli, "info", &format!("deploy: {} -> {}", actual_path, link));
        if let Err(e) = symlink::create_symlink(actual_path, link, cli) {
            log(cli, "error", &format!("failed to create link: {}", e));
            success = false;
            continue;
        }

        // Permission check/fix after successful symlink creation
        let link_path = expand_path(link);
        if link_path.is_symlink() {
            if let Ok(resolved) = link_path.canonicalize() {
                if let Some((required_mode, description)) = get_required_permission(&link_path) {
                    let is_correct = check_permission(&resolved, required_mode);
                    if !is_correct {
                        if cli.fix_permissions {
                            if cli.dry_run {
                                log(
                                    cli,
                                    "info",
                                    &format!(
                                        "Would fix permission for {} to {:03o}",
                                        link_path.display(),
                                        required_mode
                                    ),
                                );
                            } else {
                                if fix_permission(&resolved, required_mode) {
                                    log(
                                        cli,
                                        "info",
                                        &format!(
                                            "Fixed permission for {} to {:03o}",
                                            link_path.display(),
                                            required_mode
                                        ),
                                    );
                                } else {
                                    log(
                                        cli,
                                        "error",
                                        &format!(
                                            "Failed to fix permission for {}",
                                            link_path.display()
                                        ),
                                    );
                                    success = false;
                                }
                            }
                        } else if cli.check_permissions {
                            log(
                                cli,
                                "warning",
                                &format!(
                                    "{}: {} has wrong permission (expected {:03o})",
                                    description,
                                    link_path.display(),
                                    required_mode
                                ),
                            );
                        }
                    }
                }
            }
        }
    }

    // Process dependencies
    let current_dir = std::env::current_dir().map_err(|e| e.to_string())?;
    for (dep_name, dep_path) in &config.dependencies {
        log(
            cli,
            "debug",
            &format!("dependency: {}, path: {}", dep_name, dep_path),
        );
        let dep_dir = current_dir.join(dep_path);
        let dep_config = dep_dir.join("xdotter.toml");
        if dep_config.exists() {
            log(cli, "debug", &format!("entering {}", dep_dir.display()));
            // Save and restore current directory
            let prev_dir = current_dir.clone();
            if let Err(e) = std::env::set_current_dir(&dep_dir) {
                log(cli, "error", &format!("Cannot enter dependency dir: {}", e));
                success = false;
                continue;
            }
            if let Err(e) = cmd_deploy(cli) {
                log(cli, "error", &format!("Dependency deploy failed: {}", e));
                success = false;
            }
            let _ = std::env::set_current_dir(&prev_dir);
        }
    }

    if success {
        Ok(())
    } else {
        Err("Some links failed to deploy".to_string())
    }
}

fn cmd_undeploy(cli: &Cli) -> Result<(), String> {
    log(cli, "info", "Undeploying...");

    let config_path = Path::new("xdotter.toml");
    if !config_path.exists() {
        return Err(format!("Config file not found: {}", config_path.display()));
    }

    let content =
        fs::read_to_string(config_path).map_err(|e| format!("Failed to read config: {}", e))?;

    let config = Config::from_toml(&content)?;

    let mut success = true;

    for link in config.links.values() {
        let link_path = expand_path(link);
        if link_path.is_symlink() {
            if cli.dry_run {
                log(
                    cli,
                    "info",
                    &format!("Would remove symlink: {}", link_path.display()),
                );
            } else {
                if cli.interactive {
                    print!("Remove symlink {}? [y/n] ", link_path.display());
                    io::Write::flush(&mut io::stdout()).ok();
                    let mut input = String::new();
                    io::stdin().read_line(&mut input).ok();
                    if input.trim().to_lowercase() != "y" {
                        log(cli, "debug", "Skipping");
                        continue;
                    }
                }
                log(cli, "debug", &format!("Removing {}", link_path.display()));
                if let Err(e) = fs::remove_file(&link_path) {
                    log(
                        cli,
                        "error",
                        &format!("Failed to remove {}: {}", link_path.display(), e),
                    );
                    success = false;
                }
            }
        } else if link_path.exists() {
            log(
                cli,
                "warning",
                &format!("Target is not a symlink: {}", link_path.display()),
            );
            if !cli.force {
                success = false;
            }
        } else {
            log(
                cli,
                "debug",
                &format!("Link does not exist: {}", link_path.display()),
            );
        }
    }

    if success {
        Ok(())
    } else {
        Err("Some links failed to undeploy".to_string())
    }
}