Expose environment variables to note templates with the new {{env}} variable

pull/8/head
Mickaël Menu 3 years ago
parent 9fda155a99
commit 36365c68fb
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -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-*"`. * 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`. * 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.<key>}}`, e.g. `{{metadata.description}}`. Keys are normalized to lower case. * Print metadata from the YAML frontmatter in `list` output using `{{metadata.<key>}}`, e.g. `{{metadata.description}}`. Keys are normalized to lower case.
* Access environment variables from note templates with the `env.<key>` template variable, e.g. `{{env.PATH}}`.
### Changed ### Changed

@ -10,6 +10,7 @@ import (
"github.com/mickael-menu/zk/util/date" "github.com/mickael-menu/zk/util/date"
"github.com/mickael-menu/zk/util/errors" "github.com/mickael-menu/zk/util/errors"
"github.com/mickael-menu/zk/util/opt" "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/paths"
"github.com/mickael-menu/zk/util/rand" "github.com/mickael-menu/zk/util/rand"
) )
@ -96,6 +97,7 @@ type renderContext struct {
FilenameStem string `handlebars:"filename-stem"` FilenameStem string `handlebars:"filename-stem"`
Extra map[string]string Extra map[string]string
Now time.Time Now time.Time
Env map[string]string
} }
type createDeps struct { type createDeps struct {
@ -116,6 +118,7 @@ func create(
Dir: opts.Dir.Name, Dir: opts.Dir.Name,
Extra: opts.Dir.Config.Extra, Extra: opts.Dir.Config.Extra,
Now: deps.now, Now: deps.now,
Env: os.Env(),
} }
path, context, err := genPath(context, opts.Dir, deps) path, context, err := genPath(context, opts.Dir, deps)

@ -7,6 +7,7 @@ import (
"github.com/mickael-menu/zk/core/templ" "github.com/mickael-menu/zk/core/templ"
"github.com/mickael-menu/zk/core/zk" "github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util/opt" "github.com/mickael-menu/zk/util/opt"
"github.com/mickael-menu/zk/util/os"
"github.com/mickael-menu/zk/util/test/assert" "github.com/mickael-menu/zk/util/test/assert"
) )
@ -57,6 +58,7 @@ func TestCreate(t *testing.T) {
"hello": "world", "hello": "world",
}, },
Now: Now, Now: Now,
Env: os.Env(),
}}) }})
assert.Equal(t, bodyTemplate.Contexts, []interface{}{renderContext{ assert.Equal(t, bodyTemplate.Contexts, []interface{}{renderContext{
ID: "abc", ID: "abc",
@ -69,6 +71,7 @@ func TestCreate(t *testing.T) {
"hello": "world", "hello": "world",
}, },
Now: Now, Now: Now,
Env: os.Env(),
}}) }})
} }
@ -115,18 +118,21 @@ func TestCreateTriesUntilValidPath(t *testing.T) {
Title: "Note title", Title: "Note title",
Dir: "log", Dir: "log",
Now: Now, Now: Now,
Env: os.Env(),
}, },
renderContext{ renderContext{
ID: "2", ID: "2",
Title: "Note title", Title: "Note title",
Dir: "log", Dir: "log",
Now: Now, Now: Now,
Env: os.Env(),
}, },
renderContext{ renderContext{
ID: "3", ID: "3",
Title: "Note title", Title: "Note title",
Dir: "log", Dir: "log",
Now: Now, Now: Now,
Env: os.Env(),
}, },
}) })
} }

@ -128,4 +128,5 @@ type formatRenderContext struct {
Created time.Time Created time.Time
Modified time.Time Modified time.Time
Checksum string Checksum string
Env map[string]string
} }

@ -10,6 +10,7 @@ The following variables are available in the templates used when [creating new n
| `dir` | string | Parent directory in the notebook | | `dir` | string | Parent directory in the notebook |
| `extra.<key>` | string | [Additional variables](config-extra.md) provided through the config file or `--extra` | | `extra.<key>` | 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) | | `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. These additional variables are available only to the note content template, once the filename is generated.

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"github.com/mickael-menu/zk/util/opt" "github.com/mickael-menu/zk/util/opt"
) )
@ -33,3 +34,13 @@ func ReadStdinPipe() (opt.String, error) {
func GetOptEnv(key string) opt.String { func GetOptEnv(key string) opt.String {
return opt.NewNotEmptyString(os.Getenv(key)) 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
}

Loading…
Cancel
Save