Repository: casey/just (Rust)

Issue:
Make `--show` work with aliases
Given the justfile:

```
alias b = build

build:
  echo 'building!'
```

The following should work:

```
$ just --show b
```


Discussion:
I've been `cat`ing my justfiles to see aliases when I forget them. I think it would be ideal if `--show` worked like this..

Given: 
```
alias b = build
build: 
    echo 'Building!'
```
```
$just --show build
build: 
    echo 'Building!'
---------------
Aliases for build: b
```
```
$just --show b
b is an alias for build
build: 
    echo 'Building!'
```
We need a better separator than a bunch of hyphens but this is close to what an average user would want to see when they use `--show`. What do you think?

If someone else isn't working on this, I can give it a shot next week/weekend. 
It's nice when the output of a command is also valid input. In the case of `--show` it would allow moving recipes to a new justfile. For example:

```
$ just --show build >> new-project/Justfile
```

So I think for `--show` it would be good to use the syntax of the original file:

```
$ just --show b
alias b := build
build: 
    echo 'Building!'
```

It seems natural to include the `alias b := build` line if a user does `--show b`, since they specifically asked about it, and displaying the `build` recipe on its own wouldn't make much sense.

I'm not entirely sure if `--show build` should also show aliases:

```
$ just --show build
alias b := build
build: 
    echo 'Building!'
```

Or not:

```
$ just --show build
build: 
    echo 'Building!'
```

What do y'all think? I'm don't feel super strongly about it, so I'm easily swayed.
I think displaying the alias is appropriate. If `--show` displays details about a given recipe then it should also show any meta-details such as aliases as well, in my mind.  😁 



Patch:
diff --git a/src/justfile.rs b/src/justfile.rs
index 8dddad741a..288a9c6c14 100644
--- a/src/justfile.rs
+++ b/src/justfile.rs
@@ -91,16 +91,7 @@ impl<'a> Justfile<'a> where {
     let mut rest = arguments;
 
     while let Some((argument, mut tail)) = rest.split_first() {
-      let get_recipe = |name| {
-        if let Some(recipe) = self.recipes.get(name) {
-          Some(recipe)
-        } else if let Some(alias) = self.aliases.get(name) {
-          self.recipes.get(alias.target)
-        } else {
-          None
-        }
-      };
-      if let Some(recipe) = get_recipe(argument) {
+      if let Some(recipe) = self.get_recipe(argument) {
         if recipe.parameters.is_empty() {
           grouped.push((recipe, &tail[0..0]));
         } else {
@@ -150,6 +141,16 @@ impl<'a> Justfile<'a> where {
     Ok(())
   }
 
+  pub fn get_recipe(&self, name: &str) -> Option<&Recipe<'a>> {
+    if let Some(recipe) = self.recipes.get(name) {
+      Some(recipe)
+    } else if let Some(alias) = self.aliases.get(name) {
+      self.recipes.get(alias.target)
+    } else {
+      None
+    }
+  }
+
   fn run_recipe<'b>(
     &self,
     context: &'b RecipeContext<'a>,
diff --git a/src/run.rs b/src/run.rs
index 14e899c238..35d5d9e3b0 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -449,7 +449,7 @@ pub fn run() {
   }
 
   if let Some(name) = matches.value_of("SHOW") {
-    match justfile.recipes.get(name) {
+    match justfile.get_recipe(name) {
       Some(recipe) => {
         println!("{}", recipe);
         process::exit(EXIT_SUCCESS);


Test patch:
diff --git a/tests/integration.rs b/tests/integration.rs
index 1062cb0b1f..0eac478e59 100644
--- a/tests/integration.rs
+++ b/tests/integration.rs
@@ -272,6 +272,32 @@ integration_test! {
   status: EXIT_FAILURE,
 }
 
+integration_test! {
+  name: alias_show,
+  justfile: "foo:\n    bar\nalias f := foo",
+  args: ("--show", "f"),
+  stdin:  "",
+  stdout: "foo:
+    bar
+",
+  stderr: "",
+  status: EXIT_SUCCESS,
+}
+
+integration_test! {
+  name: alias_show_missing_target,
+  justfile: "alias f := foo",
+  args: ("--show", "f"),
+  stdin:  "",
+  stdout: "",
+  stderr: "error: Alias `f` has an unknown target `foo`
+  |
+1 | alias f := foo
+  |       ^
+",
+  status: EXIT_FAILURE,
+}
+
 integration_test! {
   name:     default,
   justfile: "default:\n echo hello\nother: \n echo bar",