Updated folder structures to use UUID v4 for parent folder structure instead on folder name
This commit is contained in:
1 parent
2ec496691c
commit
db6f03c3b2
1 file changed
+187
-60
+187
-60
@@ -903,11 +903,19 @@ impl ProfileFolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// One folder record in `profiles/folders.toml`.
|
/// One folder record in `profiles/folders.toml`.
|
||||||
|
///
|
||||||
|
/// Folders are linked by UUID instead of by path, so two folders can share a
|
||||||
|
/// name. Root folders have no `parent_id`.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
struct FolderRecord {
|
struct FolderRecord {
|
||||||
#[serde(default = "new_folder_id")]
|
#[serde(default = "new_folder_id")]
|
||||||
id: String,
|
id: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
name: String,
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
parent_id: Option<String>,
|
||||||
|
/// Full path from files written before folders were linked by UUID.
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
path: String,
|
path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -936,9 +944,14 @@ struct FoldersOutFile {
|
|||||||
struct ProfileFile {
|
struct ProfileFile {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
name: String,
|
name: String,
|
||||||
|
/// UUID of the folder that contains this profile.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
parent_id: String,
|
||||||
|
/// Legacy name-based folder reference, still read but no longer written.
|
||||||
|
#[serde(default, skip_serializing)]
|
||||||
folder: String,
|
folder: String,
|
||||||
#[serde(default)]
|
/// Legacy folder path fallback, still read but no longer written.
|
||||||
|
#[serde(default, skip_serializing)]
|
||||||
folder_name: String,
|
folder_name: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
kind: ProfileKind,
|
kind: ProfileKind,
|
||||||
@@ -1038,6 +1051,7 @@ impl ProfileFile {
|
|||||||
fn from_profile(profile: &Profile, folder: &str) -> Self {
|
fn from_profile(profile: &Profile, folder: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: profile.name.clone(),
|
name: profile.name.clone(),
|
||||||
|
parent_id: profile.folder_id.clone(),
|
||||||
folder: profile.folder_id.clone(),
|
folder: profile.folder_id.clone(),
|
||||||
folder_name: folder.to_owned(),
|
folder_name: folder.to_owned(),
|
||||||
kind: profile.kind,
|
kind: profile.kind,
|
||||||
@@ -1067,7 +1081,11 @@ impl ProfileFile {
|
|||||||
start_path: self.start_path,
|
start_path: self.start_path,
|
||||||
private_key: self.private_key,
|
private_key: self.private_key,
|
||||||
glyph_color: color_from_hex(&self.glyph_color).unwrap_or(Color::WHITE),
|
glyph_color: color_from_hex(&self.glyph_color).unwrap_or(Color::WHITE),
|
||||||
folder_id: self.folder,
|
folder_id: if self.parent_id.trim().is_empty() {
|
||||||
|
self.folder
|
||||||
|
} else {
|
||||||
|
self.parent_id
|
||||||
|
},
|
||||||
file: None,
|
file: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2054,39 +2072,126 @@ fn insert_folder_path(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves a profile's stored folder UUID, falling back to the saved folder
|
/// Resolves a profile's folder UUID, falling back to the legacy folder name
|
||||||
/// name when the folder is missing from `folders.toml`.
|
/// when the folder is missing from `folders.toml`.
|
||||||
fn resolve_profile_folder(
|
fn resolve_profile_folder(
|
||||||
folders: &mut HashMap<String, (String, bool)>,
|
folders: &mut HashMap<String, (String, bool)>,
|
||||||
file: &ProfileFile,
|
file: &ProfileFile,
|
||||||
) -> (String, String) {
|
) -> (String, String) {
|
||||||
if uuid::Uuid::parse_str(file.folder.trim()).is_ok() {
|
let reference = if file.parent_id.trim().is_empty() {
|
||||||
if let Some((path, _)) = folders.iter().find(|(_, (id, _))| id == &file.folder) {
|
file.folder.trim()
|
||||||
return (file.folder.clone(), path.clone());
|
} else {
|
||||||
|
file.parent_id.trim()
|
||||||
|
};
|
||||||
|
|
||||||
|
if uuid::Uuid::parse_str(reference).is_ok()
|
||||||
|
&& let Some((path, _)) = folders.iter().find(|(_, (id, _))| id == reference)
|
||||||
|
{
|
||||||
|
return (reference.to_owned(), path.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
let fallback = if file.folder_name.trim().is_empty() {
|
// Legacy files used the folder path/name instead of its UUID.
|
||||||
DEFAULT_PROFILE_FOLDER.to_owned()
|
let fallback = if !file.folder_name.trim().is_empty() {
|
||||||
} else {
|
|
||||||
normalize_folder_path(&file.folder_name)
|
normalize_folder_path(&file.folder_name)
|
||||||
|
} else if !reference.is_empty() && uuid::Uuid::parse_str(reference).is_err() {
|
||||||
|
normalize_folder_path(reference)
|
||||||
|
} else {
|
||||||
|
DEFAULT_PROFILE_FOLDER.to_owned()
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some((id, _)) = folders.get(&fallback) {
|
if let Some((id, _)) = folders.get(&fallback) {
|
||||||
return (id.clone(), fallback);
|
return (id.clone(), fallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
insert_folder_path(folders, &fallback, Some(file.folder.clone()), true);
|
let id = if uuid::Uuid::parse_str(reference).is_ok() {
|
||||||
return (file.folder.clone(), fallback);
|
reference.to_owned()
|
||||||
|
} else {
|
||||||
|
new_folder_id()
|
||||||
|
};
|
||||||
|
insert_folder_path(folders, &fallback, Some(id.clone()), true);
|
||||||
|
(id, fallback)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Turns the IDs and `parent_id` links in `folders.toml` back into paths.
|
||||||
|
/// Records written before the UUID format carry a full `path` and are kept.
|
||||||
|
fn load_folder_records(folders: &mut HashMap<String, (String, bool)>, file: FoldersFile) {
|
||||||
|
let mut linked: HashMap<String, (String, Option<String>)> = HashMap::new();
|
||||||
|
|
||||||
|
for entry in file.folders {
|
||||||
|
match entry {
|
||||||
|
FolderEntry::Path(path) => {
|
||||||
|
insert_folder_path(
|
||||||
|
folders,
|
||||||
|
&normalize_folder_path(&path),
|
||||||
|
Some(new_folder_id()),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
FolderEntry::Record(record) => {
|
||||||
|
let id = if record.id.trim().is_empty() {
|
||||||
|
new_folder_id()
|
||||||
|
} else {
|
||||||
|
record.id
|
||||||
|
};
|
||||||
|
|
||||||
|
if record.name.trim().is_empty() && !record.path.trim().is_empty() {
|
||||||
|
insert_folder_path(
|
||||||
|
folders,
|
||||||
|
&normalize_folder_path(&record.path),
|
||||||
|
Some(id),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
let parent_id = record.parent_id.filter(|parent| !parent.trim().is_empty());
|
||||||
|
linked.insert(id, (record.name, parent_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = normalize_folder_path(&file.folder);
|
// Insert parents first so `insert_folder_path` never invents a new UUID
|
||||||
if let Some((id, _)) = folders.get(&path) {
|
// for a parent that is already in the file.
|
||||||
return (id.clone(), path);
|
let mut paths: Vec<(String, String)> = linked
|
||||||
|
.keys()
|
||||||
|
.map(|id| (id.clone(), folder_path_from_records(id, &linked)))
|
||||||
|
.collect();
|
||||||
|
paths.sort_by_key(|(_, path)| path.matches('/').count());
|
||||||
|
|
||||||
|
for (id, path) in paths {
|
||||||
|
insert_folder_path(folders, &normalize_folder_path(&path), Some(id), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn folder_path_from_records(
|
||||||
|
id: &str,
|
||||||
|
records: &HashMap<String, (String, Option<String>)>,
|
||||||
|
) -> String {
|
||||||
|
let mut parts = Vec::new();
|
||||||
|
let mut current = Some(id);
|
||||||
|
let mut seen = HashSet::new();
|
||||||
|
|
||||||
|
while let Some(id) = current {
|
||||||
|
if !seen.insert(id) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = new_folder_id();
|
let Some((name, parent_id)) = records.get(id) else {
|
||||||
insert_folder_path(folders, &path, Some(id.clone()), true);
|
break;
|
||||||
(id, path)
|
};
|
||||||
|
|
||||||
|
if !name.trim().is_empty() {
|
||||||
|
parts.push(name.trim().to_owned());
|
||||||
|
}
|
||||||
|
current = parent_id.as_deref();
|
||||||
|
}
|
||||||
|
|
||||||
|
parts.reverse();
|
||||||
|
|
||||||
|
if parts.is_empty() {
|
||||||
|
DEFAULT_PROFILE_FOLDER.to_owned()
|
||||||
|
} else {
|
||||||
|
parts.join("/")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rebuilds the folder tree and profile lists from the `profiles` directory.
|
/// Rebuilds the folder tree and profile lists from the `profiles` directory.
|
||||||
@@ -2098,26 +2203,7 @@ fn load_profiles_from(dir: &Path) -> Vec<ProfileFolder> {
|
|||||||
if let Ok(text) = std::fs::read_to_string(dir.join("folders.toml"))
|
if let Ok(text) = std::fs::read_to_string(dir.join("folders.toml"))
|
||||||
&& let Ok(file) = toml::from_str::<FoldersFile>(&text)
|
&& let Ok(file) = toml::from_str::<FoldersFile>(&text)
|
||||||
{
|
{
|
||||||
for folder in file.folders {
|
load_folder_records(&mut folder_map, file);
|
||||||
let (id, path) = match folder {
|
|
||||||
FolderEntry::Path(path) => (new_folder_id(), path),
|
|
||||||
FolderEntry::Record(record) => (
|
|
||||||
if record.id.trim().is_empty() {
|
|
||||||
new_folder_id()
|
|
||||||
} else {
|
|
||||||
record.id
|
|
||||||
},
|
|
||||||
record.path,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
insert_folder_path(
|
|
||||||
&mut folder_map,
|
|
||||||
&normalize_folder_path(&path),
|
|
||||||
Some(id),
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
insert_folder_path(
|
insert_folder_path(
|
||||||
@@ -2155,11 +2241,20 @@ fn load_profiles_from(dir: &Path) -> Vec<ProfileFolder> {
|
|||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let (folder_id, _) = resolve_profile_folder(&mut folder_map, &file);
|
let (folder_id, folder_path) = resolve_profile_folder(&mut folder_map, &file);
|
||||||
|
let needs_migration = file.parent_id.trim() != folder_id
|
||||||
|
|| !file.folder.trim().is_empty()
|
||||||
|
|| !file.folder_name.trim().is_empty();
|
||||||
|
|
||||||
let mut profile = file.into_profile();
|
let mut profile = file.into_profile();
|
||||||
profile.folder_id = folder_id.clone();
|
profile.folder_id = folder_id.clone();
|
||||||
profile.file = Some(path);
|
profile.file = Some(path);
|
||||||
|
|
||||||
|
// Rewrite files that still place the profile by folder name.
|
||||||
|
if needs_migration {
|
||||||
|
save_profile(&mut profile, &folder_path);
|
||||||
|
}
|
||||||
|
|
||||||
profiles.push((folder_id, profile));
|
profiles.push((folder_id, profile));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2276,14 +2371,6 @@ fn rename_folder(state: &mut Moorly) -> Task<Message> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep the folder-name fallback inside each profile file in sync.
|
|
||||||
for (folder_index, _) in &affected {
|
|
||||||
let folder_path = state.profile_folders[*folder_index].path.clone();
|
|
||||||
for profile in &mut state.profile_folders[*folder_index].profiles {
|
|
||||||
save_profile(profile, &folder_path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
state
|
state
|
||||||
.profile_folders
|
.profile_folders
|
||||||
.sort_by(|a, b| folder_order(&a.path, &b.path));
|
.sort_by(|a, b| folder_order(&a.path, &b.path));
|
||||||
@@ -2376,16 +2463,37 @@ fn save_profile(profile: &mut Profile, folder: &str) {
|
|||||||
fn write_folders_to(dir: &Path, folders: &[ProfileFolder]) -> Option<()> {
|
fn write_folders_to(dir: &Path, folders: &[ProfileFolder]) -> Option<()> {
|
||||||
std::fs::create_dir_all(dir).ok()?;
|
std::fs::create_dir_all(dir).ok()?;
|
||||||
|
|
||||||
let mut records: Vec<FolderRecord> = folders
|
let mut sorted: Vec<&ProfileFolder> =
|
||||||
|
folders.iter().filter(|folder| !folder.untracked).collect();
|
||||||
|
sorted.sort_by(|a, b| folder_order(&a.path, &b.path));
|
||||||
|
sorted.dedup_by(|a, b| a.path == b.path);
|
||||||
|
|
||||||
|
let ids: HashMap<&str, &str> = sorted
|
||||||
|
.iter()
|
||||||
|
.map(|folder| (folder.path.as_str(), folder.id.as_str()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let records: Vec<FolderRecord> = sorted
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|folder| !folder.untracked)
|
|
||||||
.map(|folder| FolderRecord {
|
.map(|folder| FolderRecord {
|
||||||
id: folder.id.clone(),
|
id: folder.id.clone(),
|
||||||
path: folder.path.clone(),
|
name: if folder.name.trim().is_empty() {
|
||||||
|
folder
|
||||||
|
.path
|
||||||
|
.rsplit('/')
|
||||||
|
.next()
|
||||||
|
.unwrap_or(DEFAULT_PROFILE_FOLDER)
|
||||||
|
.to_owned()
|
||||||
|
} else {
|
||||||
|
folder.name.clone()
|
||||||
|
},
|
||||||
|
parent_id: folder
|
||||||
|
.path
|
||||||
|
.rsplit_once('/')
|
||||||
|
.and_then(|(parent, _)| ids.get(parent).map(|id| (*id).to_owned())),
|
||||||
|
path: String::new(),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
records.sort_by(|a, b| folder_order(&a.path, &b.path));
|
|
||||||
records.dedup_by(|a, b| a.path == b.path);
|
|
||||||
|
|
||||||
let text = toml::to_string_pretty(&FoldersOutFile { folders: records }).ok()?;
|
let text = toml::to_string_pretty(&FoldersOutFile { folders: records }).ok()?;
|
||||||
std::fs::write(dir.join("folders.toml"), text).ok()?;
|
std::fs::write(dir.join("folders.toml"), text).ok()?;
|
||||||
@@ -9868,7 +9976,10 @@ mod tests {
|
|||||||
|
|
||||||
super::write_folders_to(&root, &folders).unwrap();
|
super::write_folders_to(&root, &folders).unwrap();
|
||||||
let folders_text = std::fs::read_to_string(root.join("folders.toml")).unwrap();
|
let folders_text = std::fs::read_to_string(root.join("folders.toml")).unwrap();
|
||||||
assert!(folders_text.contains("\"Work/Production\""));
|
assert!(folders_text.contains("name = \"Work\""));
|
||||||
|
assert!(folders_text.contains("name = \"Production\""));
|
||||||
|
assert!(folders_text.contains(&format!("parent_id = \"{}\"", work.id)));
|
||||||
|
assert!(!folders_text.contains("path ="));
|
||||||
assert!(!folders_text.contains("Missing"));
|
assert!(!folders_text.contains("Missing"));
|
||||||
|
|
||||||
std::fs::remove_dir_all(&root).unwrap();
|
std::fs::remove_dir_all(&root).unwrap();
|
||||||
@@ -9883,21 +9994,36 @@ mod tests {
|
|||||||
let root = std::env::temp_dir().join(format!("moorly-profile-uuid-test-{stamp}"));
|
let root = std::env::temp_dir().join(format!("moorly-profile-uuid-test-{stamp}"));
|
||||||
std::fs::create_dir_all(&root).unwrap();
|
std::fs::create_dir_all(&root).unwrap();
|
||||||
let id = super::new_folder_id();
|
let id = super::new_folder_id();
|
||||||
|
let child = super::new_folder_id();
|
||||||
std::fs::write(
|
std::fs::write(
|
||||||
root.join("folders.toml"),
|
root.join("folders.toml"),
|
||||||
format!("[[folders]]\nid = \"{id}\"\npath = \"Work\"\n"),
|
format!(
|
||||||
|
"[[folders]]\nid = \"{id}\"\nname = \"Work\"\n\n\
|
||||||
|
[[folders]]\nid = \"{child}\"\nname = \"Production\"\nparent_id = \"{id}\"\n"
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
std::fs::write(
|
std::fs::write(
|
||||||
root.join("Server.toml"),
|
root.join("Server.toml"),
|
||||||
format!("name = \"Server\"\nfolder = \"{id}\"\nfolder_name = \"Work\"\n"),
|
format!("name = \"Server\"\nparent_id = \"{child}\"\n"),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let folders = super::load_profiles_from(&root);
|
let folders = super::load_profiles_from(&root);
|
||||||
let work = folders.iter().find(|folder| folder.path == "Work").unwrap();
|
let production = folders
|
||||||
assert_eq!(work.id, id);
|
.iter()
|
||||||
assert_eq!(work.profiles[0].folder_id, id);
|
.find(|folder| folder.path == "Work/Production")
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(production.id, child);
|
||||||
|
assert_eq!(production.profiles[0].folder_id, child);
|
||||||
|
assert_eq!(
|
||||||
|
folders
|
||||||
|
.iter()
|
||||||
|
.find(|folder| folder.path == "Work")
|
||||||
|
.unwrap()
|
||||||
|
.id,
|
||||||
|
id
|
||||||
|
);
|
||||||
|
|
||||||
std::fs::remove_dir_all(&root).unwrap();
|
std::fs::remove_dir_all(&root).unwrap();
|
||||||
}
|
}
|
||||||
@@ -9929,8 +10055,9 @@ mod tests {
|
|||||||
assert_eq!(path.file_name().unwrap(), "Server_Test.toml");
|
assert_eq!(path.file_name().unwrap(), "Server_Test.toml");
|
||||||
|
|
||||||
let text = std::fs::read_to_string(&path).unwrap();
|
let text = std::fs::read_to_string(&path).unwrap();
|
||||||
assert!(text.contains("folder = \"folder-id\""));
|
assert!(text.contains("parent_id = \"folder-id\""));
|
||||||
assert!(text.contains("folder_name = \"Work/Production\""));
|
assert!(!text.contains("folder ="));
|
||||||
|
assert!(!text.contains("folder_name ="));
|
||||||
assert!(text.contains("kind = \"ssh\""));
|
assert!(text.contains("kind = \"ssh\""));
|
||||||
assert!(text.contains("session_end = \"reconnect\""));
|
assert!(text.contains("session_end = \"reconnect\""));
|
||||||
assert!(text.contains("host = \"example.com\""));
|
assert!(text.contains("host = \"example.com\""));
|
||||||
|
|||||||
Reference in new issue
Block a user