-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add top-level events command with list subcommand (#1307)
Co-authored-by: Kamil Ogórek <kamil.ogorek@gmail.com>
- Loading branch information
Showing
13 changed files
with
374 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use anyhow::Result; | ||
use clap::{Arg, ArgMatches, Command}; | ||
|
||
use crate::api::Api; | ||
use crate::config::Config; | ||
use crate::utils::formatting::Table; | ||
|
||
pub fn make_command(command: Command) -> Command { | ||
command | ||
.about("List all events in your organization.") | ||
.arg( | ||
Arg::with_name("show_user") | ||
.long("show-user") | ||
.short('U') | ||
.help("Display the Users column."), | ||
) | ||
.arg( | ||
Arg::with_name("show_tags") | ||
.long("show-tags") | ||
.short('T') | ||
.help("Display the Tags column."), | ||
) | ||
.arg( | ||
Arg::new("max_rows") | ||
.long("max-rows") | ||
.value_name("MAX_ROWS") | ||
.value_parser(clap::value_parser!(usize)) | ||
.help("Maximum number of rows to print."), | ||
) | ||
.arg( | ||
Arg::new("pages") | ||
.long("pages") | ||
.value_name("PAGES") | ||
.default_value("5") | ||
.value_parser(clap::value_parser!(usize)) | ||
.help("Maximum number of pages to fetch (100 events/page)."), | ||
) | ||
} | ||
|
||
pub fn execute(matches: &ArgMatches) -> Result<()> { | ||
let config = Config::current(); | ||
let org = config.get_org(matches)?; | ||
let project = config.get_project(matches)?; | ||
let pages = *matches.get_one("pages").unwrap(); | ||
let api = Api::current(); | ||
|
||
let events = api.list_organization_project_events(&org, &project, pages)?; | ||
|
||
let mut table = Table::new(); | ||
let title_row = table.title_row().add("Event ID").add("Date").add("Title"); | ||
|
||
if matches.is_present("show_user") { | ||
title_row.add("User"); | ||
} | ||
|
||
if matches.is_present("show_tags") { | ||
title_row.add("Tags"); | ||
} | ||
|
||
let max_rows = std::cmp::min( | ||
events.len(), | ||
*matches.get_one("max_rows").unwrap_or(&std::usize::MAX), | ||
); | ||
|
||
if let Some(events) = events.get(..max_rows) { | ||
for event in events { | ||
let row = table.add_row(); | ||
row.add(&event.event_id) | ||
.add(&event.date_created) | ||
.add(&event.title); | ||
|
||
if matches.is_present("show_user") { | ||
if let Some(user) = &event.user { | ||
row.add(user); | ||
} else { | ||
row.add("-"); | ||
} | ||
} | ||
|
||
if matches.is_present("show_tags") { | ||
if let Some(tags) = &event.tags { | ||
row.add( | ||
tags.iter() | ||
.map(|t| t.to_string()) | ||
.collect::<Vec<_>>() | ||
.join("\n"), | ||
); | ||
} else { | ||
row.add("-"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if table.is_empty() { | ||
println!("No events found"); | ||
} else { | ||
table.print(); | ||
} | ||
|
||
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,45 @@ | ||
use anyhow::Result; | ||
use clap::{ArgMatches, Command}; | ||
|
||
use crate::utils::args::ArgExt; | ||
|
||
pub mod list; | ||
|
||
macro_rules! each_subcommand { | ||
($mac:ident) => { | ||
$mac!(list); | ||
}; | ||
} | ||
|
||
pub fn make_command(mut command: Command) -> Command { | ||
macro_rules! add_subcommand { | ||
($name:ident) => {{ | ||
command = command.subcommand(crate::commands::events::$name::make_command( | ||
Command::new(stringify!($name).replace('_', "-")), | ||
)); | ||
}}; | ||
} | ||
|
||
command = command | ||
.about("Manage events on Sentry.") | ||
.subcommand_required(true) | ||
.arg_required_else_help(true) | ||
.org_arg() | ||
.project_arg(true); | ||
each_subcommand!(add_subcommand); | ||
command | ||
} | ||
|
||
pub fn execute(matches: &ArgMatches) -> Result<()> { | ||
macro_rules! execute_subcommand { | ||
($name:ident) => {{ | ||
if let Some(sub_matches) = | ||
matches.subcommand_matches(&stringify!($name).replace('_', "-")) | ||
{ | ||
return crate::commands::events::$name::execute(&sub_matches); | ||
} | ||
}}; | ||
} | ||
each_subcommand!(execute_subcommand); | ||
unreachable!(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
``` | ||
$ sentry-cli events --help | ||
? success | ||
sentry-cli[EXE]-events | ||
Manage events on Sentry. | ||
|
||
USAGE: | ||
sentry-cli[EXE] events [OPTIONS] <SUBCOMMAND> | ||
|
||
OPTIONS: | ||
--auth-token <AUTH_TOKEN> Use the given Sentry auth token. | ||
-h, --help Print help information | ||
--header <KEY:VALUE> Custom headers that should be attached to all requests | ||
in key:value format. | ||
--log-level <LOG_LEVEL> Set the log output verbosity. [possible values: trace, debug, | ||
info, warn, error] | ||
-o, --org <ORG> The organization slug | ||
-p, --project <PROJECT> The project slug. | ||
--quiet Do not print any output while preserving correct exit code. | ||
This flag is currently implemented only for selected | ||
subcommands. [aliases: silent] | ||
|
||
SUBCOMMANDS: | ||
help Print this message or the help of the given subcommand(s) | ||
list List all events in your organization. | ||
|
||
``` |
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,6 @@ | ||
``` | ||
$ sentry-cli events list | ||
? success | ||
No events found | ||
|
||
``` |
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,28 @@ | ||
``` | ||
$ sentry-cli events list --help | ||
? success | ||
sentry-cli[EXE]-events-list | ||
List all events in your organization. | ||
|
||
USAGE: | ||
sentry-cli[EXE] events list [OPTIONS] | ||
|
||
OPTIONS: | ||
--auth-token <AUTH_TOKEN> Use the given Sentry auth token. | ||
-h, --help Print help information | ||
--header <KEY:VALUE> Custom headers that should be attached to all requests | ||
in key:value format. | ||
--log-level <LOG_LEVEL> Set the log output verbosity. [possible values: trace, debug, | ||
info, warn, error] | ||
--max-rows <MAX_ROWS> Maximum number of rows to print. | ||
-o, --org <ORG> The organization slug | ||
-p, --project <PROJECT> The project slug. | ||
--pages <PAGES> Maximum number of pages to fetch (100 events/page). [default: | ||
5] | ||
--quiet Do not print any output while preserving correct exit code. | ||
This flag is currently implemented only for selected | ||
subcommands. [aliases: silent] | ||
-T, --show-tags Display the Tags column. | ||
-U, --show-user Display the Users column. | ||
|
||
``` |
27 changes: 27 additions & 0 deletions
27
tests/integration/_cases/events/events-no-subcommand.trycmd
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,27 @@ | ||
``` | ||
$ sentry-cli events | ||
? failed | ||
sentry-cli[EXE]-events | ||
Manage events on Sentry. | ||
|
||
USAGE: | ||
sentry-cli[EXE] events [OPTIONS] <SUBCOMMAND> | ||
|
||
OPTIONS: | ||
--auth-token <AUTH_TOKEN> Use the given Sentry auth token. | ||
-h, --help Print help information | ||
--header <KEY:VALUE> Custom headers that should be attached to all requests | ||
in key:value format. | ||
--log-level <LOG_LEVEL> Set the log output verbosity. [possible values: trace, debug, | ||
info, warn, error] | ||
-o, --org <ORG> The organization slug | ||
-p, --project <PROJECT> The project slug. | ||
--quiet Do not print any output while preserving correct exit code. | ||
This flag is currently implemented only for selected | ||
subcommands. [aliases: silent] | ||
|
||
SUBCOMMANDS: | ||
help Print this message or the help of the given subcommand(s) | ||
list List all events in your organization. | ||
|
||
``` |
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
Oops, something went wrong.