Skip to content

Commit

Permalink
Extract code
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Mar 19, 2024
1 parent e6ba059 commit c552453
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod octocrab_utils;
mod parser;
mod tracing;

Expand Down
35 changes: 35 additions & 0 deletions src/cli/octocrab_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use anyhow::Result;
use futures::TryStreamExt;
use secrecy::ExposeSecret;

use crate::{cli, constants::APP_ID};

use super::Auth;

pub(super) async fn auth_to_octocrab<A>(auth: &mut A) -> Result<octocrab::Octocrab>
where
A: TryInto<Auth, Error = anyhow::Error> + Default,
{
match std::mem::take(auth).try_into()? {
cli::Auth::AppKey(app_key) => {
let key = jsonwebtoken::EncodingKey::from_rsa_pem(app_key.expose_secret().as_bytes())?;
let base = octocrab::Octocrab::builder().app(APP_ID, key).build()?;
let insts = base
.apps()
.installations()
.send()
.await?
.into_stream(&base)
.try_collect::<Vec<_>>()
.await?;
tracing::info!("Installations: {}", serde_json5::to_string(&insts)?);
Ok(octocrab::Octocrab::installation(&base, insts[0].id))
}
cli::Auth::GitHubToken(github_token) => {
Ok(octocrab::Octocrab::builder()
// https://github.com/XAMPPRocky/octocrab/issues/594
.personal_token(github_token.expose_secret().to_owned())
.build()?)
}
}
}
9 changes: 9 additions & 0 deletions src/cli/parser.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use clap::{Args, Parser, Subcommand};
use serde::Deserialize;

use anyhow::Result;
use secrecy::SecretString;
use std::fmt::Display;

use crate::{constants::ORG, utils::get_credential};

use super::octocrab_utils::auth_to_octocrab;

#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
Expand All @@ -30,6 +33,12 @@ pub(crate) struct AuthInner {
github_token: Option<SecretString>,
}

impl AuthInner {
pub(crate) async fn into_octocrab(&mut self) -> Result<octocrab::Octocrab> {
auth_to_octocrab(self).await
}
}

pub(crate) enum Auth {
AppKey(SecretString),
GitHubToken(SecretString),
Expand Down
31 changes: 1 addition & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use anyhow::{anyhow, Result};
use clap::Parser;
use futures::TryStreamExt;
use secrecy::ExposeSecret;

mod benchmark;
mod cli;
Expand All @@ -21,35 +19,8 @@ async fn main() -> Result<()> {

let mut cli = cli::Cli::parse();

// initialize octocrab
match std::mem::take(&mut cli.auth).try_into()? {
cli::Auth::AppKey(app_key) => {
let key = jsonwebtoken::EncodingKey::from_rsa_pem(app_key.expose_secret().as_bytes())?;
let base = octocrab::Octocrab::builder()
.app(constants::APP_ID, key)
.build()?;
let insts = base
.apps()
.installations()
.send()
.await?
.into_stream(&base)
.try_collect::<Vec<_>>()
.await?;
tracing::info!("Installations: {}", serde_json5::to_string(&insts)?);
let crab = octocrab::Octocrab::installation(&base, insts[0].id);
octocrab::initialise(crab);
}
cli::Auth::GitHubToken(github_token) => {
let crab = octocrab::Octocrab::builder()
// https://github.com/XAMPPRocky/octocrab/issues/594
.personal_token(github_token.expose_secret().to_owned())
.build()?;
octocrab::initialise(crab);
}
};
octocrab::initialise(cli.auth.into_octocrab().await?);

// run command
match cli.command {
cli::Commands::Serve(args) => {
server::serve(args).await?;
Expand Down

0 comments on commit c552453

Please sign in to comment.