`repo identity adopt` — fetch the remote binding and force-adopt it.
(root: &std::path::Path, remote: Option<&str>, json: bool)
| 99 | |
| 100 | /// `repo identity adopt` — fetch the remote binding and force-adopt it. |
| 101 | fn adopt(root: &std::path::Path, remote: Option<&str>, json: bool) -> CliResult<()> { |
| 102 | use atomic_remote::{HttpRemote, HttpRemoteConfig}; |
| 103 | let repository = atomic_repository::Repository::open(root).map_err(CliError::Repository)?; |
| 104 | let remote_name = remote.unwrap_or("origin").to_string(); |
| 105 | let entry = match repository.get_remote(&remote_name) { |
| 106 | Ok(entry) => entry, |
| 107 | Err(atomic_repository::RepositoryError::RemoteNotFound { .. }) => { |
| 108 | return Err(CliError::InvalidArgument { |
| 109 | message: format!( |
| 110 | "no remote named '{remote_name}'; add one with 'atomic remote add {remote_name} <url>' or pass --remote" |
| 111 | ), |
| 112 | }); |
| 113 | } |
| 114 | Err(e) => return Err(CliError::Repository(e)), |
| 115 | }; |
| 116 | let rt = tokio::runtime::Runtime::new().map_err(|e| { |
| 117 | CliError::Internal(anyhow::anyhow!("Failed to create async runtime: {e}")) |
| 118 | })?; |
| 119 | let (project_id, previous) = rt.block_on(async { |
| 120 | let config = |
| 121 | atomic_remote::auth::attach_identity(HttpRemoteConfig::default(), &entry.url, None) |
| 122 | .await; |
| 123 | let remote = HttpRemote::with_config(&entry.url, config).map_err(|e| { |
| 124 | CliError::InvalidArgument { |
| 125 | message: format!("cannot reach remote {}: {e}", entry.url), |
| 126 | } |
| 127 | })?; |
| 128 | let previous = repository |
| 129 | .project_identity() |
| 130 | .map_err(CliError::Repository)? |
| 131 | .map(|record| { |
| 132 | record |
| 133 | .project_id() |
| 134 | .map(|id| id.to_base32()) |
| 135 | .unwrap_or_else(|_| "<unreadable>".to_string()) |
| 136 | }); |
| 137 | let project_id = libatomic::identity_transport::adopt_from_remote(&repository, &remote) |
| 138 | .await |
| 139 | .map_err(CliError::Repository)?; |
| 140 | Ok::<_, CliError>((project_id, previous)) |
| 141 | })?; |
| 142 | if json { |
| 143 | println!( |
| 144 | "{}", |
| 145 | serde_json::to_string_pretty(&serde_json::json!({ |
| 146 | "adopted": project_id, |
| 147 | "urn": format!("urn:atomic:project:{project_id}"), |
| 148 | })) |
| 149 | .expect("JSON serialization") |
| 150 | ); |
| 151 | } else { |
| 152 | if let Some(previous) = previous.filter(|p| *p != project_id) { |
| 153 | println!("Replaced local identity {previous}"); |
| 154 | } |
| 155 | println!("Adopted project identity {project_id}"); |
| 156 | println!("The derived knowledge graph was rebuilt under the adopted id."); |
| 157 | } |
| 158 | Ok(()) |
nothing calls this directly
no test coverage detected