Skip to content

Commit

Permalink
First pass at adding ID override to new notes
Browse files Browse the repository at this point in the history
  • Loading branch information
skbolton committed Mar 22, 2022
1 parent a0ced3c commit 276ea85
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/cli/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type New struct {
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 Down Expand Up @@ -54,6 +55,7 @@ func (cmd *New) Run(container *cli.Container) error {
Extra: cmd.Extra,
Date: date,
DryRun: cmd.DryRun,
ID: cmd.ID,
})

if cmd.DryRun {
Expand Down
13 changes: 12 additions & 1 deletion internal/core/notebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type NewNoteOpts struct {
Date time.Time
// Don't save the generated note on the file system.
DryRun bool
// Use a provided id over generating one
ID string
}

// ErrNoteExists is an error returned when a note already exists with the
Expand Down Expand Up @@ -148,6 +150,15 @@ func (n *Notebook) NewNote(opts NewNoteOpts) (*Note, error) {
return nil, wrap(err)
}

var idGenerator IDGenerator
if opts.ID != "" {
idGenerator = func() string {
return opts.ID
}
} else {
idGenerator = n.idGeneratorFactory(config.Note.IDOptions)
}

task := newNoteTask{
dir: dir,
title: opts.Title.OrString(config.Note.DefaultTitle).Unwrap(),
Expand All @@ -159,7 +170,7 @@ func (n *Notebook) NewNote(opts NewNoteOpts) (*Note, error) {
filenameTemplate: config.Note.FilenameTemplate + "." + config.Note.Extension,
bodyTemplatePath: opts.Template.Or(config.Note.BodyTemplatePath),
templates: templates,
genID: n.idGeneratorFactory(config.Note.IDOptions),
genID: idGenerator,
dryRun: opts.DryRun,
}
path, content, err := task.execute()
Expand Down
5 changes: 5 additions & 0 deletions tests/cmd-new.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ $ zk new --help
> -n, --dry-run Don't actually create the note. Instead, prints
> its content on stdout and the generated path on
> stderr.
> --id=ID Skip Id generation and use provided value.

# Default note title.
$ zk new --print-path
Expand All @@ -48,6 +49,10 @@ $ cat custom-title.md
>
>

# Provide a custom note id
$ zk new --group id --id 123abc --dry-run
2>{{working-dir}}/123abc.md

# Provide a custom title (short flag).
$ zk new -t "Another custom title" -p
>{{working-dir}}/another-custom-title.md
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/new/.zk/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ filename = "{{date now '%d-%m'}}"
template = "empty.md"
filename = "{{now}}"

[group.id.note]
template = "empty.md"
filename = "{{id}}"

[group.handlebars.note]
template = "handlebars.md"

Expand Down

0 comments on commit 276ea85

Please sign in to comment.