zk/cmd/new.go

46 lines
1.2 KiB
Go
Raw Normal View History

package cmd
2020-12-25 19:25:52 +00:00
import (
"fmt"
2020-12-28 15:00:48 +00:00
"github.com/mickael-menu/zk/core/note"
2020-12-25 19:25:52 +00:00
"github.com/mickael-menu/zk/core/zk"
2020-12-28 15:00:48 +00:00
"github.com/mickael-menu/zk/util/opt"
2020-12-25 19:25:52 +00:00
)
2020-12-28 15:00:48 +00:00
// New adds a new note to the slip box.
type New struct {
2020-12-28 15:00:48 +00:00
Directory string `arg optional type:"path" default:"." help:"Directory in which to create the note"`
ShowPath bool `help:"Shows the path of the created note instead of editing it"`
Title string `short:"t" help:"Title of the new note" placeholder:"TITLE"`
Template string `type:"path" help:"Custom template to use to render the note" placeholder:"PATH"`
Extra map[string]string `help:"Extra variables passed to the templates"`
}
2020-12-28 15:00:48 +00:00
func (cmd *New) Run(container *Container) error {
zk, err := zk.Open(".")
if err != nil {
return err
}
dir, err := zk.DirAt(cmd.Directory)
if err != nil {
return err
}
opts := note.CreateOpts{
Dir: *dir,
Title: opt.NewNotEmptyString(cmd.Title),
Content: opt.NullString,
Template: opt.NewNotEmptyString(cmd.Template),
Extra: cmd.Extra,
}
2020-12-29 16:22:19 +00:00
file, err := note.Create(zk, opts, container.TemplateLoader())
2020-12-28 15:00:48 +00:00
if err != nil {
return err
}
fmt.Printf("%+v\n", file)
return nil
}