Add explicit flag to read from standard input (#242)

pull/245/head
Pete Kazmier 2 years ago committed by GitHub
parent eefc3be9c6
commit a6e522562e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,7 +2,16 @@
All notable changes to this project will be documented in this file.
<!--## Unreleased-->
## Unreleased
### Changed
* `zk new` now requires the `--interactive`/`-i` flag to read the note body from a pipe or standard input. [See rational](https://github.com/mickael-menu/zk/pull/242#issuecomment-1182602001).
### Fixed
* [#244](https://github.com/mickael-menu/zk/issues/244) Fixed `zk new` waiting for `Ctrl-D` to proceed (contributed by [@pkazmier](https://github.com/mickael-menu/zk/pull/242)).
## 0.11.0

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 166 KiB

After

Width:  |  Height:  |  Size: 166 KiB

@ -14,11 +14,17 @@ This option is available when running `zk edit --interactive`, which spawns [`fz
## Create a note with initial content
Initial content can be fed to the template through a standard input pipe, which will be expandable with the `{{content}}` [template variable](template-creation.md).
Initial content can be fed to the template through standard input using `zk new --interactive`, which will be expandable with the `{{content}}` [template variable](template-creation.md).
For example, to use the content of the macOS clipboard as the initial content you can run:
```sh
$ pbpaste | zk new
$ pbpaste | zk new --interactive
```
Alternatively, you can use the content of a file:
```sh
$ zk new --interactive < file.txt
```

@ -16,15 +16,16 @@ import (
// New adds a new note to the notebook.
type New struct {
Directory string `arg optional default:"." help:"Directory in which to create the note."`
Title string `short:t placeholder:TITLE help:"Title of the new note."`
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:","`
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."`
ID string ` placeholder:ID help:"Skip id generation and use provided value."`
Directory string `arg optional default:"." help:"Directory in which to create the note."`
Interactive bool `short:i help:"Read contents from standard input."`
Title string `short:t placeholder:TITLE help:"Title of the new note."`
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:","`
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."`
ID string ` placeholder:ID help:"Skip id generation and use provided value."`
}
func (cmd *New) Run(container *cli.Container) error {
@ -33,9 +34,12 @@ func (cmd *New) Run(container *cli.Container) error {
return err
}
content, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
var content []byte
if cmd.Interactive {
content, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
}
date := time.Now()

@ -3,7 +3,7 @@ $ cd new
$ mkdir "a dir"
# Test Handlebars template variables.
$ echo "Piped content" | zk new --group handlebars --title "Note title" --date "January 2nd" --dry-run "a dir"
$ echo "Piped content" | zk new --interactive --group handlebars --title "Note title" --date "January 2nd" --dry-run "a dir"
>id: {{match "[a-z0-9]{4}"}}
>title: Note title
>content: Piped content

@ -17,6 +17,7 @@ $ zk new --help
> current working directory.
> --no-input Never prompt or ask for confirmation.
>
> -i, --interactive Read contents from standard input.
> -t, --title=TITLE Title of the new note.
> --date=DATE Set the current date.
> -g, --group=NAME Name of the config group this note belongs to.
@ -103,7 +104,7 @@ $ zk new -n --title "Dry run"
2>{{working-dir}}/dry-run.md
# Pipe content in a new note.
$ echo "Content of the note" | EDITOR=cat zk new --title "Piped note"
$ echo "Content of the note" | EDITOR=cat zk new --interactive --title "Piped note"
># Piped note
>
>Content of the note
@ -111,7 +112,7 @@ $ echo "Content of the note" | EDITOR=cat zk new --title "Piped note"
# Redirect file to standard input when creating a new note.
$ echo "Content of the note" > input
$ EDITOR=cat zk new --title "Note from redirected input" < input
$ EDITOR=cat zk new --interactive --title "Note from redirected input" < input
># Note from redirected input
>
>Content of the note

@ -17,7 +17,7 @@ $ zk new --title "A new note" --date "January 5th" --dry-run
# Test the filename Handlebars variables.
$ mkdir "a dir"
$ echo "[note]\n filename = '\{{title}},\{{content}},\{{date now \"%m-%d\"}},\{{json extra}}'" > .zk/config.toml
$ echo "Piped content" | zk new --title "A new note" --date "January 5th" --extra key=value --dry-run
$ echo "Piped content" | zk new --interactive --title "A new note" --date "January 5th" --extra key=value --dry-run
2>{{working-dir}}/A new note,Piped content
2>,01-05,{"key":"value"}.md
$ echo "[note]\n filename = '\{{id}},\{{dir}},\{{json extra}},\{{env.ZK_NOTEBOOK_DIR}}'" > .zk/config.toml

Loading…
Cancel
Save