Skip to content

Commit

Permalink
Add view subcommand to view a change in a browser (#30)
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
9999years committed Apr 4, 2024
1 parent 4545511 commit 9f4a55d
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
189 changes: 189 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tracing = { version = "0.1.40", features = ["attributes"] }
tracing-human-layer = "0.1.3"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "registry"] }
utf8-command = "1.0.1"
webbrowser = "0.8.13"

[dev-dependencies]
indoc = "2.0.5"
Expand Down
6 changes: 6 additions & 0 deletions src/change_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use std::ops::Deref;
#[serde(transparent)]
pub struct ChangeId(pub String);

impl From<ChangeId> for String {
fn from(value: ChangeId) -> Self {
value.0
}
}

impl Display for ChangeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Expand Down
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ pub enum Command {
/// Defaults to the `HEAD` commit's change.
query: Option<String>,
},
/// Open a change in a web browser.
View {
/// The change to view.
///
/// Defaults to the `HEAD` commit's change.
query: Option<String>,
},
}

#[derive(Debug, Clone, Subcommand)]
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ fn main() -> miette::Result<()> {
let chain = gerrit.format_chain(query)?;
let _ = stdoutln!("{chain}");
}
cli::Command::View { query } => {
let git = Git::new();
let gerrit = git.gerrit(None)?;
let query = match query {
Some(query) => query,
None => git.change_id("HEAD")?.into(),
};
let change = gerrit.get_change(query)?;
let url = &change.url;
webbrowser::open(url)
.into_diagnostic()
.wrap_err_with(|| format!("Failed to open browser for {url}"))?;
}
}

Ok(())
Expand Down

0 comments on commit 9f4a55d

Please sign in to comment.