Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Format: `<description> (<contributor>, <commit>)`

## Unreleased

- Add --extra-json flag (by @mcDevnagh, #633)

## Fixed

- Release tarballs now output the program version (by @WhyNotHugo, 344b99f)
Expand Down
21 changes: 21 additions & 0 deletions docs/config/config-extra.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ section, which may override values from the root section.
[extra]
visibility = "public"
author = "Mickaël"
authors = ["Thomas", "Aristotle"]

[group.journal.extra]
visibility = "private" # overrides
Expand All @@ -33,6 +34,21 @@ $ zk new --extra author=Thomas
$ zk new --extra show-header=1,author=Thomas
```

`--extra-json` can also be used to pass a JSON object instead of command-line
arguments. The following commands are the JSON equivalent to the above ones:

```sh
$ zk new --extra-json '{"author": "Thomas"}'
$ zk new --extra-json '{"show-header": "1", "author": "Thomas"}'
```

While the value passed to `--extra-json` must be a JSON object, its values need
not be:
Comment on lines +45 to +46

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly do you mean here? The values are all valid JSON.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the values of its fields can be any valid JSON

for example:

{
  "key1": "string",
  "key2": 1,
  "key3": ["apple", "pear"],
  "key4": { "innerkey": "innervalue" }
}

my point was that we aren't limit to simple key/value pairs when using --extra-json, like we are when using --extra

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My question was aimed at the "its values need not be" part.


```sh
$ zk new --extra-json '{"show-header": 1, "authors": ["Thomas", "Aristotle"]}'
```

## Using extra variables in templates

After declaring extra variables, you can expand them inside the
Expand All @@ -45,4 +61,9 @@ After declaring extra variables, you can expand them inside the
Written by {{extra.author}}.

{{#if extra.show-header}} Behold, the mighty dynamic header! {{/if}}
{{#if extra.authors}}
{{#each}}
{{.}}
{{/each}}
{{/if}}
```
2 changes: 1 addition & 1 deletion internal/adapter/lsp/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type cmdNewOpts struct {
Dir string `json:"dir"`
Group string `json:"group"`
Template string `json:"template"`
Extra map[string]string `json:"extra"`
Extra map[string]any `json:"extra"`
Date string `json:"date"`
Edit jsonBoolean `json:"edit"`
DryRun jsonBoolean `json:"dryRun"`
Expand Down
18 changes: 17 additions & 1 deletion internal/cli/cmd/new.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -22,6 +23,7 @@ type New struct {
Date string ` placeholder:DATE help:"Set the current date."`
Group string `short:g placeholder:NAME help:"Name of the config group this note belongs to. Takes precedence over the config of the directory."`
Extra map[string]string ` help:"Extra variables passed to the templates." mapsep:","`
ExtraJSON string `name:"extra-json" help:"Extra variables passed to the templates, as a JSON object"`
Template string ` placeholder:PATH help:"Custom template used to render the note."`
PrintPath bool `short:p help:"Print the path of the created note instead of editing it."`
DryRun bool `short:n help:"Don't actually create the note. Instead, prints its content on stdout and the generated path on stderr."`
Expand All @@ -42,6 +44,20 @@ func (cmd *New) Run(container *cli.Container) error {
}
}

var extra map[string]any
if len(cmd.ExtraJSON) > 0 {
err = json.Unmarshal([]byte(cmd.ExtraJSON), &extra)
if err != nil {
return err
}
}
if extra == nil {
extra = make(map[string]any)
}
for k, v := range cmd.Extra {
extra[k] = v
}

date := time.Now()
if cmd.Date != "" {
date, err = dateutil.TimeFromNatural(cmd.Date)
Expand All @@ -56,7 +72,7 @@ func (cmd *New) Run(container *cli.Container) error {
Directory: opt.NewNotEmptyString(cmd.Directory),
Group: opt.NewNotEmptyString(cmd.Group),
Template: opt.NewNotEmptyString(cmd.Template),
Extra: cmd.Extra,
Extra: extra,
Date: date,
DryRun: cmd.DryRun,
ID: cmd.ID,
Expand Down
12 changes: 6 additions & 6 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Config struct {
LSP LSPConfig
Filters map[string]string
Aliases map[string]string
Extra map[string]string
Extra map[string]any
}

// NOTE: config generation occurs in core.Init. The below function is used
Expand Down Expand Up @@ -74,7 +74,7 @@ func NewDefaultConfig() Config {
},
Filters: map[string]string{},
Aliases: map[string]string{},
Extra: map[string]string{},
Extra: map[string]any{},
}
}

Expand Down Expand Up @@ -237,7 +237,7 @@ type NoteConfig struct {
type GroupConfig struct {
Paths []string
Note NoteConfig
Extra map[string]string
Extra map[string]any
}

// ExcludeGlobs returns all the Note.Exclude path globs for the group paths,
Expand All @@ -263,7 +263,7 @@ func (c GroupConfig) Clone() GroupConfig {
clone.Paths = make([]string, len(c.Paths))
copy(clone.Paths, c.Paths)

clone.Extra = make(map[string]string)
clone.Extra = map[string]any{}
for k, v := range c.Extra {
clone.Extra[k] = v
}
Expand Down Expand Up @@ -521,7 +521,7 @@ type tomlConfig struct {
Format tomlFormatConfig
Tool tomlToolConfig
LSP tomlLSPConfig
Extra map[string]string
Extra map[string]any
Filters map[string]string `toml:"filter"`
Aliases map[string]string `toml:"alias"`
}
Expand All @@ -546,7 +546,7 @@ type tomlNoteConfig struct {
type tomlGroupConfig struct {
Paths []string
Note tomlNoteConfig
Extra map[string]string
Extra map[string]any
}

type tomlFormatConfig struct {
Expand Down
20 changes: 10 additions & 10 deletions internal/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestParseDefaultConfig(t *testing.T) {
},
Filters: make(map[string]string),
Aliases: make(map[string]string),
Extra: make(map[string]string),
Extra: make(map[string]any),
})
}

Expand Down Expand Up @@ -180,7 +180,7 @@ func TestParseComplete(t *testing.T) {
DefaultTitle: "Ohne Titel",
Exclude: []string{"ignored", ".git", "new-ignored"},
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
"salut": "le monde",
"log-ext": "value",
Expand All @@ -201,7 +201,7 @@ func TestParseComplete(t *testing.T) {
DefaultTitle: "Sans titre",
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
"salut": "le monde",
},
Expand All @@ -221,7 +221,7 @@ func TestParseComplete(t *testing.T) {
DefaultTitle: "Sans titre",
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
"salut": "le monde",
},
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestParseComplete(t *testing.T) {
"ls": "zk list $@",
"ed": "zk edit $@",
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
"salut": "le monde",
},
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
DefaultTitle: "Sans titre",
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "override",
"salut": "le monde",
"log-ext": "value",
Expand All @@ -407,7 +407,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
DefaultTitle: "Sans titre",
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
"salut": "le monde",
},
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
},
Filters: make(map[string]string),
Aliases: make(map[string]string),
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
"salut": "le monde",
},
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestGroupConfigClone(t *testing.T) {
DefaultTitle: "Sans titre",
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
},
}
Expand Down Expand Up @@ -644,7 +644,7 @@ func TestGroupConfigClone(t *testing.T) {
DefaultTitle: "Sans titre",
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
Extra: map[string]any{
"hello": "world",
},
})
Expand Down
4 changes: 2 additions & 2 deletions internal/core/note_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type newNoteTask struct {
title string
content string
date time.Time
extra map[string]string
extra map[string]any
env map[string]string
fs FileStorage
filenameTemplate string
Expand Down Expand Up @@ -104,7 +104,7 @@ type newNoteTemplateContext struct {
Dir string
Filename string
FilenameStem string `handlebars:"filename-stem"`
Extra map[string]string
Extra map[string]any
Now time.Time
Env map[string]string
}
26 changes: 13 additions & 13 deletions internal/core/note_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNotebookNewNote(t *testing.T) {
note, err := test.run(NewNoteOpts{
Title: opt.NewString("Note title"),
Content: "Note content",
Extra: map[string]string{
Extra: map[string]any{
"add-extra": "ec83da",
},
Date: now,
Expand All @@ -45,7 +45,7 @@ func TestNotebookNewNote(t *testing.T) {
Dir: "",
Filename: "",
FilenameStem: "",
Extra: map[string]string{"add-extra": "ec83da", "conf-extra": "38srnw"},
Extra: map[string]any{"add-extra": "ec83da", "conf-extra": "38srnw"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand All @@ -58,7 +58,7 @@ func TestNotebookNewNote(t *testing.T) {
Dir: "",
Filename: "filename.ext",
FilenameStem: "filename",
Extra: map[string]string{"add-extra": "ec83da", "conf-extra": "38srnw"},
Extra: map[string]any{"add-extra": "ec83da", "conf-extra": "38srnw"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand All @@ -80,7 +80,7 @@ func TestNotebookNewNoteWithDefaultTitle(t *testing.T) {
newNoteTemplateContext{
ID: "id",
Title: "Titre par défaut",
Extra: map[string]string{"conf-extra": "38srnw"},
Extra: map[string]any{"conf-extra": "38srnw"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestNotebookNewNoteInDir(t *testing.T) {
Dir: "a-dir",
Filename: "",
FilenameStem: "",
Extra: map[string]string{"conf-extra": "38srnw"},
Extra: map[string]any{"conf-extra": "38srnw"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand All @@ -141,7 +141,7 @@ func TestNotebookNewNoteInDir(t *testing.T) {
Dir: "a-dir",
Filename: "filename.ext",
FilenameStem: "filename",
Extra: map[string]string{"conf-extra": "38srnw"},
Extra: map[string]any{"conf-extra": "38srnw"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand All @@ -165,7 +165,7 @@ func TestNotebookNewNoteInDirWithGroup(t *testing.T) {
Case: CaseMixed,
},
},
Extra: map[string]string{
Extra: map[string]any{
"group-extra": "e48rs",
},
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestNotebookNewNoteInDirWithGroup(t *testing.T) {
Dir: "a-dir",
Filename: "",
FilenameStem: "",
Extra: map[string]string{"group-extra": "e48rs"},
Extra: map[string]any{"group-extra": "e48rs"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand All @@ -217,7 +217,7 @@ func TestNotebookNewNoteInDirWithGroup(t *testing.T) {
Dir: "a-dir",
Filename: "group-filename.group-ext",
FilenameStem: "group-filename",
Extra: map[string]string{"group-extra": "e48rs"},
Extra: map[string]any{"group-extra": "e48rs"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand All @@ -240,7 +240,7 @@ func TestNotebookNewNoteWithGroup(t *testing.T) {
Case: CaseMixed,
},
},
Extra: map[string]string{
Extra: map[string]any{
"group-extra": "e48rs",
},
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestNotebookNewNoteWithGroup(t *testing.T) {
Dir: "",
Filename: "",
FilenameStem: "",
Extra: map[string]string{"group-extra": "e48rs"},
Extra: map[string]any{"group-extra": "e48rs"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand All @@ -292,7 +292,7 @@ func TestNotebookNewNoteWithGroup(t *testing.T) {
Dir: "",
Filename: "group-filename.group-ext",
FilenameStem: "group-filename",
Extra: map[string]string{"group-extra": "e48rs"},
Extra: map[string]any{"group-extra": "e48rs"},
Now: now,
Env: map[string]string{"KEY1": "foo", "KEY2": "bar"},
},
Expand Down Expand Up @@ -459,7 +459,7 @@ func (t *newNoteTest) setup() {
},
},
Groups: t.groups,
Extra: map[string]string{
Extra: map[string]any{
"conf-extra": "38srnw",
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/notebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type NewNoteOpts struct {
// Path to a custom template used to render the note.
Template opt.String
// Extra variables passed to the templates.
Extra map[string]string
Extra map[string]any
// Creation date provided to the templates.
Date time.Time
// Don't save the generated note on the file system.
Expand Down
Loading