Add a new server profile.
(&self, add: &ServerAdd)
| 226 | |
| 227 | /// Add a new server profile. |
| 228 | fn add_server(&self, add: &ServerAdd) -> CliResult<()> { |
| 229 | if !add.url.contains("://") { |
| 230 | return Err(CliError::InvalidArgument { |
| 231 | message: format!( |
| 232 | "Invalid URL '{}': must include a scheme (e.g. https://)", |
| 233 | add.url |
| 234 | ), |
| 235 | }); |
| 236 | } |
| 237 | |
| 238 | let mut config = GlobalConfig::load() |
| 239 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to load config: {}", e)))?; |
| 240 | |
| 241 | if config.servers.contains_key(&add.name) { |
| 242 | return Err(CliError::InvalidArgument { |
| 243 | message: format!( |
| 244 | "Server profile '{}' already exists. \ |
| 245 | Use 'atomic server set-identity' or 'atomic server remove' to modify it.", |
| 246 | add.name |
| 247 | ), |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | let profile = ServerConfig { |
| 252 | url: Some(add.url.trim_end_matches('/').to_string()), |
| 253 | default_org: add.org.clone(), |
| 254 | default_workspaces: std::collections::BTreeMap::new(), |
| 255 | identity: add.identity.clone(), |
| 256 | }; |
| 257 | |
| 258 | config.servers.insert(add.name.clone(), profile); |
| 259 | |
| 260 | if add.set_default { |
| 261 | config.default_server = Some(add.name.clone()); |
| 262 | } |
| 263 | |
| 264 | config |
| 265 | .save() |
| 266 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to save config: {}", e)))?; |
| 267 | |
| 268 | print_success(&format!("Server profile '{}' added", add.name)); |
| 269 | println!(" URL: {}", add.url); |
| 270 | if let Some(ref org) = add.org { |
| 271 | println!(" Org: {}", org); |
| 272 | } |
| 273 | if let Some(ref identity) = add.identity { |
| 274 | println!(" Identity: {}", identity); |
| 275 | } |
| 276 | if add.set_default { |
| 277 | println!(); |
| 278 | print_success(&format!("'{}' is now the active server", add.name)); |
| 279 | } else { |
| 280 | println!(); |
| 281 | print_hint(&format!( |
| 282 | "To make this your default: atomic server set {}", |
| 283 | add.name |
| 284 | )); |
| 285 | } |
no test coverage detected