Skip to main content

deslicer_cli/commands/
pipeline.rs

1use crate::ci::{self, CiPlatform, OidcError};
2use crate::errors::CliError;
3use crate::observer_client::Client;
4use crate::resolver::ResolvedBackend;
5use crate::Ctx;
6
7pub struct AuthenticatedSession {
8    pub platform: CiPlatform,
9    pub backend: ResolvedBackend,
10}
11
12pub async fn authenticate(
13    ctx: &Ctx,
14    environment: Option<&str>,
15    plan_id: Option<&str>,
16) -> Result<(AuthenticatedSession, Client), CliError> {
17    let platform = ci::detect_platform(ctx.ci_override);
18    let jwt = ci::provider_for(platform)
19        .fetch_token(ci::AUDIENCE)
20        .await
21        .map_err(map_oidc_error)?;
22    let backend = crate::resolver::resolve(ctx, &jwt, platform, environment, plan_id).await?;
23    let token =
24        crate::oidc_exchange::exchange(&backend.observer_api_url, &jwt, platform, environment)
25            .await?;
26    let client = Client::new(backend.observer_api_url.clone(), token);
27    Ok((AuthenticatedSession { platform, backend }, client))
28}
29
30pub fn map_oidc_error(err: OidcError) -> CliError {
31    match err {
32        OidcError::MissingEnv(msg) => CliError::Other(msg),
33        OidcError::Http(msg) => CliError::Transport(msg),
34        OidcError::Unsupported(msg) => CliError::UnsupportedPlatform(msg),
35        OidcError::Other(msg) => CliError::Other(msg),
36    }
37}
38
39pub fn map_cli_error(err: CliError) -> i32 {
40    eprintln!("{err}");
41    err.exit_code()
42}