Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add shell to configuration file #302

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

All notable changes to this project will be documented in this file.

<!--## Unreleased-->
## 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

Expand Down
2 changes: 1 addition & 1 deletion docs/config-alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -63,10 +64,10 @@ author = "Mickaël"


# GROUP OVERRIDES
[dir.journal]
lsvmello marked this conversation as resolved.
Show resolved Hide resolved
[group.journal]
paths = ["journal/weekly", "journal/daily"]

[dir.journal.note]
[group.journal.note]
filename = "{{format-date now}}"


Expand All @@ -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"

Expand Down
14 changes: 14 additions & 0 deletions docs/tool-shell.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions internal/cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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"`
Expand Down
3 changes: 3 additions & 0 deletions internal/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestParseComplete(t *testing.T) {

[tool]
editor = "vim"
shell = "/bin/bash"
pager = "less"
fzf-preview = "bat {1}"
fzf-line = "{{title}}"
Expand Down Expand Up @@ -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}}"),
Expand Down
5 changes: 4 additions & 1 deletion internal/util/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
mickael-menu marked this conversation as resolved.
Show resolved Hide resolved
if value, ok := os.LookupEnv(key); ok {
return opt.NewNotEmptyString(value)
}
return opt.NullString
}

// Env returns a map of environment variables.
Expand Down