use crate::api_client::{ApiClient, ProjectListItem};
use crate::config::TracevaultConfig;
use crate::program::CLI;
pub fn resolve_unique_project<'a>(
items: &'a [ProjectListItem],
name: &str,
) -> Result<&'a ProjectListItem, String> {
let matches: Vec<&ProjectListItem> = items.iter().filter(|p| p.name == name).collect();
match matches.as_slice() {
[one] => Ok(one),
[] => {
let mut names: Vec<&str> = items.iter().map(|p| p.name.as_str()).collect();
names.sort_unstable();
names.dedup();
Err(format!(
"project '{name}' not found. Available: {}",
if names.is_empty() {
"(none visible to this account)".to_string()
} else {
names.join(", ")
}
))
}
many => {
let ids: Vec<String> = many.iter().map(|p| p.id.to_string()).collect();
Err(format!(
"project name '{name}' is ambiguous: {} projects carry it ({}); set \
`project_id = \"<id>\"` in .visdom/tracing.toml by hand",
many.len(),
ids.join(", ")
))
}
}
}
#[derive(Debug)]
pub enum ResolveNameError {
Unreachable(String),
Name(String),
}
impl std::fmt::Display for ResolveNameError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ResolveNameError::Unreachable(e) => write!(f, "could not list projects: {e}"),
ResolveNameError::Name(e) => f.write_str(e),
}
}
}
pub async fn resolve_project_by_name(
client: &ApiClient,
name: &str,
) -> Result<ProjectListItem, ResolveNameError> {
let items = client
.list_projects()
.await
.map_err(|e| ResolveNameError::Unreachable(e.to_string()))?;
resolve_unique_project(&items, name)
.map(|p| ProjectListItem {
id: p.id,
name: p.name.clone(),
})
.map_err(ResolveNameError::Name)
}
fn has_valid_project_id(config: &TracevaultConfig) -> bool {
crate::resolution::project_from_config(config)
.as_ref()
.and_then(crate::resolution::capture_project_id)
.is_some()
}
#[derive(Debug, PartialEq, Eq)]
pub enum Migration {
NotNeeded,
Migrated {
name: String,
project_id: uuid::Uuid,
},
DroppedLegacy { name: String },
}
pub async fn migrate_default_project(
config: &mut TracevaultConfig,
client: &ApiClient,
) -> Result<Migration, ResolveNameError> {
let Some(name) = config.default_project.clone() else {
return Ok(Migration::NotNeeded);
};
if has_valid_project_id(config) {
config.default_project = None;
return Ok(Migration::DroppedLegacy { name });
}
let project = resolve_project_by_name(client, &name)
.await
.map_err(|e| match e {
ResolveNameError::Name(msg) => {
ResolveNameError::Name(format!("cannot migrate default_project '{name}': {msg}"))
}
other => other,
})?;
config.project_id = Some(project.id.to_string());
config.project_name = Some(project.name);
config.default_project = None;
Ok(Migration::Migrated {
name,
project_id: project.id,
})
}
pub fn migration_report(migration: &Migration) -> Option<String> {
match migration {
Migration::NotNeeded => None,
Migration::Migrated { name, project_id } => Some(format!(
"Migrated .visdom/tracing.toml: default_project '{name}' -> project_id {project_id}"
)),
Migration::DroppedLegacy { name } => Some(format!(
"Removed the legacy default_project '{name}' from .visdom/tracing.toml \
(its project_id is what attribution uses)"
)),
}
}
pub fn legacy_default_project_notice(config: &TracevaultConfig) -> Option<String> {
let name = config.default_project.as_deref()?;
Some(if has_valid_project_id(config) {
format!(
"default_project \"{name}\" in .visdom/tracing.toml is ignored (its project_id \
wins); run `{CLI} sync` to remove it"
)
} else {
format!(
"default_project \"{name}\" is not used for capture until migrated; run \
`{CLI} sync`"
)
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::{http_json, spawn_seq};
fn item(id: u128, name: &str) -> ProjectListItem {
ProjectListItem {
id: uuid::Uuid::from_u128(id),
name: name.into(),
}
}
#[test]
fn resolve_unique_project_exact_unknown_and_ambiguous() {
let items = vec![item(1, "visdom"), item(2, "web"), item(3, "web")];
assert_eq!(
resolve_unique_project(&items, "visdom").unwrap().id,
uuid::Uuid::from_u128(1)
);
let unknown = resolve_unique_project(&items, "Visdom").unwrap_err();
assert!(unknown.contains("'Visdom' not found"), "{unknown}");
assert!(unknown.contains("visdom, web"), "{unknown}");
let ambiguous = resolve_unique_project(&items, "web").unwrap_err();
assert!(ambiguous.contains("ambiguous"), "{ambiguous}");
assert!(
ambiguous.contains(&uuid::Uuid::from_u128(2).to_string())
&& ambiguous.contains(&uuid::Uuid::from_u128(3).to_string()),
"{ambiguous}"
);
}
const LIST: &str = r#"[{"id":"00000000-0000-4000-8000-0000000000aa","name":"visdom"},{"id":"00000000-0000-4000-8000-0000000000bb","name":"dup"},{"id":"00000000-0000-4000-8000-0000000000cc","name":"dup"}]"#;
#[tokio::test]
async fn migrate_resolves_the_name_and_drops_the_legacy_key() {
let (base, _rx) = spawn_seq(vec![http_json("200 OK", LIST)]);
let client = ApiClient::with_credential(&base, None);
let mut cfg = TracevaultConfig {
default_project: Some("visdom".into()),
..Default::default()
};
let got = migrate_default_project(&mut cfg, &client).await.unwrap();
let id: uuid::Uuid = "00000000-0000-4000-8000-0000000000aa".parse().unwrap();
assert_eq!(
got,
Migration::Migrated {
name: "visdom".into(),
project_id: id
}
);
assert_eq!(cfg.project_id.as_deref(), Some(id.to_string().as_str()));
assert_eq!(cfg.project_name.as_deref(), Some("visdom"));
assert_eq!(cfg.default_project, None);
}
#[tokio::test]
async fn migrate_unknown_or_ambiguous_name_errors_and_leaves_config_unchanged() {
for name in ["nope", "dup"] {
let (base, _rx) = spawn_seq(vec![http_json("200 OK", LIST)]);
let client = ApiClient::with_credential(&base, None);
let mut cfg = TracevaultConfig {
default_project: Some(name.into()),
..Default::default()
};
let err = migrate_default_project(&mut cfg, &client)
.await
.unwrap_err();
assert!(matches!(err, ResolveNameError::Name(_)), "{err}");
assert!(err.to_string().contains(&format!("'{name}'")), "{err}");
assert_eq!(cfg.default_project.as_deref(), Some(name));
assert_eq!(cfg.project_id, None);
assert_eq!(cfg.project_name, None);
}
}
#[tokio::test]
async fn migrate_unreachable_server_leaves_config_unchanged() {
let (base, _rx) = spawn_seq(vec![http_json("500 Internal Server Error", "{}")]);
let client = ApiClient::with_credential(&base, None);
let mut cfg = TracevaultConfig {
default_project: Some("visdom".into()),
..Default::default()
};
let err = migrate_default_project(&mut cfg, &client)
.await
.unwrap_err();
assert!(matches!(err, ResolveNameError::Unreachable(_)), "{err}");
assert_eq!(cfg.default_project.as_deref(), Some("visdom"));
assert_eq!(cfg.project_id, None);
}
#[tokio::test]
async fn migrate_with_project_id_present_drops_legacy_without_a_lookup() {
let client = ApiClient::with_credential("http://127.0.0.1:9", None);
let mut cfg = TracevaultConfig {
default_project: Some("old".into()),
project_id: Some("00000000-0000-4000-8000-0000000000aa".into()),
project_name: Some("visdom".into()),
..Default::default()
};
let got = migrate_default_project(&mut cfg, &client).await.unwrap();
assert_eq!(got, Migration::DroppedLegacy { name: "old".into() });
assert_eq!(cfg.default_project, None);
assert_eq!(cfg.project_name.as_deref(), Some("visdom"));
}
#[tokio::test]
async fn migrate_with_an_invalid_project_id_resolves_the_name() {
let (base, _rx) = spawn_seq(vec![http_json("200 OK", LIST)]);
let client = ApiClient::with_credential(&base, None);
let mut cfg = TracevaultConfig {
default_project: Some("visdom".into()),
project_id: Some("not-a-uuid".into()),
..Default::default()
};
let got = migrate_default_project(&mut cfg, &client).await.unwrap();
assert!(matches!(got, Migration::Migrated { .. }), "{got:?}");
assert_eq!(
cfg.project_id.as_deref(),
Some("00000000-0000-4000-8000-0000000000aa")
);
assert_eq!(cfg.default_project, None);
let unmigrated = TracevaultConfig {
default_project: Some("visdom".into()),
project_id: Some("not-a-uuid".into()),
..Default::default()
};
assert!(legacy_default_project_notice(&unmigrated)
.unwrap()
.contains("until migrated"));
}
#[tokio::test]
async fn migrate_without_legacy_key_is_a_no_op() {
let client = ApiClient::with_credential("http://127.0.0.1:9", None);
let mut cfg = TracevaultConfig::default();
assert_eq!(
migrate_default_project(&mut cfg, &client).await.unwrap(),
Migration::NotNeeded
);
}
#[test]
fn legacy_notice_unmigrated_vs_shadowed() {
let mut cfg = TracevaultConfig {
default_project: Some("visdom".into()),
..Default::default()
};
assert_eq!(
legacy_default_project_notice(&cfg).unwrap(),
"default_project \"visdom\" is not used for capture until migrated; run `tracevault sync`"
);
cfg.project_id = Some("00000000-0000-4000-8000-0000000000aa".into());
assert!(legacy_default_project_notice(&cfg)
.unwrap()
.contains("ignored (its project_id wins)"));
cfg.default_project = None;
assert_eq!(legacy_default_project_notice(&cfg), None);
}
}