(os: &mut Os, start_url: Option<String>, region: Option<String>)
| 288 | } |
| 289 | |
| 290 | async fn try_device_authorization(os: &mut Os, start_url: Option<String>, region: Option<String>) -> Result<()> { |
| 291 | let device_auth = start_device_authorization(&os.database, start_url.clone(), region.clone()).await?; |
| 292 | |
| 293 | println!(); |
| 294 | println!("Confirm the following code in the browser"); |
| 295 | println!("Code: {}", StyledText::emphasis(&device_auth.user_code)); |
| 296 | println!(); |
| 297 | |
| 298 | let print_open_url = || println!("Open this URL: {}", device_auth.verification_uri_complete); |
| 299 | |
| 300 | if is_remote() { |
| 301 | print_open_url(); |
| 302 | } else if let Err(err) = crate::util::open::open_url_async(&device_auth.verification_uri_complete).await { |
| 303 | error!(%err, "Failed to open URL with browser"); |
| 304 | print_open_url(); |
| 305 | } |
| 306 | |
| 307 | let mut spinner = Spinner::new(vec![ |
| 308 | SpinnerComponent::Spinner, |
| 309 | SpinnerComponent::Text(" Logging in...".into()), |
| 310 | ]); |
| 311 | |
| 312 | loop { |
| 313 | let ctrl_c_stream = ctrl_c(); |
| 314 | tokio::select! { |
| 315 | _ = tokio::time::sleep(Duration::from_secs(device_auth.interval.try_into().unwrap_or(1))) => (), |
| 316 | Ok(_) = ctrl_c_stream => { |
| 317 | #[allow(clippy::exit)] |
| 318 | exit(1); |
| 319 | } |
| 320 | } |
| 321 | match poll_create_token( |
| 322 | &os.database, |
| 323 | device_auth.device_code.clone(), |
| 324 | start_url.clone(), |
| 325 | region.clone(), |
| 326 | &os.telemetry, |
| 327 | ) |
| 328 | .await |
| 329 | { |
| 330 | PollCreateToken::Pending => {}, |
| 331 | PollCreateToken::Complete => { |
| 332 | os.telemetry.send_user_logged_in().ok(); |
| 333 | spinner.stop_with_message("Logged in".into()); |
| 334 | break; |
| 335 | }, |
| 336 | PollCreateToken::Error(err) => { |
| 337 | spinner.stop(); |
| 338 | return Err(err.into()); |
| 339 | }, |
| 340 | }; |
| 341 | } |
| 342 | Ok(()) |
| 343 | } |
| 344 | |
| 345 | async fn select_profile_interactive(os: &mut Os, whoami: bool) -> Result<()> { |
| 346 | let mut spinner = Spinner::new(vec![ |
no test coverage detected