Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add explicit flag to read from standard input #242

Merged
merged 7 commits into from
Jul 13, 2022
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
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/assets/media/new1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/assets/media/screencast.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions docs/note-creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

28 changes: 16 additions & 12 deletions internal/cli/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/cmd-new-template.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/cmd-new.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -103,15 +104,15 @@ $ 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
>

# 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
Expand Down
2 changes: 1 addition & 1 deletion tests/config-note-filename.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down