Skip to main content

vs_core/service/
migrate.rs

1//! Services for migrating data from legacy `vfox` homes.
2
3use crate::{App, CoreError, MigrateSummary};
4
5impl App {
6    /// Copies compatible state from a legacy home into the active home.
7    pub fn migrate(&self, source: Option<String>) -> Result<MigrateSummary, CoreError> {
8        let source_home = source
9            .map(|source| self.normalize_source_path(&source))
10            .or_else(|| self.home_layout.migration_candidates.first().cloned())
11            .ok_or(CoreError::MissingMigrationSource)?;
12
13        let mut copied_roots = 0;
14        for relative in ["config.yaml", "global", "registry", "plugins", "cache"] {
15            let source_path = source_home.join(relative);
16            let destination_path = self.home().join(relative);
17            if !source_path.exists() {
18                continue;
19            }
20            if source_path.is_dir() {
21                self.copy_tree(&source_path, &destination_path)?;
22            } else {
23                if let Some(parent) = destination_path.parent() {
24                    std::fs::create_dir_all(parent)?;
25                }
26                std::fs::copy(&source_path, &destination_path)?;
27            }
28            copied_roots += 1;
29        }
30
31        Ok(MigrateSummary {
32            source_home,
33            copied_roots,
34        })
35    }
36}