Skip to content

Commit 297b17b

Browse files
committed
feat: add tracing
1 parent c3788ce commit 297b17b

File tree

4 files changed

+220
-26
lines changed

4 files changed

+220
-26
lines changed

Cargo.lock

Lines changed: 199 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "magic-opener"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
authors = ["Dan Sully"]
55
edition = "2024"
66
description = "An 'open' replacement that tries to do the right thing."
@@ -16,12 +16,14 @@ name = "open"
1616
path = "src/main.rs"
1717

1818
[dependencies]
19-
clap = { version = "^4.5", default-features = false, features = [
19+
clap = { version = "4.5", default-features = false, features = [
2020
"help",
2121
"std",
2222
"usage",
2323
] }
2424
thiserror = "2"
25+
tracing = "0.1.41"
26+
tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
2527

2628
[dev-dependencies]
2729
testresult = "0"

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ mod parser;
1010
mod repo;
1111

1212
use repo::{GitRepository, RepositoryError};
13+
use tracing::debug;
14+
use tracing_subscriber::prelude::*;
15+
use tracing_subscriber::{EnvFilter, fmt};
1316

1417
const LOCALHOST: &str = "localhost";
1518
const OPEN: &str = "/usr/bin/open";
@@ -27,6 +30,8 @@ fn expand_tilde(path: &str) -> String {
2730
}
2831

2932
fn main() {
33+
tracing_subscriber::registry().with(fmt::layer()).with(EnvFilter::from_default_env()).init();
34+
3035
let cli = ClapCommand::new(env!("CARGO_PKG_NAME"))
3136
.version(env!("CARGO_PKG_VERSION"))
3237
.author(env!("CARGO_PKG_AUTHORS"))
@@ -65,6 +70,8 @@ fn main() {
6570
}
6671
};
6772

73+
debug!("Remote path resolved to: {}", remote_path);
74+
6875
if remote_path.starts_with('-') {
6976
let command = if remote_path == "--help" { vec!["-h".to_string()] } else { paths };
7077

@@ -113,5 +120,7 @@ fn main() {
113120
args.insert(0, "--background");
114121
}
115122

123+
debug!("Opening with args: {:?}", args);
124+
116125
let _ = Command::new(OPEN).args(&args).exec();
117126
}

src/repo.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::path::{Path, PathBuf};
22
use std::process::{Command, ExitStatus, Stdio};
3-
use std::{fmt, str};
3+
use std::str;
44

55
use thiserror::Error;
6+
use tracing::debug;
67

78
use crate::parser::parse_git_url;
89

@@ -146,25 +147,31 @@ impl GitRepository {
146147
pub fn url(current_dir: &str, paths: &[String]) -> Result<String, RepositoryError> {
147148
let is_git = is_git_repo(current_dir);
148149

150+
debug!("is_git_repo({current_dir}) = {is_git}");
151+
149152
let join_paths = || match paths.join(" ") {
150153
path if path == "." => current_dir.to_string(),
151154
path => path,
152155
};
153156

154157
if !is_git {
158+
debug!("Not a Git repository; returning joined paths: {}", join_paths());
155159
return Ok(join_paths());
156160
}
157161

158162
let r = Self::from_path(current_dir)?;
159163

160164
if paths.is_empty() {
165+
debug!("No additional paths; returning repository URL: {}", r.http_url());
161166
return Ok(r.http_url());
162167
}
163168

164169
if paths.len() == 1 {
165170
let arg = &paths[0];
166171
let is_commit = is_valid_commit_hash(arg);
167172

173+
debug!("is_commit_hash({arg}) = {is_commit}");
174+
168175
if is_commit || is_pr_number(arg) {
169176
return if is_commit {
170177
match r.pr_for_commit(arg) {
@@ -181,18 +188,6 @@ impl GitRepository {
181188
}
182189
}
183190

184-
impl fmt::Debug for GitRepository {
185-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186-
write!(f, "{}", self.http_url())
187-
}
188-
}
189-
190-
impl fmt::Display for GitRepository {
191-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192-
f.write_str(&self.http_url())
193-
}
194-
}
195-
196191
fn git(path: impl AsRef<Path>, args: &[&str]) -> Result<String, RepositoryError> {
197192
let out = Command::new("git")
198193
.args(args)

0 commit comments

Comments
 (0)