(self, os: &mut Os)
| 76 | |
| 77 | impl LoginArgs { |
| 78 | pub async fn execute(self, os: &mut Os) -> Result<ExitCode> { |
| 79 | if crate::auth::is_logged_in(&mut os.database).await { |
| 80 | eyre::bail!( |
| 81 | "Already logged in, please logout with {} first", |
| 82 | StyledText::command(&format!("{CLI_BINARY_NAME} logout")) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | let login_method = match self.license { |
| 87 | Some(LicenseType::Free) => AuthMethod::BuilderId, |
| 88 | Some(LicenseType::Pro) => AuthMethod::IdentityCenter, |
| 89 | None => { |
| 90 | if self.identity_provider.is_some() && self.region.is_some() { |
| 91 | // If license is specified and --identity-provider and --region are specified, |
| 92 | // the license is determined to be pro |
| 93 | AuthMethod::IdentityCenter |
| 94 | } else { |
| 95 | // --license is not specified, prompt the user to choose |
| 96 | let options = [AuthMethod::BuilderId, AuthMethod::IdentityCenter]; |
| 97 | let i = match choose("Select login method", &options)? { |
| 98 | Some(i) => i, |
| 99 | None => bail!("No login method selected"), |
| 100 | }; |
| 101 | options[i] |
| 102 | } |
| 103 | }, |
| 104 | }; |
| 105 | |
| 106 | match login_method { |
| 107 | AuthMethod::BuilderId | AuthMethod::IdentityCenter => { |
| 108 | let (start_url, region) = match login_method { |
| 109 | AuthMethod::BuilderId => (None, None), |
| 110 | AuthMethod::IdentityCenter => { |
| 111 | let default_start_url = match self.identity_provider { |
| 112 | Some(start_url) => Some(start_url), |
| 113 | None => os.database.get_start_url()?, |
| 114 | }; |
| 115 | let default_region = match self.region { |
| 116 | Some(region) => Some(region), |
| 117 | None => os.database.get_idc_region()?, |
| 118 | }; |
| 119 | |
| 120 | let start_url = input("Enter Start URL", default_start_url.as_deref())?; |
| 121 | let region = input("Enter Region", default_region.as_deref())?.trim().to_string(); |
| 122 | |
| 123 | let _ = os.database.set_start_url(start_url.clone()); |
| 124 | let _ = os.database.set_idc_region(region.clone()); |
| 125 | |
| 126 | (Some(start_url), Some(region)) |
| 127 | }, |
| 128 | }; |
| 129 | |
| 130 | // Remote machine won't be able to handle browser opening and redirects, |
| 131 | // hence always use device code flow. |
| 132 | if is_remote() || self.use_device_flow { |
| 133 | try_device_authorization(os, start_url.clone(), region.clone()).await?; |
| 134 | } else { |
| 135 | let (client, registration) = start_pkce_authorization(start_url.clone(), region.clone()).await?; |
nothing calls this directly
no test coverage detected