treeboot 0.3.0

Bootstrap Git worktrees from one repo-local setup file.
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
620
621
622
623
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::ExitCode;

use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum, ValueHint};
use clap_complete::{ArgValueCompleter, CompletionCandidate, Shell};
use treeboot_core::{
    CommandKind, CommandOperation, ConfigOptions, ConfigReport, Error, FileOperation,
    FileOperationCompletionOptions, FileOperationKind, FileOperationOptions, InitKind, InitOptions,
    OutputEvent, Reporter, RunOptions, RuntimeOptionOverrides, SymlinkMode, SyncCompare,
};

#[derive(Debug, Parser)]
#[command(
    name = "treeboot",
    version,
    about = "Bootstrap new Git worktrees from one repo-local setup file.",
    propagate_version = true
)]
struct Cli {
    #[command(flatten)]
    run: RunArgs,

    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Run worktree bootstrap.
    Run(RunArgs),
    /// Copy files or directories from the root checkout.
    Copy(CopyArgs),
    /// Symlink files or directories from the root checkout.
    Symlink(SymlinkArgs),
    /// Sync files or directories from the root checkout.
    Sync(SyncArgs),
    /// Parse and print normalized config without executing it.
    Config(ConfigArgs),
    /// Create a starter config or init script.
    Init(InitArgs),
    /// Print shell completion scripts.
    Completions(CompletionsArgs),
}

#[derive(Debug, Args, Clone, Default)]
struct ManualArgs {
    /// Override the checkout used as the file-operation source.
    #[arg(short, long, value_hint = ValueHint::DirPath)]
    root: Option<PathBuf>,

    /// Target path in the current worktree.
    #[arg(short, long, value_hint = ValueHint::AnyPath)]
    target: Option<PathBuf>,

    /// Fail when a source is missing.
    #[arg(long)]
    required: bool,

    /// Fail on stricter file-operation conflicts.
    #[arg(short = 'S', long)]
    strict: bool,

    /// Replace existing file-operation targets where supported.
    #[arg(short, long)]
    force: bool,

    /// Print planned work without changing files.
    #[arg(short = 'n', long)]
    dry_run: bool,

    /// Source paths from the root checkout.
    #[arg(
        required = true,
        num_args = 1..,
        value_hint = ValueHint::AnyPath,
        add = ArgValueCompleter::new(root_source_completer),
    )]
    sources: Vec<PathBuf>,
}

#[derive(Debug, Args, Clone, Default)]
struct CopyArgs {
    #[command(flatten)]
    manual: ManualArgs,

    /// How to handle source symlinks.
    #[arg(long, value_enum)]
    symlinks: Option<CliSymlinkMode>,
}

#[derive(Debug, Args, Clone, Default)]
struct SymlinkArgs {
    #[command(flatten)]
    manual: ManualArgs,
}

#[derive(Debug, Args, Clone, Default)]
struct SyncArgs {
    #[command(flatten)]
    manual: ManualArgs,

    /// How to handle source symlinks.
    #[arg(long, value_enum)]
    symlinks: Option<CliSymlinkMode>,

    /// How to compare source and target files.
    #[arg(long, value_enum)]
    compare: Option<CliSyncCompare>,

    /// Delete target-only files.
    #[arg(short = 'D', long, conflicts_with = "no_delete")]
    delete: bool,

    /// Preserve target-only files.
    #[arg(long, conflicts_with = "delete")]
    no_delete: bool,
}

#[derive(Debug, Args, Clone, Default)]
struct RunArgs {
    /// Override the checkout used as the file-operation source.
    #[arg(short, long)]
    root: Option<PathBuf>,

    /// Use one specific config file and skip init script discovery.
    #[arg(short, long)]
    config: Option<PathBuf>,

    /// Skip init script discovery and use declarative config discovery.
    #[arg(long)]
    no_init_script: bool,

    /// Fail on missing config and stricter file-operation conflicts.
    #[arg(short = 'S', long)]
    strict: bool,

    /// Replace existing file-operation targets where supported.
    #[arg(short, long)]
    force: bool,

    /// Print planned work without changing files or running commands.
    #[arg(short = 'n', long)]
    dry_run: bool,

    /// Run file operations only.
    #[arg(long)]
    skip_commands: bool,
}

#[derive(Debug, Args, Clone, Default)]
struct InitArgs {
    /// Create a starter TOML config.
    #[arg(long, conflicts_with = "script")]
    config: bool,

    /// Create an executable init script.
    #[arg(short, long, conflicts_with = "config")]
    script: bool,

    /// Output path for the generated file.
    #[arg(short, long)]
    path: Option<PathBuf>,
}

#[derive(Debug, Args, Clone, Default)]
struct ConfigArgs {
    /// Override the checkout used as the file-operation source.
    #[arg(short, long)]
    root: Option<PathBuf>,

    /// Use one specific config file and skip config discovery.
    #[arg(short, long)]
    config: Option<PathBuf>,

    /// Output format for normalized config.
    #[arg(short = 'o', long, value_enum, conflicts_with = "json")]
    format: Option<ConfigFormat>,

    /// Print normalized config as JSON.
    #[arg(short = 'J', long)]
    json: bool,
}

#[derive(Debug, Args, Clone, Copy)]
struct CompletionsArgs {
    /// Shell to generate completions for.
    shell: Shell,
}

#[derive(Debug, Clone, Copy, Default, ValueEnum)]
enum ConfigFormat {
    #[default]
    Text,
    Json,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum CliSymlinkMode {
    Preserve,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum CliSyncCompare {
    Metadata,
    Checksum,
}

fn main() -> ExitCode {
    clap_complete::CompleteEnv::with_factory(Cli::command).complete();

    let cli = Cli::parse();
    let mut reporter = StdoutReporter;
    let result = match cli.command {
        Some(Command::Run(args)) => treeboot_core::run(args.into(), &mut reporter).map(|_| ()),
        Some(Command::Copy(args)) => {
            treeboot_core::run_file_operation(args.into_options(), &mut reporter).map(|_| ())
        }
        Some(Command::Symlink(args)) => {
            treeboot_core::run_file_operation(args.into_options(), &mut reporter).map(|_| ())
        }
        Some(Command::Sync(args)) => {
            treeboot_core::run_file_operation(args.into_options(), &mut reporter).map(|_| ())
        }
        Some(Command::Config(args)) => run_config_command(args),
        Some(Command::Init(args)) => treeboot_core::init(args.into(), &mut reporter).map(|_| ()),
        Some(Command::Completions(args)) => run_completions_command(args),
        None => treeboot_core::run(cli.run.into(), &mut reporter).map(|_| ()),
    };

    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(error) => {
            eprintln!("treeboot: {error}");
            ExitCode::from(error.exit_code())
        }
    }
}

fn root_source_completer(current: &OsStr) -> Vec<CompletionCandidate> {
    let root = completion_root_override();
    treeboot_core::file_operation_source_candidates(FileOperationCompletionOptions {
        cwd: std::env::current_dir().ok(),
        root,
        current: PathBuf::from(current),
    })
    .into_iter()
    .map(CompletionCandidate::new)
    .collect()
}

fn completion_root_override() -> Option<PathBuf> {
    let mut args = std::env::args_os();
    for arg in args.by_ref() {
        if arg == "--" {
            break;
        }
    }

    while let Some(arg) = args.next() {
        if let Some(value) = arg.to_string_lossy().strip_prefix("--root=") {
            return Some(PathBuf::from(value));
        }
        if arg == "--root" || arg == "-r" {
            return args.next().map(PathBuf::from);
        }
    }

    None
}

fn run_completions_command(args: CompletionsArgs) -> treeboot_core::Result<()> {
    let shells = clap_complete::env::Shells::builtins();
    let shell = shells
        .completer(&args.shell.to_string())
        .ok_or_else(|| Error::Output {
            source: std::io::Error::other(format!("unsupported shell {}", args.shell)),
        })?;
    let stdout = std::io::stdout();
    let mut handle = stdout.lock();
    let completer = std::env::current_exe()
        .map(|path| path.to_string_lossy().into_owned())
        .unwrap_or_else(|_| "treeboot".to_owned());

    shell
        .write_registration("COMPLETE", "treeboot", "treeboot", &completer, &mut handle)
        .map_err(|source| Error::Output { source })
}

fn run_config_command(args: ConfigArgs) -> treeboot_core::Result<()> {
    let format = args.output_format();
    let env_options = RuntimeOptionOverrides::from_env()?;
    let report = treeboot_core::inspect_config(args.into())?;

    match format {
        ConfigFormat::Text => print_config_text(&report).map_err(|source| Error::Output { source }),
        ConfigFormat::Json => {
            let output = serde_json::json!({
                "path": report.path,
                "config": report.config,
            });
            let stdout = std::io::stdout();
            let mut handle = stdout.lock();

            serde_json::to_writer_pretty(&mut handle, &output).map_err(|source| Error::Output {
                source: std::io::Error::other(source),
            })?;
            use std::io::Write;
            writeln!(handle).map_err(|source| Error::Output { source })
        }
    }?;

    let plan_options = env_options.resolve(&report.config.options, false);

    if let Err(error) = treeboot_core::ActionPlan::from_manifest(
        &report.path,
        &report.config,
        &report.context,
        plan_options.into(),
    ) {
        eprintln!("treeboot: warning: run validation would fail: {error}");
    }

    Ok(())
}

fn print_config_text(report: &ConfigReport) -> std::io::Result<()> {
    println!("treeboot: config {}", report.path.display());
    println!();
    println!("files:");
    if report.config.files.is_empty() {
        println!("  (none)");
    } else {
        for operation in &report.config.files {
            println!("  {}", file_operation_summary(operation));
        }
    }
    println!();
    println!("commands:");
    if report.config.commands.is_empty() {
        println!("  (none)");
    } else {
        for command in &report.config.commands {
            println!("  {}", command_summary(command));
        }
    }

    Ok(())
}

fn file_operation_summary(operation: &FileOperation) -> String {
    let mut summary = format!(
        "{} {} -> {}",
        operation.operation.as_str(),
        operation.source.display(),
        operation.target.display()
    );

    if operation.required {
        summary.push_str(" required=true");
    }
    if let Some(compare) = operation.compare {
        summary.push_str(&format!(" compare={compare:?}").to_lowercase());
    }
    if let Some(delete) = operation.delete {
        summary.push_str(&format!(" delete={delete}"));
    }
    if let Some(symlinks) = operation.symlinks {
        summary.push_str(&format!(" symlinks={symlinks:?}").to_lowercase());
    }

    summary
}

fn command_summary(command: &CommandOperation) -> String {
    let mut summary = match &command.command {
        CommandKind::Shell { run } => format!("run {run:?}"),
        CommandKind::Direct { program, args } => {
            let mut parts = vec![program.as_str()];
            parts.extend(args.iter().map(String::as_str));
            format!("exec {}", parts.join(" "))
        }
    };

    if command.allow_failure {
        summary.push_str(" allow_failure=true");
    }
    if let Some(cwd) = &command.cwd {
        summary.push_str(&format!(" cwd={}", cwd.display()));
    }
    if !command.env.is_empty() {
        let env = command
            .env
            .iter()
            .map(|(name, value)| format!("{name}={value:?}"))
            .collect::<Vec<_>>()
            .join(", ");
        summary.push_str(&format!(" env={{{env}}}"));
    }

    summary
}

impl From<RunArgs> for RunOptions {
    fn from(args: RunArgs) -> Self {
        Self {
            cwd: None,
            root: args.root,
            config: args.config,
            no_init_script: args.no_init_script,
            strict: args.strict,
            force: args.force,
            dry_run: args.dry_run,
            skip_commands: args.skip_commands,
        }
    }
}

impl CopyArgs {
    fn into_options(self) -> FileOperationOptions {
        FileOperationOptions {
            symlinks: self.symlinks.map(Into::into),
            ..self.manual.into_options(FileOperationKind::Copy)
        }
    }
}

impl SymlinkArgs {
    fn into_options(self) -> FileOperationOptions {
        self.manual.into_options(FileOperationKind::Symlink)
    }
}

impl SyncArgs {
    fn into_options(self) -> FileOperationOptions {
        FileOperationOptions {
            symlinks: self.symlinks.map(Into::into),
            compare: self.compare.map(Into::into),
            delete: sync_delete_option(self.delete, self.no_delete),
            ..self.manual.into_options(FileOperationKind::Sync)
        }
    }
}

impl ManualArgs {
    fn into_options(self, operation: FileOperationKind) -> FileOperationOptions {
        FileOperationOptions {
            cwd: None,
            root: self.root,
            operation,
            sources: self.sources,
            target: self.target,
            required: self.required,
            symlinks: None,
            compare: None,
            delete: None,
            strict: self.strict,
            force: self.force,
            dry_run: self.dry_run,
        }
    }
}

const fn sync_delete_option(delete: bool, no_delete: bool) -> Option<bool> {
    if delete {
        Some(true)
    } else if no_delete {
        Some(false)
    } else {
        None
    }
}

impl From<CliSymlinkMode> for SymlinkMode {
    fn from(value: CliSymlinkMode) -> Self {
        match value {
            CliSymlinkMode::Preserve => Self::Preserve,
        }
    }
}

impl From<CliSyncCompare> for SyncCompare {
    fn from(value: CliSyncCompare) -> Self {
        match value {
            CliSyncCompare::Metadata => Self::Metadata,
            CliSyncCompare::Checksum => Self::Checksum,
        }
    }
}

impl From<ConfigArgs> for ConfigOptions {
    fn from(args: ConfigArgs) -> Self {
        Self {
            cwd: None,
            root: args.root,
            config: args.config,
        }
    }
}

impl ConfigArgs {
    fn output_format(&self) -> ConfigFormat {
        if self.json {
            ConfigFormat::Json
        } else {
            self.format.unwrap_or_default()
        }
    }
}

impl From<InitArgs> for InitOptions {
    fn from(args: InitArgs) -> Self {
        Self {
            cwd: None,
            kind: init_kind(&args),
            path: args.path,
        }
    }
}

fn init_kind(args: &InitArgs) -> InitKind {
    match (args.config, args.script) {
        (false, true) => InitKind::Script,
        _ => InitKind::Config,
    }
}

struct StdoutReporter;

impl Reporter for StdoutReporter {
    fn report(&mut self, event: OutputEvent) -> std::io::Result<()> {
        println!("{}", event.message());
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn manual_args() -> ManualArgs {
        ManualArgs {
            sources: vec![PathBuf::from(".env")],
            ..ManualArgs::default()
        }
    }

    #[test]
    fn sync_delete_option_should_normalize_delete_flags() {
        assert_eq!(sync_delete_option(false, false), None);
        assert_eq!(sync_delete_option(true, false), Some(true));
        assert_eq!(sync_delete_option(false, true), Some(false));
        assert_eq!(sync_delete_option(true, true), Some(true));
    }

    #[test]
    fn copy_args_should_preserve_manual_options_and_symlink_mode() {
        let args = CopyArgs {
            manual: ManualArgs {
                target: Some(PathBuf::from("local")),
                required: true,
                strict: true,
                force: true,
                dry_run: true,
                ..manual_args()
            },
            symlinks: Some(CliSymlinkMode::Preserve),
        };

        let options = args.into_options();

        assert_eq!(options.operation, FileOperationKind::Copy);
        assert_eq!(options.target, Some(PathBuf::from("local")));
        assert!(options.required);
        assert!(options.strict);
        assert!(options.force);
        assert!(options.dry_run);
        assert_eq!(options.symlinks, Some(SymlinkMode::Preserve));
    }

    #[test]
    fn symlink_args_should_set_symlink_operation() {
        let options = SymlinkArgs {
            manual: manual_args(),
        }
        .into_options();

        assert_eq!(options.operation, FileOperationKind::Symlink);
    }

    #[test]
    fn sync_args_should_normalize_sync_specific_options() {
        let options = SyncArgs {
            manual: manual_args(),
            symlinks: Some(CliSymlinkMode::Preserve),
            compare: Some(CliSyncCompare::Checksum),
            delete: true,
            no_delete: false,
        }
        .into_options();

        assert_eq!(options.operation, FileOperationKind::Sync);
        assert_eq!(options.symlinks, Some(SymlinkMode::Preserve));
        assert_eq!(options.compare, Some(SyncCompare::Checksum));
        assert_eq!(options.delete, Some(true));
    }

    #[test]
    fn cli_value_enums_should_convert_to_core_options() {
        assert_eq!(
            SymlinkMode::from(CliSymlinkMode::Preserve),
            SymlinkMode::Preserve
        );
        assert_eq!(
            SyncCompare::from(CliSyncCompare::Metadata),
            SyncCompare::Metadata
        );
        assert_eq!(
            SyncCompare::from(CliSyncCompare::Checksum),
            SyncCompare::Checksum
        );
    }
}