From 72a9d7c0fab1b4c2daa16e65725c70d6a0d4e836 Mon Sep 17 00:00:00 2001 From: Leonardo Mello Date: Mon, 27 Mar 2023 13:31:18 -0300 Subject: [PATCH 1/2] Add `shell` to configuration file --- 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 ++++- 7 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 docs/tool-shell.md diff --git a/docs/config-alias.md b/docs/config-alias.md index bb55a00b..2bedb4e4 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 a8cff1cf..81a0bc67 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 00000000..db785a73 --- /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 56c26ce0..eb19085c 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 8db108f7..d6778dc0 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 c29e51a6..2c534cf4 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 038a388c..262aa553 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. From 19e7faa2be9dcb86c5f073b8498e53d60c01ec0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Tue, 28 Mar 2023 21:24:26 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cacfd7d..f328aa9f 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