(
api_client: &CodSpeedAPIClient,
config_name: Option<&str>,
mut config: CodSpeedConfig,
with_token: bool,
)
| 53 | const LOGIN_SESSION_MAX_DURATION: Duration = Duration::from_secs(60 * 5); // 5 minutes |
| 54 | |
| 55 | async fn login( |
| 56 | api_client: &CodSpeedAPIClient, |
| 57 | config_name: Option<&str>, |
| 58 | mut config: CodSpeedConfig, |
| 59 | with_token: bool, |
| 60 | ) -> Result<()> { |
| 61 | debug!("Login to CodSpeed"); |
| 62 | |
| 63 | let token = if with_token { |
| 64 | let mut buf = String::new(); |
| 65 | std::io::stdin().read_to_string(&mut buf)?; |
| 66 | let token = buf.trim().to_owned(); |
| 67 | if token.is_empty() { |
| 68 | bail!("No token provided on stdin"); |
| 69 | } |
| 70 | token |
| 71 | } else { |
| 72 | start_group!("Creating login session"); |
| 73 | let login_session_payload = api_client.create_login_session().await?; |
| 74 | end_group!(); |
| 75 | |
| 76 | if open::that(&login_session_payload.callback_url).is_ok() { |
| 77 | info!("Your browser has been opened to complete the login process"); |
| 78 | } else { |
| 79 | warn!("Failed to open the browser automatically, please open the URL manually"); |
| 80 | } |
| 81 | info!( |
| 82 | "Authentication URL: {}\n", |
| 83 | style(login_session_payload.callback_url) |
| 84 | .blue() |
| 85 | .bold() |
| 86 | .underlined() |
| 87 | ); |
| 88 | |
| 89 | start_group!("Waiting for the login to be completed"); |
| 90 | let token; |
| 91 | let start = Instant::now(); |
| 92 | loop { |
| 93 | if start.elapsed() > LOGIN_SESSION_MAX_DURATION { |
| 94 | bail!("Login session expired, please try again"); |
| 95 | } |
| 96 | |
| 97 | match api_client |
| 98 | .consume_login_session(&login_session_payload.session_id) |
| 99 | .await? |
| 100 | .token |
| 101 | { |
| 102 | Some(token_from_api) => { |
| 103 | token = token_from_api; |
| 104 | break; |
| 105 | } |
| 106 | None => sleep(Duration::from_secs(5)).await, |
| 107 | } |
| 108 | } |
| 109 | end_group!(); |
| 110 | token |
| 111 | }; |
| 112 |
no test coverage detected