|
| 1 | +use anyhow::Result; |
| 2 | +use crates_io_github::{GitHubClient, RealGitHubClient}; |
| 3 | +use oauth2::AccessToken; |
| 4 | +use reqwest::Client; |
| 5 | +use secrecy::{ExposeSecret, SecretString}; |
| 6 | +use tracing::level_filters::LevelFilter; |
| 7 | +use tracing_subscriber::EnvFilter; |
| 8 | + |
| 9 | +#[derive(clap::Parser, Debug)] |
| 10 | +enum Request { |
| 11 | + CurrentUser { |
| 12 | + #[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)] |
| 13 | + access_token: SecretString, |
| 14 | + }, |
| 15 | + OrgByName { |
| 16 | + org_name: String, |
| 17 | + #[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)] |
| 18 | + access_token: SecretString, |
| 19 | + }, |
| 20 | + TeamByName { |
| 21 | + org_name: String, |
| 22 | + team_name: String, |
| 23 | + #[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)] |
| 24 | + access_token: SecretString, |
| 25 | + }, |
| 26 | + OrgMembership { |
| 27 | + org_id: i32, |
| 28 | + username: String, |
| 29 | + #[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)] |
| 30 | + access_token: SecretString, |
| 31 | + }, |
| 32 | + TeamMembership { |
| 33 | + org_id: i32, |
| 34 | + team_id: i32, |
| 35 | + username: String, |
| 36 | + #[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)] |
| 37 | + access_token: SecretString, |
| 38 | + }, |
| 39 | + PublicKeys { |
| 40 | + client_id: String, |
| 41 | + #[clap(long, env = "GITHUB_CLIENT_SECRET", hide_env_values = true)] |
| 42 | + client_secret: SecretString, |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +#[tokio::main] |
| 47 | +async fn main() -> Result<()> { |
| 48 | + use clap::Parser; |
| 49 | + |
| 50 | + init_tracing(); |
| 51 | + |
| 52 | + let client = Client::new(); |
| 53 | + let github_client = RealGitHubClient::new(client); |
| 54 | + |
| 55 | + match Request::parse() { |
| 56 | + Request::CurrentUser { access_token } => { |
| 57 | + let access_token = AccessToken::new(access_token.expose_secret().into()); |
| 58 | + let response = github_client.current_user(&access_token).await?; |
| 59 | + println!("{response:#?}"); |
| 60 | + } |
| 61 | + Request::OrgByName { |
| 62 | + org_name, |
| 63 | + access_token, |
| 64 | + } => { |
| 65 | + let access_token = AccessToken::new(access_token.expose_secret().into()); |
| 66 | + let response = github_client.org_by_name(&org_name, &access_token).await?; |
| 67 | + println!("{response:#?}"); |
| 68 | + } |
| 69 | + Request::TeamByName { |
| 70 | + org_name, |
| 71 | + team_name, |
| 72 | + access_token, |
| 73 | + } => { |
| 74 | + let access_token = AccessToken::new(access_token.expose_secret().into()); |
| 75 | + let response = github_client |
| 76 | + .team_by_name(&org_name, &team_name, &access_token) |
| 77 | + .await?; |
| 78 | + println!("{response:#?}"); |
| 79 | + } |
| 80 | + Request::OrgMembership { |
| 81 | + org_id, |
| 82 | + username, |
| 83 | + access_token, |
| 84 | + } => { |
| 85 | + let access_token = AccessToken::new(access_token.expose_secret().into()); |
| 86 | + let response = github_client |
| 87 | + .org_membership(org_id, &username, &access_token) |
| 88 | + .await?; |
| 89 | + println!("{response:#?}"); |
| 90 | + } |
| 91 | + Request::TeamMembership { |
| 92 | + org_id, |
| 93 | + team_id, |
| 94 | + username, |
| 95 | + access_token, |
| 96 | + } => { |
| 97 | + let access_token = AccessToken::new(access_token.expose_secret().into()); |
| 98 | + let response = github_client |
| 99 | + .team_membership(org_id, team_id, &username, &access_token) |
| 100 | + .await?; |
| 101 | + println!("{response:#?}"); |
| 102 | + } |
| 103 | + Request::PublicKeys { |
| 104 | + client_id, |
| 105 | + client_secret, |
| 106 | + } => { |
| 107 | + let response = github_client |
| 108 | + .public_keys(&client_id, client_secret.expose_secret()) |
| 109 | + .await?; |
| 110 | + println!("{response:#?}"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + Ok(()) |
| 115 | +} |
| 116 | + |
| 117 | +fn init_tracing() { |
| 118 | + let env_filter = EnvFilter::builder() |
| 119 | + .with_default_directive(LevelFilter::DEBUG.into()) |
| 120 | + .from_env_lossy(); |
| 121 | + |
| 122 | + tracing_subscriber::fmt() |
| 123 | + .compact() |
| 124 | + .with_env_filter(env_filter) |
| 125 | + .init(); |
| 126 | +} |
0 commit comments