-
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
10 changed files
with
389 additions
and
401 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +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(()) | ||
} | ||
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 |
---|---|---|
@@ -1,39 +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(()) | ||
} | ||
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 |
---|---|---|
@@ -1,35 +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(()) | ||
} | ||
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(()) | ||
} |
Oops, something went wrong.