-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement notion shim
command
#100
Conversation
notion shim
commandnotion shim
command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just one request to refactor a deep function.
src/command/shim.rs
Outdated
} | ||
} | ||
|
||
fn list(session: &mut Session, verbose: bool) -> Fallible<bool> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is hard to read. Can you break out some of the nested loops into helper functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some more feedback... hang in there, this is good progress and a good opportunity to pick up some good Rust style. We're getting there!
src/command/shim.rs
Outdated
@@ -100,38 +108,41 @@ Options: | |||
} | |||
} | |||
|
|||
fn list(session: &mut Session, verbose: bool) -> Fallible<bool> { | |||
fn list(session: &Session, verbose: bool) -> Fallible<bool> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an improvement! But there are still a few things we can do to make it simpler. First a few general points:
- You should generally avoid inspecting error cases, and just propagate them.
- You should generally prefer
let foo = bar()?;
tobar().map(|foo|, { ... })
. This avoids rightward drift, avoids confusing iterationmap
versus resultmap
, and lets you code closer to a style that's like exceptions. As an analogy in JS, this is whylet foo = await bar();
is nicer thanbar.then((foo) => { ... })
. - It's easiest to just convert foreign error types like
io::Error
into notion's error types as early as possible, and the?
syntax makes that lightweight. So instead of returningResult<Foo, io::Result>
from helper functions, just returnFallible<Foo>
.
As for the Command
trait, don't throw away errors and return Ok(false)
-- you want to let the particular error propagate out.
Here's how we could write this function to be simpler and more readable/maintainable:
fn list(session: &Session, verbose: bool) -> Fallible<()> {
let shim_dir = path:shim_dir()?;
let files = fs::read_dir(shim_dir).unknown()?;
for file in files {
print_file_info(f, session, verbose)?;
}
Ok(())
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is so much simpler!
I made peace with the borrow checker, but lately I have been fighting Result and Option. This helps a lot!
src/command/shim.rs
Outdated
session: &Session, | ||
verbose: bool, | ||
) -> Result<bool, io::Error> { | ||
file.path().file_name().map_or(Ok(false), |shim_name| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A DirEntry
always has a file name, so instead of using the Option
that comes from file.path().file_name()
you can use file.file_name()
which directly returns an OsString
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh! I missed that in the docs somehow
src/command/shim.rs
Outdated
match resolve_shim(session, &shim_name) { | ||
Ok(shim_info) => println!("{} -> {}", shim_name.to_string_lossy(), shim_info), | ||
Err(err) => { | ||
style::display_error(style::ErrorContext::Notion, &err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to be doing this error handling logic here, as opposed to letting it propagate out and be handled at the top level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/command/shim.rs
Outdated
}) | ||
} | ||
|
||
fn print_file_info( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a simplified version of this function:
fn print_file_info(file: fs::DirEntry, session: &Session, verbose: bool) -> Fallible<()> {
let shim_name = file.file_name();
if verbose {
let shim_info = resolve_shim(session, &shim_name)?;
println!("{} -> {}", shim_name.to_string_lossy(), shim_info);
} else {
println!("{}", shim_name.to_string_lossy());
}
Ok(())
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So much better!
src/command/shim.rs
Outdated
@@ -152,13 +163,18 @@ fn resolve_shim(session: &mut Session, shim_name: &OsStr) -> Fallible<ShimKind> | |||
} | |||
} | |||
|
|||
fn node_is_available(project: &Project, session: &Session) -> Fallible<Option<Version>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this isn't a boolean predicate, maybe a better name would be something like available_node_version
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree that's a better name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great.
Implements
notion shim
List shims
Running
notion shim
lists the shims that exist in~/.notion/bin
:Running
notion shim --verbose
lists those shims and where they will resolve to:Create shims
Running
notion shim <name>
creates a 3rd-party executable shim in the notion shim dir (~/.notion/bin/
on unix), pointing to.notion/launchbin
:Delete shims
Running
notion shim <name> --delete
deletes a 3rd-party executable shim:Closes #96.