From a8d1db4c5738b8ae549bf87c079b829396bcbe9e Mon Sep 17 00:00:00 2001 From: Leonardo Mello Date: Tue, 28 Mar 2023 16:29:18 -0300 Subject: [PATCH] Add `tool.shell` configuration key (#302) --- CHANGELOG.md | 6 +++++- docs/config-alias.md | 2 +- docs/config.md | 8 ++++++-- docs/tool-shell.md | 14 ++++++++++++++ internal/cli/container.go | 5 +++++ internal/core/config.go | 5 +++++ internal/core/config_test.go | 3 +++ internal/util/os/os.go | 5 ++++- 8 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 docs/tool-shell.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cacfd7..f328aa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,11 @@ All notable changes to this project will be documented in this file. - +## Unreleased + +### Added + +* New [`tool.shell`](docs/tool-shell.md) configuration key to set a custom shell (contributed by [@lsvmello](https://github.com/mickael-menu/zk/pull/302)). ## 0.13.0 diff --git a/docs/config-alias.md b/docs/config-alias.md index bb55a00..2bedb4e 100644 --- a/docs/config-alias.md +++ b/docs/config-alias.md @@ -6,7 +6,7 @@ Declaring your own aliases is a great way to make your experience with `zk` easi ## Configuring aliases -Command aliases are declared in your [configuration file](config.md), under the `[alias]` section. They are executed with `$SHELL -c`, which allows you to: +Command aliases are declared in your [configuration file](config.md), under the `[alias]` section. They are executed with [your default shell](tool-shell.md), which allows you to: * expand arguments with `$@` or `$*` * expand environment variables diff --git a/docs/config.md b/docs/config.md index a8cff1c..81a0bc6 100644 --- a/docs/config.md +++ b/docs/config.md @@ -8,6 +8,7 @@ Each [notebook](notebook.md) contains a configuration file used to customize you * `[format]` configures the [note format settings](note-format.md), such as Markdown options * `[tool]` customizes interaction with external programs such as: * [your default editor](tool-editor.md) + * [your default shell](tool-shell.md) * [your default pager](tool-pager.md) * [`fzf`](tool-fzf.md) * `[lsp]` setups the [Language Server Protocol settings](config-lsp.md) for [editors integration](editors-integration.md) @@ -63,10 +64,10 @@ author = "Mickaƫl" # GROUP OVERRIDES -[dir.journal] +[group.journal] paths = ["journal/weekly", "journal/daily"] -[dir.journal.note] +[group.journal.note] filename = "{{format-date now}}" @@ -84,6 +85,9 @@ colon-tags = true # Default editor used to open notes. editor = "nvim" +# Default shell used by aliases and commands. +shell = "/bin/bash" + # Pager used to scroll through long output. pager = "less -FIRX" diff --git a/docs/tool-shell.md b/docs/tool-shell.md new file mode 100644 index 0000000..db785a7 --- /dev/null +++ b/docs/tool-shell.md @@ -0,0 +1,14 @@ +# Setting your default shell + +This is *currently* not supported on Windows (that defaults always to `cmd`). + +You can customize which shell to use to run aliases and commands either from the [configuration file](config.md) or environment variables. In order of precedence, `zk` will use: + +1. `ZK_SHELL` environment variable +2. `shell` configuration property + ```toml + [tool] + shell = "/bin/bash" + ``` +3. `SHELL` environment variable +4. `sh` as fallback diff --git a/internal/cli/container.go b/internal/cli/container.go index 56c26ce..eb19085 100644 --- a/internal/cli/container.go +++ b/internal/cli/container.go @@ -71,6 +71,11 @@ func NewContainer(version string) (*Container, error) { } } + // Set the default shell if not already set + if osutil.GetOptEnv("ZK_SHELL").IsNull() && !config.Tool.Shell.IsEmpty() { + os.Setenv("ZK_SHELL", config.Tool.Shell.Unwrap()) + } + return &Container{ Version: version, Config: config, diff --git a/internal/core/config.go b/internal/core/config.go index 8db108f..d6778dc 100644 --- a/internal/core/config.go +++ b/internal/core/config.go @@ -148,6 +148,7 @@ type MarkdownConfig struct { // ToolConfig holds the external tooling configuration. type ToolConfig struct { Editor opt.String + Shell opt.String Pager opt.String FzfPreview opt.String FzfLine opt.String @@ -355,6 +356,9 @@ func ParseConfig(content []byte, path string, parentConfig Config) (Config, erro if tool.Editor != nil { config.Tool.Editor = opt.NewNotEmptyString(*tool.Editor) } + if tool.Shell != nil { + config.Tool.Shell = opt.NewNotEmptyString(*tool.Shell) + } if tool.Pager != nil { config.Tool.Pager = opt.NewStringWithPtr(tool.Pager) } @@ -511,6 +515,7 @@ type tomlMarkdownConfig struct { type tomlToolConfig struct { Editor *string + Shell *string Pager *string FzfPreview *string `toml:"fzf-preview"` FzfLine *string `toml:"fzf-line"` diff --git a/internal/core/config_test.go b/internal/core/config_test.go index c29e51a..2c534cf 100644 --- a/internal/core/config_test.go +++ b/internal/core/config_test.go @@ -40,6 +40,7 @@ func TestParseDefaultConfig(t *testing.T) { }, Tool: ToolConfig{ Editor: opt.NullString, + Shell: opt.NullString, Pager: opt.NullString, FzfPreview: opt.NullString, FzfLine: opt.NullString, @@ -86,6 +87,7 @@ func TestParseComplete(t *testing.T) { [tool] editor = "vim" + shell = "/bin/bash" pager = "less" fzf-preview = "bat {1}" fzf-line = "{{title}}" @@ -228,6 +230,7 @@ func TestParseComplete(t *testing.T) { }, Tool: ToolConfig{ Editor: opt.NewString("vim"), + Shell: opt.NewString("/bin/bash"), Pager: opt.NewString("less"), FzfPreview: opt.NewString("bat {1}"), FzfLine: opt.NewString("{{title}}"), diff --git a/internal/util/os/os.go b/internal/util/os/os.go index 038a388..262aa55 100644 --- a/internal/util/os/os.go +++ b/internal/util/os/os.go @@ -10,7 +10,10 @@ import ( // Getenv returns an optional String for the environment variable with given // key. func GetOptEnv(key string) opt.String { - return opt.NewNotEmptyString(os.Getenv(key)) + if value, ok := os.LookupEnv(key); ok { + return opt.NewNotEmptyString(value) + } + return opt.NullString } // Env returns a map of environment variables.