From 36365c68fb7dc9f5cab3b6b480e8d2c145650583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Thu, 11 Mar 2021 21:18:29 +0100 Subject: [PATCH] Expose environment variables to note templates with the new {{env}} variable --- CHANGELOG.md | 1 + core/note/create.go | 3 +++ core/note/create_test.go | 6 ++++++ core/note/format.go | 1 + docs/template-creation.md | 1 + util/os/os.go | 11 +++++++++++ 6 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd64a7c..e6f3fa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file. * Use glob patterns to match multiple tags, e.g. `--tag "book-*"`. * Many tag flavors are supported: `#hashtags`, `:colon:separated:tags:` ([opt-in](docs/note-format.md)) and even Bear's [`#multi-word tags#`](https://blog.bear.app/2017/11/bear-tips-how-to-create-multi-word-tags/) ([opt-in](docs/note-format.md)). If you prefer to use a YAML frontmatter, list your tags with the key `tags` or `keywords`. * Print metadata from the YAML frontmatter in `list` output using `{{metadata.}}`, e.g. `{{metadata.description}}`. Keys are normalized to lower case. +* Access environment variables from note templates with the `env.` template variable, e.g. `{{env.PATH}}`. ### Changed diff --git a/core/note/create.go b/core/note/create.go index dbf746e..2d7e4bf 100644 --- a/core/note/create.go +++ b/core/note/create.go @@ -10,6 +10,7 @@ import ( "github.com/mickael-menu/zk/util/date" "github.com/mickael-menu/zk/util/errors" "github.com/mickael-menu/zk/util/opt" + "github.com/mickael-menu/zk/util/os" "github.com/mickael-menu/zk/util/paths" "github.com/mickael-menu/zk/util/rand" ) @@ -96,6 +97,7 @@ type renderContext struct { FilenameStem string `handlebars:"filename-stem"` Extra map[string]string Now time.Time + Env map[string]string } type createDeps struct { @@ -116,6 +118,7 @@ func create( Dir: opts.Dir.Name, Extra: opts.Dir.Config.Extra, Now: deps.now, + Env: os.Env(), } path, context, err := genPath(context, opts.Dir, deps) diff --git a/core/note/create_test.go b/core/note/create_test.go index 7bf3f49..49e1acd 100644 --- a/core/note/create_test.go +++ b/core/note/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/mickael-menu/zk/core/templ" "github.com/mickael-menu/zk/core/zk" "github.com/mickael-menu/zk/util/opt" + "github.com/mickael-menu/zk/util/os" "github.com/mickael-menu/zk/util/test/assert" ) @@ -57,6 +58,7 @@ func TestCreate(t *testing.T) { "hello": "world", }, Now: Now, + Env: os.Env(), }}) assert.Equal(t, bodyTemplate.Contexts, []interface{}{renderContext{ ID: "abc", @@ -69,6 +71,7 @@ func TestCreate(t *testing.T) { "hello": "world", }, Now: Now, + Env: os.Env(), }}) } @@ -115,18 +118,21 @@ func TestCreateTriesUntilValidPath(t *testing.T) { Title: "Note title", Dir: "log", Now: Now, + Env: os.Env(), }, renderContext{ ID: "2", Title: "Note title", Dir: "log", Now: Now, + Env: os.Env(), }, renderContext{ ID: "3", Title: "Note title", Dir: "log", Now: Now, + Env: os.Env(), }, }) } diff --git a/core/note/format.go b/core/note/format.go index c94a738..56c07fb 100644 --- a/core/note/format.go +++ b/core/note/format.go @@ -128,4 +128,5 @@ type formatRenderContext struct { Created time.Time Modified time.Time Checksum string + Env map[string]string } diff --git a/docs/template-creation.md b/docs/template-creation.md index 1ad24d1..7bf74c5 100644 --- a/docs/template-creation.md +++ b/docs/template-creation.md @@ -10,6 +10,7 @@ The following variables are available in the templates used when [creating new n | `dir` | string | Parent directory in the notebook | | `extra.` | string | [Additional variables](config-extra.md) provided through the config file or `--extra` | | `now` | date | Current date and time, useful when paired with [`{{date now}}`](template.md) | +| `env` | map | Dictionary of case-sensitive environment variables, e.g. `{{env.PATH}}`. | These additional variables are available only to the note content template, once the filename is generated. diff --git a/util/os/os.go b/util/os/os.go index dcce62d..fb9a0c5 100644 --- a/util/os/os.go +++ b/util/os/os.go @@ -4,6 +4,7 @@ import ( "bufio" "io/ioutil" "os" + "strings" "github.com/mickael-menu/zk/util/opt" ) @@ -33,3 +34,13 @@ func ReadStdinPipe() (opt.String, error) { func GetOptEnv(key string) opt.String { return opt.NewNotEmptyString(os.Getenv(key)) } + +// Env returns a map of environment variables. +func Env() map[string]string { + env := map[string]string{} + for _, e := range os.Environ() { + pair := strings.SplitN(e, "=", 2) + env[pair[0]] = pair[1] + } + return env +}