Skip to content

Commit

Permalink
Add just_executable() function
Browse files Browse the repository at this point in the history
  • Loading branch information
bew committed Mar 27, 2021
1 parent 6f42c8b commit 2581c64
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,20 @@ $ just system-info
This is an x86_64 machine
```
- `just_excutable()` - Path to the running just executable.
For example:
```make
itself:
@echo The executable is at: {{just_excutable()}}
```
```
$ just itself
The executable is at: /bin/just
```
==== Environment Variables
- `env_var(key)` – Retrieves the environment variable with name `key`, aborting if it is not present.
Expand Down
16 changes: 16 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lazy_static! {
("invocation_directory", Nullary(invocation_directory)),
("env_var", Unary(env_var)),
("env_var_or_default", Binary(env_var_or_default)),
("just_executable", Nullary(just_executable)),
]
.into_iter()
.collect();
Expand Down Expand Up @@ -123,3 +124,18 @@ fn env_var_or_default(
Ok(value) => Ok(value),
}
}

fn just_executable(_context: &FunctionContext) -> Result<String, String> {
let exe_path = std::env::current_exe()
.map_err(|e| format!("Error getting just executable: {}", e))?;

exe_path
.to_str()
.map(str::to_owned)
.ok_or_else(|| {
format!(
"Just executable path is not valid unicode: {}",
exe_path.to_string_lossy()
)
})
}

0 comments on commit 2581c64

Please sign in to comment.