mise 2026.9.3

Dev tools, env vars, and tasks in one CLI
use eyre::Result;

use crate::config::{Config, Settings};
use crate::system;
use crate::system::history::OperationScope;

/// Apply dotfiles from `[dotfiles]`
///
/// Applies configured whole-file entries and edits that aren't in their
/// desired state. Whole-file entries may symlink, copy, or render templates.
/// Edit entries manage a marker-delimited block or a single line in a file
/// mise doesn't otherwise own.
#[derive(Debug, usage_rs::Args)]
#[usage(
    verbatim_doc_comment,
    example(
        r###"mise bootstrap dotfiles apply
mise bootstrap dotfiles apply --dry-run
mise bootstrap dotfiles apply --force --yes"###
    )
)]
pub(crate) struct DotfilesApply {
    /// Only apply these targets
    #[usage(value_name = "TARGET")]
    targets: Vec<String>,

    /// Overwrite existing files that conflict with whole-file dotfile entries
    #[usage(long, short)]
    force: bool,

    /// Print the actions that would run without writing anything
    #[usage(long, short = 'n')]
    dry_run: bool,

    /// Skip the confirmation prompt
    #[usage(long, short)]
    yes: bool,
}

impl DotfilesApply {
    pub(crate) fn dry_run(&self) -> bool {
        self.dry_run
    }

    /// Load and filter the configured whole-file and edit requests.
    pub(crate) fn requests(
        &self,
        config: &Config,
    ) -> Result<(
        Vec<system::files::FileRequest>,
        Vec<system::edits::EditRequest>,
    )> {
        super::select_requests(config, &self.targets)
    }

    pub(crate) async fn run(self) -> Result<bool> {
        OperationScope::wrap("bootstrap dotfiles apply", self.dry_run, self.run_inner()).await
    }

    /// The apply without an operation of its own, for a caller that already
    /// opened one.
    pub(crate) async fn run_inner(self) -> Result<bool> {
        let config = Config::get().await?;
        let (files, edits) = self.requests(&config)?;
        if files.is_empty() && edits.is_empty() {
            super::warn_if_dotfiles_ignored();
            info!("no dotfiles configured in [dotfiles]");
            return Ok(true);
        }
        if !files.is_empty() {
            let opts = system::files::ApplyOpts {
                dry_run: self.dry_run,
                verbose: Settings::get().verbose,
                force: self.force,
                force_hint: "use --force",
                yes: self.yes,
            };
            if !system::files::apply(&config, &files, &opts)? {
                return Ok(false);
            }
        }
        if !edits.is_empty() {
            let opts = system::edits::ApplyOpts {
                part: "dotfiles",
                dry_run: self.dry_run,
                verbose: Settings::get().verbose,
                yes: self.yes,
            };
            if !system::edits::apply(&config, &edits, &opts)? {
                return Ok(false);
            }
        }
        Ok(true)
    }
}