From 59b8269344363dca2439cb168e847aa9f35f4444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Sat, 30 Oct 2021 16:44:46 +0200 Subject: [PATCH] Add `--dry-run` flag for `zk new` (#96) --- CHANGELOG.md | 1 + internal/cli/cmd/new.go | 18 ++++++++++++++++-- internal/core/note_new.go | 21 ++++++++++++--------- internal/core/note_parse.go | 15 ++++++++++----- internal/core/notebook.go | 17 +++++++++++------ 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae058d2..79ab46a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file. # Show the note filename without extension as detail. note-detail = "{{filename-stem}}" ``` +* New `--dry-run` flag for `zk new` which prints out the path and content of the generated note instead of saving it to the file system. ### Fixed diff --git a/internal/cli/cmd/new.go b/internal/cli/cmd/new.go index 0999e73..1649e43 100644 --- a/internal/cli/cmd/new.go +++ b/internal/cli/cmd/new.go @@ -3,13 +3,14 @@ package cmd import ( "errors" "fmt" + "os" "path/filepath" "time" "github.com/mickael-menu/zk/internal/cli" "github.com/mickael-menu/zk/internal/core" "github.com/mickael-menu/zk/internal/util/opt" - "github.com/mickael-menu/zk/internal/util/os" + osutil "github.com/mickael-menu/zk/internal/util/os" ) // New adds a new note to the notebook. @@ -20,6 +21,7 @@ type New struct { 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."` } func (cmd *New) Run(container *cli.Container) error { @@ -28,7 +30,7 @@ func (cmd *New) Run(container *cli.Container) error { return err } - content, err := os.ReadStdinPipe() + content, err := osutil.ReadStdinPipe() if err != nil { return err } @@ -41,7 +43,19 @@ func (cmd *New) Run(container *cli.Container) error { Template: opt.NewNotEmptyString(cmd.Template), Extra: cmd.Extra, Date: time.Now(), + DryRun: cmd.DryRun, }) + + if cmd.DryRun { + if err != nil { + return err + } + path := filepath.Join(notebook.Path, note.Path) + fmt.Fprintln(os.Stderr, path) + fmt.Print(note.RawContent) + return nil + } + var path string if err == nil { path = filepath.Join(notebook.Path, note.Path) diff --git a/internal/core/note_new.go b/internal/core/note_new.go index 80a28b1..923d139 100644 --- a/internal/core/note_new.go +++ b/internal/core/note_new.go @@ -20,19 +20,20 @@ type newNoteTask struct { bodyTemplatePath opt.String templates TemplateLoader genID IDGenerator + dryRun bool } -func (t *newNoteTask) execute() (string, error) { +func (t *newNoteTask) execute() (string, string, error) { filenameTemplate, err := t.templates.LoadTemplate(t.filenameTemplate) if err != nil { - return "", err + return "", "", err } var contentTemplate Template = NullTemplate if templatePath := t.bodyTemplatePath.Unwrap(); templatePath != "" { contentTemplate, err = t.templates.LoadTemplateAt(templatePath) if err != nil { - return "", err + return "", "", err } } @@ -47,20 +48,22 @@ func (t *newNoteTask) execute() (string, error) { path, context, err := t.generatePath(context, filenameTemplate) if err != nil { - return "", err + return "", "", err } content, err := contentTemplate.Render(context) if err != nil { - return "", err + return "", "", err } - err = t.fs.Write(path, []byte(content)) - if err != nil { - return "", err + if !t.dryRun { + err = t.fs.Write(path, []byte(content)) + if err != nil { + return "", "", err + } } - return path, nil + return path, content, nil } func (c *newNoteTask) generatePath(context newNoteTemplateContext, filenameTemplate Template) (string, newNoteTemplateContext, error) { diff --git a/internal/core/note_parse.go b/internal/core/note_parse.go index cfb1a95..1321bae 100644 --- a/internal/core/note_parse.go +++ b/internal/core/note_parse.go @@ -44,15 +44,22 @@ type NoteContent struct { func (n *Notebook) ParseNoteAt(absPath string) (*Note, error) { wrap := errors.Wrapper(absPath) - relPath, err := n.RelPath(absPath) + content, err := n.fs.Read(absPath) if err != nil { return nil, wrap(err) } - content, err := n.fs.Read(absPath) + return n.ParseNoteWithContent(absPath, content) +} + +func (n *Notebook) ParseNoteWithContent(absPath string, content []byte) (*Note, error) { + wrap := errors.Wrapper(absPath) + + relPath, err := n.RelPath(absPath) if err != nil { return nil, wrap(err) } + contentStr := string(content) contentParts, err := n.parser.ParseNoteContent(contentStr) if err != nil { @@ -86,9 +93,7 @@ func (n *Notebook) ParseNoteAt(absPath string) (*Note, error) { } times, err := times.Stat(absPath) - if err != nil { - n.logger.Err(err) - } else { + if err == nil { note.Modified = times.ModTime().UTC() note.Created = creationDateFrom(note.Metadata, times) } diff --git a/internal/core/notebook.go b/internal/core/notebook.go index 0f812f1..6da957f 100644 --- a/internal/core/notebook.go +++ b/internal/core/notebook.go @@ -108,6 +108,8 @@ type NewNoteOpts struct { Extra map[string]string // Creation date provided to the templates. Date time.Time + // Don't save the generated note on the file system. + DryRun bool } // ErrNoteExists is an error returned when a note already exists with the @@ -159,23 +161,26 @@ func (n *Notebook) NewNote(opts NewNoteOpts) (*Note, error) { bodyTemplatePath: opts.Template.Or(config.Note.BodyTemplatePath), templates: templates, genID: n.idGeneratorFactory(config.Note.IDOptions), + dryRun: opts.DryRun, } - path, err := task.execute() + path, content, err := task.execute() if err != nil { return nil, wrap(err) } - note, err := n.ParseNoteAt(path) + note, err := n.ParseNoteWithContent(path, []byte(content)) if note == nil || err != nil { return nil, wrap(err) } - id, err := n.index.Add(*note) - if err != nil { - return nil, wrap(err) + if !opts.DryRun { + id, err := n.index.Add(*note) + if err != nil { + return nil, wrap(err) + } + note.ID = id } - note.ID = id return note, nil }