Skip to content

Commit

Permalink
Merge pull request #44 from DanCardin/dc/default-args
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin authored Oct 6, 2022
2 parents 1da049d + ce4e01b commit c1e3f1d
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 36 deletions.
22 changes: 21 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# Changelog

### [v0.7.1](https://github.com/DanCardin/sauce/compare/v0.7.0...v0.7.1) (2022-09-28)
## [v0.8.0](https://github.com/DanCardin/sauce/compare/v0.7.2...v0.8.0) (2022-10-06)

### Features

* Add autoload-args config option.
([052778b](https://github.com/DanCardin/sauce/commit/052778b8c6d8fc559f8b3a29378cc52f461ca211))
* Add the ability to configure default arguments supplied to the sauce command.
([49a617d](https://github.com/DanCardin/sauce/commit/49a617d8f386b2ac877c1a796b69b377b9629b1c))
* Add -t/--target option to only sauce a specific target.
([89ef948](https://github.com/DanCardin/sauce/commit/89ef948998b662d8cef663d17d350e85080dead9))
* Add file target.
([b539b5d](https://github.com/DanCardin/sauce/commit/b539b5d2c08ea1dfef7c48b25708a95331ba35b1))

### [v0.7.2](https://github.com/DanCardin/sauce/compare/v0.7.1...v0.7.2) (2022-10-04)

#### Fixes

* Inverted error handling logic for file creation.
([4fdee76](https://github.com/DanCardin/sauce/commit/4fdee76cadbff7b3933c575cb4120b847fc0afeb))

### [v0.7.1](https://github.com/DanCardin/sauce/compare/v0.7.0...v0.7.1) (2022-09-29)

#### Fixes

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sauce"
version = "0.7.2"
version = "0.8.0"
authors = ["Dan Cardin <ddcardin@gmail.com>"]
edition = "2021"
description = "A tool for managing directory-specific state."
Expand Down
13 changes: 10 additions & 3 deletions doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ startup and changing directory.

Note this does not actually enable the autoload feature!

*Handy Tip!* I set this to `true` at the **global** level, because
_Handy Tip!_ I set this to `true` at the **global** level, because
that’s what will always end up loaded at every shell start.

### `autoload`
Expand All @@ -30,10 +30,17 @@ Defaults to `false`. When `true` **actually** enables autoload behavior
“Autoload behavior” causes `sauce` to be invoked upon both new shells as
well as when changing directory.

*Handy Tip!* I set this to `true` at the **local** level, enabling me to
_Handy Tip!_ I set this to `true` at the **local** level, enabling me to
opt in to autoload in whatever directories I like, which I find I more
frequently prefer.

### `autoload-args`

Defaults to `""`. By default, autoloaded invocations are effectively an
argument-less call to `sauce`. When set, this causes the `sauce shell init`
command to emit the `autoload-args`'s value into the shell wrapper around
sauce such that autoloaded invocations also include those arguments.

### `clear-ignore`

Defaults to `[]`. When set, values should be `string`s and their values
Expand All @@ -46,7 +53,7 @@ When a list item matches a given target’s key (i.e. env var name, alias
name, function name) it will then be **excluded** from from the set of
values returned for whatever subcommand you’re invoking.

*Handy Tip!* I sometimes use this to set values like `$PATH` in
_Handy Tip!_ I sometimes use this to set values like `$PATH` in
directories that i’m sure I’ll only `sauce` once, while not potentially
breaking my shell by unsetting `$PATH`.

Expand Down
11 changes: 8 additions & 3 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ Any key-value pair can be tagged with, you might call “namespaces”.

Consider an env var definition

``` toml
```toml
AWS_PROFILE = {default = "projectname-dev", uat = "projectname-uat", prod = "projectname-prod"}
```

Given `sauce`, you will get the “default” namespace
(i.e. AWS\_PROFILE=projectname-dev) for this value, as well as all other
(i.e. AWS_PROFILE=projectname-dev) for this value, as well as all other
unnamespaced values.

Given `sauce --as prod`, you will get the “prod” namespace
(i.e. AWS\_PROFILE=projectname-prod) for this value, as well as all
(i.e. AWS_PROFILE=projectname-prod) for this value, as well as all
other unnamespaced values.

## `sauce --glob glob` and `sauce --filter filter`
Expand All @@ -39,6 +39,11 @@ which might be important given that there can be overlap between
targets. Targets are separated from their search term by `:`,
i.e. `--glob env:database*,function:work-*`.

## `sauce --target target` / `sauce -t target`

This can be thought of as a simpler way of performing `--glob target:*`,
by automatically only performing the sauce command for the given target.

## `sauce --show`

This option is essentially a “dry run” option. vanilla `sauce`
Expand Down
19 changes: 18 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use toml_edit::{Document, Item, Table, Value};
pub struct RealizedSettings {
pub autoload_hook: bool,
pub autoload: bool,
pub autoload_args: String,
pub clear_ignore: Vec<String>,
}

Expand All @@ -22,6 +23,7 @@ pub struct Settings {
pub file: PathBuf,
pub autoload_hook: Option<bool>,
pub autoload: Option<bool>,
pub autoload_args: Option<String>,
pub clear_ignore: Option<Vec<String>>,
}

Expand All @@ -37,12 +39,14 @@ impl Settings {

let autoload_hook = Setting::new(general, "autoload-hook").as_bool();
let autoload = Setting::new(general, "autoload").as_bool();
let autoload_args = Setting::new(general, "autoload-args").as_string();
let clear_ignore = Setting::new(general, "clear-ignore").as_vec_of_string();

Self {
file,
autoload_hook,
autoload,
autoload_args,
clear_ignore,
}
}
Expand All @@ -59,6 +63,9 @@ impl Settings {
if let Some(v) = settings.autoload {
default.autoload = v;
}
if let Some(v) = &settings.autoload_args {
default.autoload_args = v.to_string();
}
if let Some(v) = &settings.clear_ignore {
default.clear_ignore = v.to_vec();
}
Expand All @@ -76,7 +83,7 @@ impl Settings {
let values = pairs
.iter()
.filter_map(|(setting, value)| match setting.as_ref() {
"autoload" | "autoload-hook" | "clear-ignore" => {
"autoload" | "autoload-hook" | "autoload-args" | "clear-ignore" => {
if let Ok(parsed_value) = value.as_ref().parse::<Value>() {
Some((setting.as_ref(), toml_edit::value(parsed_value)))
} else {
Expand Down Expand Up @@ -132,6 +139,15 @@ impl<'a> Setting<'a> {
}
}

pub fn as_string(&self) -> Option<String> {
if let Some(value) = self.get_value() {
let value = value.as_str().map(|s| s.to_string());
self.notify_invalid("string", value)
} else {
None
}
}

pub fn as_vec_of_string(&self) -> Option<Vec<String>> {
if let Some(value) = self.get_value() {
self.notify_invalid("list of string", value.as_array())
Expand Down Expand Up @@ -182,6 +198,7 @@ impl Default for Settings {
file: PathBuf::new(),
autoload_hook: None,
autoload: None,
autoload_args: None,
clear_ignore: None,
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/shell/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ pub fn edit(output: &mut Output, shell: &dyn Shell, path: &Path) {
}
}

pub fn init(output: &mut Output, shell: &dyn Shell, autoload_hook: bool) {
let result = shell.init("sauce", autoload_hook);
pub fn init(output: &mut Output, shell: &dyn Shell, autoload_hook: bool, autoload_args: &str) {
let autoload_args = if autoload_args.is_empty() {
autoload_args.to_string()
} else {
String::from(" ") + autoload_args
};
let result = shell.init("sauce", autoload_hook, &autoload_args);
output.output(result);
}

Expand Down Expand Up @@ -194,7 +199,7 @@ mod tests {
fn it_defaults() {
let (out, err, mut output) = setup();
let shell = TestShell {};
init(&mut output, &shell, false);
init(&mut output, &shell, false, "");

assert_eq!(out.value(), "sauce\n");
assert_eq!(err.value(), "");
Expand All @@ -205,7 +210,7 @@ mod tests {
let (out, err, mut output) = setup();
let shell = TestShell {};

init(&mut output, &shell, true);
init(&mut output, &shell, true, "");

assert_eq!(out.value(), "sauce --autoload\n");
assert_eq!(err.value(), "");
Expand Down
3 changes: 2 additions & 1 deletion src/shell/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ impl<'a> Context<'a> {
pub fn init_shell(&mut self, shell_kind: &dyn Shell, output: &mut Output) {
self.load_settings(output);
let autoload_hook = self.settings().autoload_hook.unwrap_or(false);
actions::init(output, shell_kind, autoload_hook)
let autoload_args = self.settings().autoload_args.as_deref().unwrap_or("");
actions::init(output, shell_kind, autoload_hook, autoload_args)
}

pub fn execute_shell_command(
Expand Down
15 changes: 10 additions & 5 deletions src/shell/kinds/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ impl Shell for Bash {
"bash"
}

fn init(&self, binary: &str, autoload_hook: bool) -> String {
fn init(&self, binary: &str, autoload_hook: bool, autoload_args: &str) -> String {
let mut init = format!(
include_str!("bash_init.sh"),
binary,
qualify_binary_path(binary)
qualify_binary_path(binary),
);

if autoload_hook {
write!(init, include_str!("bash_init_autoload.sh"), binary).ok();
write!(
init,
include_str!("bash_init_autoload.sh"),
binary, autoload_args,
)
.ok();
}

init
Expand Down Expand Up @@ -71,7 +76,7 @@ mod tests {
#[test]
fn it_defaults() {
let shell = Bash {};
let output = shell.init("foo", false);
let output = shell.init("foo", false, "");
assert_eq!(
output,
"function foo {\n eval \"$(command foo --shell bash \"$@\")\"\n}\n"
Expand All @@ -81,7 +86,7 @@ mod tests {
#[test]
fn it_autoloads() {
let shell = Bash {};
let output = shell.init("foo", true);
let output = shell.init("foo", true, "");
assert_eq!(output.contains("--autoload"), true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/shell/kinds/bash_init_autoload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _chpwd_hook() {{
PROMPT_COMMAND="_chpwd_hook${{PROMPT_COMMAND:+;$PROMPT_COMMAND}}"

_{0}_autoload() {{
{0} --autoload
{0}{1} --autoload
}}

# append the command into CHPWD_COMMAND
Expand Down
15 changes: 10 additions & 5 deletions src/shell/kinds/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ impl Shell for Fish {
"fish"
}

fn init(&self, binary: &str, autoload_hook: bool) -> String {
fn init(&self, binary: &str, autoload_hook: bool, autoload_args: &str) -> String {
let mut init = format!(
include_str!("fish_init.fish"),
binary,
qualify_binary_path(binary)
qualify_binary_path(binary),
);

if autoload_hook {
write!(init, include_str!("fish_init_autoload.fish"), binary).ok();
write!(
init,
include_str!("fish_init_autoload.fish"),
binary, autoload_args,
)
.ok();
}

init
Expand Down Expand Up @@ -71,7 +76,7 @@ mod tests {
#[test]
fn it_defaults() {
let shell = Fish {};
let output = shell.init("foo", false);
let output = shell.init("foo", false, "");
assert_eq!(
output,
"function foo\n command foo --shell fish $argv | source\nend\n"
Expand All @@ -81,7 +86,7 @@ mod tests {
#[test]
fn it_includes_autoload() {
let shell = Fish {};
let output = shell.init("foo", true);
let output = shell.init("foo", true, "");
assert_eq!(output.contains("--autoload"), true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/shell/kinds/fish_init_autoload.fish
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function {0}_autoload --on-variable PWD;
{0} --autoload | source
{0}{1} --autoload | source
end
15 changes: 10 additions & 5 deletions src/shell/kinds/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ impl Shell for Zsh {
"zsh"
}

fn init(&self, binary: &str, autoload_hook: bool) -> String {
fn init(&self, binary: &str, autoload_hook: bool, autoload_args: &str) -> String {
let mut init = format!(
include_str!("zsh_init.zsh"),
binary,
qualify_binary_path(binary)
qualify_binary_path(binary),
);

if autoload_hook {
write!(init, include_str!("zsh_init_autoload.zsh"), binary).ok();
write!(
init,
include_str!("zsh_init_autoload.zsh"),
binary, autoload_args,
)
.ok();
}

init
Expand Down Expand Up @@ -71,7 +76,7 @@ mod tests {
#[test]
fn it_defaults() {
let shell = Zsh {};
let output = shell.init("foo", false);
let output = shell.init("foo", false, "");
assert_eq!(
output,
"function foo {\n eval \"$(command foo --shell zsh \"$@\")\"\n}\n"
Expand All @@ -81,7 +86,7 @@ mod tests {
#[test]
fn it_includes_autoload() {
let shell = Zsh {};
let output = shell.init("foo", true);
let output = shell.init("foo", true, "");
assert_eq!(output.contains("--autoload"), true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/shell/kinds/zsh_init_autoload.zsh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function _{0}_autoload {{
{0} --autoload
{0}{1} --autoload
}}

function _{0}_autoload_precmd {{
Expand Down
2 changes: 1 addition & 1 deletion src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait Shell {
editor.map(|e| format!("{} '{}'", e.to_string_lossy(), path))
}

fn init(&self, binary: &str, autoload: bool) -> String;
fn init(&self, binary: &str, autoload: bool, autoload_args: &str) -> String;
fn set_var(&self, var: &str, value: &str) -> String;
fn unset_var(&self, var: &str) -> String;

Expand Down
4 changes: 2 additions & 2 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl Shell for TestShell {
"test"
}

fn init(&self, binary: &str, autoload_hook: bool) -> String {
fn init(&self, binary: &str, autoload_hook: bool, autoload_args: &str) -> String {
if autoload_hook {
format!("{} {}", binary, "--autoload")
format!("{} {}{}", binary, "--autoload", autoload_args)
} else {
binary.to_string()
}
Expand Down

0 comments on commit c1e3f1d

Please sign in to comment.