Skip to content

Commit f8b07e4

Browse files
authored
Merge pull request #1 from calvinrp/multi-namespace-fixes
fixes
2 parents 8134a10 + aa9d364 commit f8b07e4

File tree

5 files changed

+46
-87
lines changed

5 files changed

+46
-87
lines changed

crates/wac-resolver/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,8 @@ impl From<serde_json::Error> for CommandError {
199199
impl From<WargClientError> for CommandError {
200200
fn from(value: WargClientError) -> Self {
201201
match &value.0 {
202-
ClientError::PackageDoesNotExistWithHint { .. } => {
203-
CommandError::WargHint(value.0.into())
204-
}
205-
_ => CommandError::WargClient(value.0.into()),
202+
ClientError::PackageDoesNotExistWithHint { .. } => CommandError::WargHint(value.0),
203+
_ => CommandError::WargClient(value.0),
206204
}
207205
}
208206
}

crates/wac-resolver/src/registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl RegistryPackageResolver {
172172
self.client
173173
.get_warg_registry(id.namespace())
174174
.await
175-
.map_err(|e| CommandError::WargClient(e))?
175+
.map_err(CommandError::WargClient)?
176176
.as_ref(),
177177
&id,
178178
)
@@ -287,7 +287,7 @@ impl RegistryPackageResolver {
287287
self.client
288288
.get_warg_registry(id.namespace())
289289
.await
290-
.map_err(|e| CommandError::WargClient(e.into()))?
290+
.map_err(CommandError::WargClient)?
291291
.as_ref(),
292292
&id,
293293
)

src/bin/wac.rs

Lines changed: 40 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -30,99 +30,61 @@ async fn main() -> Result<()> {
3030
pretty_env_logger::init();
3131

3232
with_interactive_retry(|retry: Option<Retry>| async {
33-
if let Err(e) = match Wac::parse() {
33+
if let Err(err) = match Wac::parse() {
3434
Wac::Parse(cmd) => cmd.exec().await,
3535
Wac::Resolve(cmd) => cmd.exec(retry).await,
3636
Wac::Encode(cmd) => cmd.exec(retry).await,
3737
} {
38-
match e {
39-
CommandError::General(e) => {
40-
eprintln!(
41-
"{error}: {e:?}",
42-
error = "error".if_supports_color(Stream::Stderr, |text| {
43-
text.style(Style::new().red().bold())
44-
})
38+
if let CommandError::WargHint(ClientError::PackageDoesNotExistWithHint { name, hint }) = &err {
39+
if let Some((namespace, registry)) = hint.to_str().unwrap().split_once('=') {
40+
let prompt = format!(
41+
"The package `{}`, does not exist in the registry you're using.
42+
However, the package namespace `{namespace}` does exist in the registry at {registry}.
43+
Would you like to configure your warg cli to use this registry for packages with this namespace in the future? y/N\n",
44+
name.name(),
4545
);
46-
std::process::exit(1);
47-
}
48-
CommandError::Serde(e) => {
49-
eprintln!(
50-
"{error}: {e:?}",
51-
error = "error".if_supports_color(Stream::Stderr, |text| {
52-
text.style(Style::new().red().bold())
53-
})
54-
);
55-
std::process::exit(1);
56-
}
57-
CommandError::Wac(e) => {
58-
eprintln!(
59-
"{error}: {e:?}",
60-
error = "error".if_supports_color(Stream::Stderr, |text| {
61-
text.style(Style::new().red().bold())
62-
})
63-
);
64-
std::process::exit(1);
65-
}
66-
CommandError::WacResolution(e) => {
67-
eprintln!(
46+
if Confirm::with_theme(&ColorfulTheme::default())
47+
.with_prompt(prompt)
48+
.interact()
49+
.unwrap()
50+
{
51+
if let Err(e) = match Wac::parse() {
52+
Wac::Parse(cmd) => cmd.exec().await,
53+
Wac::Resolve(cmd) => {
54+
cmd.exec(Some(Retry::new(
55+
namespace.to_string(),
56+
registry.to_string(),
57+
)))
58+
.await
59+
},
60+
Wac::Encode(cmd) => {
61+
cmd.exec(Some(Retry::new(
62+
namespace.to_string(),
63+
registry.to_string(),
64+
)))
65+
.await
66+
},
67+
}
68+
{
69+
eprintln!(
6870
"{error}: {e:?}",
6971
error = "error".if_supports_color(Stream::Stderr, |text| {
7072
text.style(Style::new().red().bold())
7173
})
7274
);
7375
std::process::exit(1);
74-
},
75-
CommandError::WargClient(e) => {
76-
eprintln!(
77-
"{error}: {e:?}",
78-
error = "error".if_supports_color(Stream::Stderr, |text| {
79-
text.style(Style::new().red().bold())
80-
})
81-
);
82-
std::process::exit(1);
83-
}
84-
CommandError::WargHint(e) => {
85-
if let ClientError::PackageDoesNotExistWithHint { name, hint } = e {
86-
let hint_reg = hint.to_str().unwrap();
87-
let mut terms = hint_reg.split('=');
88-
let namespace = terms.next();
89-
let registry = terms.next();
90-
if let (Some(namespace), Some(registry)) = (namespace, registry) {
91-
let prompt = format!(
92-
"The package `{}`, does not exist in the registry you're using.\nHowever, the package namespace `{namespace}` does exist in the registry at {registry}.\nWould you like to configure your warg cli to use this registry for packages with this namespace in the future? y/N\n",
93-
name.name()
94-
);
95-
if Confirm::with_theme(&ColorfulTheme::default())
96-
.with_prompt(prompt)
97-
.interact()
98-
.unwrap()
99-
{
100-
if let Err(e) = match Wac::parse() {
101-
Wac::Parse(cmd) => cmd.exec().await,
102-
Wac::Resolve(cmd) => cmd.exec(Some(Retry::new(
103-
namespace.to_string(),
104-
registry.to_string(),
105-
)))
106-
.await,
107-
Wac::Encode(cmd) => cmd.exec(Some(Retry::new(
108-
namespace.to_string(),
109-
registry.to_string(),
110-
)))
111-
.await
112-
} {
113-
eprintln!(
114-
"{error}: {e:?}",
115-
error = "error".if_supports_color(Stream::Stderr, |text| {
116-
text.style(Style::new().red().bold())
117-
})
118-
);
119-
std::process::exit(1);
12076
}
12177
}
122-
}
123-
}
12478
}
12579
}
80+
81+
eprintln!(
82+
"{error}: {err:?}",
83+
error = "error".if_supports_color(Stream::Stderr, |text| {
84+
text.style(Style::new().red().bold())
85+
})
86+
);
87+
std::process::exit(1);
12688
}
12789
Ok(())
12890
})

src/commands/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl EncodeCommand {
107107
validate: !self.no_validate,
108108
..Default::default()
109109
})
110-
.map_err(|e| CommandError::Wac(e))?;
110+
.map_err(CommandError::Wac)?;
111111
if self.wat {
112112
bytes = print_bytes(&bytes)
113113
.context("failed to convert binary wasm output to text")?

src/commands/parse.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ impl ParseCommand {
2424

2525
let document = Document::parse(&contents).map_err(|e| fmt_err(e, &self.path, &contents))?;
2626

27-
serde_json::to_writer_pretty(std::io::stdout(), &document)
28-
.map_err(|e| CommandError::Serde(e))?;
27+
serde_json::to_writer_pretty(std::io::stdout(), &document).map_err(CommandError::Serde)?;
2928
println!();
3029

3130
Ok(())

0 commit comments

Comments
 (0)