-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
799 additions
and
396 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pub(self) mod checker; | ||
pub mod checker; | ||
#[cfg(feature = "update")] | ||
mod command; | ||
mod parse; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use anyhow::{Context, Result}; | ||
use clap::Parser; | ||
use hop::webhooks::types::{PossibleEvents, EVENT_CATEGORIES, EVENT_NAMES}; | ||
|
||
use super::utils::string_to_event; | ||
use crate::state::State; | ||
use crate::utils::urlify; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(about = "Create a new webhook")] | ||
#[group(skip)] | ||
pub struct Options { | ||
#[clap(short, long, help = "The url to send the webhook to")] | ||
pub url: Option<String>, | ||
#[clap(short, long, help = "The events to send the webhook on", value_parser = string_to_event )] | ||
pub events: Vec<PossibleEvents>, | ||
} | ||
|
||
pub async fn handle(options: Options, state: State) -> Result<()> { | ||
let project = state.ctx.current_project_error()?; | ||
|
||
let url = if let Some(url) = options.url { | ||
url | ||
} else { | ||
dialoguer::Input::new() | ||
.with_prompt("Webhook URL") | ||
.interact_text()? | ||
}; | ||
|
||
let events = if !options.events.is_empty() { | ||
options.events | ||
} else { | ||
let mut events = vec![]; | ||
let mut start_idx = 0usize; | ||
|
||
for (name, end_idx) in EVENT_CATEGORIES { | ||
let end_idx = end_idx as usize + start_idx; | ||
|
||
for (_, event) in &EVENT_NAMES[start_idx..end_idx] { | ||
events.push(format!("{name}: {event}")) | ||
} | ||
|
||
start_idx = end_idx; | ||
} | ||
|
||
let dialoguer_events = loop { | ||
let test = dialoguer::MultiSelect::new() | ||
.with_prompt("Select events") | ||
.items(&events) | ||
.interact()?; | ||
|
||
if !test.is_empty() { | ||
break test; | ||
} | ||
}; | ||
|
||
EVENT_NAMES | ||
.into_iter() | ||
.enumerate() | ||
.filter(|(idx, _)| dialoguer_events.contains(idx)) | ||
.map(|(_, (event, _))| event) | ||
.collect() | ||
}; | ||
|
||
let webhook = state | ||
.hop | ||
.webhooks | ||
.create(&project.id, &url, &events) | ||
.await?; | ||
|
||
log::info!("Webhook successfully created. ID: {}\n", webhook.id); | ||
log::info!("This is your webhook's secret, this is how you will authenticate traffic coming to your endpoint"); | ||
log::info!("Webhook Header: {}", urlify("X-Hop-Hooks-Signature")); | ||
log::info!( | ||
"Webhook Secret: {}", | ||
urlify( | ||
&webhook | ||
.secret | ||
.context("Webhook secret was expected to be present")? | ||
) | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use anyhow::{Context, Result}; | ||
use clap::Parser; | ||
|
||
use crate::commands::webhooks::utils::format_webhooks; | ||
use crate::state::State; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(about = "Delete a webhook")] | ||
#[group(skip)] | ||
pub struct Options { | ||
#[clap(short, long, help = "The id of the webhook")] | ||
pub id: Option<String>, | ||
} | ||
|
||
pub async fn handle(options: Options, state: State) -> Result<()> { | ||
let project = state.ctx.current_project_error()?; | ||
|
||
let all = state.hop.webhooks.get_all(&project.id).await?; | ||
|
||
let webhook = if let Some(id) = options.id { | ||
all.into_iter() | ||
.find(|webhook| webhook.id == id) | ||
.context("Webhook not found")? | ||
} else { | ||
let formatted_webhooks = format_webhooks(&all, false); | ||
|
||
let idx = dialoguer::Select::new() | ||
.with_prompt("Select a webhook to update") | ||
.items(&formatted_webhooks) | ||
.default(0) | ||
.interact()?; | ||
|
||
all[idx].clone() | ||
}; | ||
|
||
state.hop.webhooks.delete(&project.id, &webhook.id).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
|
||
use super::utils::format_webhooks; | ||
use crate::state::State; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(about = "List webhooks")] | ||
#[group(skip)] | ||
pub struct Options { | ||
#[clap(short, long, help = "Only print the IDs")] | ||
pub quiet: bool, | ||
} | ||
|
||
pub async fn handle(options: Options, state: State) -> Result<()> { | ||
let project = state.ctx.current_project_error()?; | ||
|
||
let webhooks = state.hop.webhooks.get_all(&project.id).await?; | ||
|
||
if options.quiet { | ||
let ids = webhooks | ||
.iter() | ||
.map(|d| d.id.as_str()) | ||
.collect::<Vec<_>>() | ||
.join(" "); | ||
|
||
println!("{ids}"); | ||
} else { | ||
let webhooks_fmt = format_webhooks(&webhooks, true); | ||
|
||
println!("{}", webhooks_fmt.join("\n")); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
mod create; | ||
mod delete; | ||
mod list; | ||
mod regenerate; | ||
mod update; | ||
mod utils; | ||
|
||
use anyhow::Result; | ||
use clap::{Parser, Subcommand}; | ||
|
||
use crate::state::State; | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum Commands { | ||
#[clap(name = "ls", alias = "list")] | ||
List(list::Options), | ||
#[clap(name = "new", alias = "create", alias = "add")] | ||
Create(create::Options), | ||
#[clap(name = "update", alias = "edit")] | ||
Update(update::Options), | ||
#[clap(name = "rm", alias = "delete", alias = "del")] | ||
Delete(delete::Options), | ||
#[clap(name = "regenerate", alias = "regen")] | ||
Regenerate(regenerate::Options), | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(about = "Manage webhooks")] | ||
#[group(skip)] | ||
pub struct Options { | ||
#[clap(subcommand)] | ||
pub commands: Commands, | ||
} | ||
|
||
pub async fn handle(options: Options, state: State) -> Result<()> { | ||
match options.commands { | ||
Commands::List(options) => list::handle(options, state).await, | ||
Commands::Create(options) => create::handle(options, state).await, | ||
Commands::Update(options) => update::handle(options, state).await, | ||
Commands::Delete(options) => delete::handle(options, state).await, | ||
Commands::Regenerate(options) => regenerate::handle(options, state).await, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use anyhow::{Context, Result}; | ||
use clap::Parser; | ||
|
||
use crate::commands::webhooks::utils::format_webhooks; | ||
use crate::state::State; | ||
use crate::utils::urlify; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(about = "Regenerate a webhook secret")] | ||
#[group(skip)] | ||
pub struct Options { | ||
#[clap(short, long, help = "The id of the webhook")] | ||
pub id: Option<String>, | ||
} | ||
|
||
pub async fn handle(options: Options, state: State) -> Result<()> { | ||
let project = state.ctx.current_project_error()?; | ||
|
||
let all = state.hop.webhooks.get_all(&project.id).await?; | ||
|
||
let webhook = if let Some(id) = options.id { | ||
all.into_iter() | ||
.find(|webhook| webhook.id == id) | ||
.context("Webhook not found")? | ||
} else { | ||
let formatted_webhooks = format_webhooks(&all, false); | ||
|
||
let idx = dialoguer::Select::new() | ||
.with_prompt("Select a webhook to update") | ||
.items(&formatted_webhooks) | ||
.default(0) | ||
.interact()?; | ||
|
||
all[idx].clone() | ||
}; | ||
|
||
let token = state | ||
.hop | ||
.webhooks | ||
.regenerate_secret(&project.id, &webhook.id) | ||
.await?; | ||
|
||
log::info!("This is your webhook's secret, this is how you will authenticate traffic coming to your endpoint"); | ||
log::info!("Webhook Header: {}", urlify("X-Hop-Hooks-Signature")); | ||
log::info!("Webhook Secret: {}", urlify(&token)); | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.