2020-12-28 12:15:56 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-01-16 13:13:57 +00:00
|
|
|
"io"
|
2021-02-10 20:14:39 +00:00
|
|
|
"os"
|
2021-01-24 11:10:13 +00:00
|
|
|
"sync"
|
2021-01-16 13:13:57 +00:00
|
|
|
|
2021-01-23 12:29:14 +00:00
|
|
|
"github.com/mickael-menu/zk/adapter/fzf"
|
2020-12-28 12:15:56 +00:00
|
|
|
"github.com/mickael-menu/zk/adapter/handlebars"
|
2021-01-16 18:37:10 +00:00
|
|
|
"github.com/mickael-menu/zk/adapter/markdown"
|
2021-01-02 11:29:21 +00:00
|
|
|
"github.com/mickael-menu/zk/adapter/sqlite"
|
2021-01-23 20:29:22 +00:00
|
|
|
"github.com/mickael-menu/zk/adapter/term"
|
2021-01-16 13:13:57 +00:00
|
|
|
"github.com/mickael-menu/zk/core/zk"
|
2020-12-28 12:15:56 +00:00
|
|
|
"github.com/mickael-menu/zk/util"
|
|
|
|
"github.com/mickael-menu/zk/util/date"
|
2021-01-16 13:13:57 +00:00
|
|
|
"github.com/mickael-menu/zk/util/pager"
|
2020-12-28 12:15:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Container struct {
|
2020-12-29 16:22:19 +00:00
|
|
|
Date date.Provider
|
|
|
|
Logger util.Logger
|
2021-01-23 20:29:22 +00:00
|
|
|
Terminal *term.Terminal
|
2020-12-29 16:22:19 +00:00
|
|
|
templateLoader *handlebars.Loader
|
2021-01-24 11:10:13 +00:00
|
|
|
|
|
|
|
zkOnce sync.Once
|
|
|
|
zk *zk.Zk
|
|
|
|
zkErr error
|
2020-12-28 12:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewContainer() *Container {
|
|
|
|
date := date.NewFrozenNow()
|
|
|
|
|
|
|
|
return &Container{
|
2021-01-03 13:43:27 +00:00
|
|
|
Logger: util.NewStdLogger("zk: ", 0),
|
2020-12-28 12:15:56 +00:00
|
|
|
// zk is short-lived, so we freeze the current date to use the same
|
|
|
|
// date for any rendering during the execution.
|
2021-01-23 20:29:22 +00:00
|
|
|
Date: &date,
|
|
|
|
Terminal: term.New(),
|
2020-12-28 12:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 11:10:13 +00:00
|
|
|
func (c *Container) OpenZk() (*zk.Zk, error) {
|
|
|
|
c.zkOnce.Do(func() {
|
|
|
|
c.zk, c.zkErr = zk.Open(".")
|
2021-02-10 20:14:39 +00:00
|
|
|
if c.zkErr == nil {
|
|
|
|
os.Setenv("ZK_PATH", c.zk.Path)
|
|
|
|
}
|
2021-01-24 11:10:13 +00:00
|
|
|
})
|
|
|
|
return c.zk, c.zkErr
|
|
|
|
}
|
|
|
|
|
2021-01-03 17:03:18 +00:00
|
|
|
func (c *Container) TemplateLoader(lang string) *handlebars.Loader {
|
2020-12-29 16:22:19 +00:00
|
|
|
if c.templateLoader == nil {
|
2021-01-23 20:29:22 +00:00
|
|
|
handlebars.Init(lang, c.Logger, c.Terminal)
|
2020-12-29 16:22:19 +00:00
|
|
|
c.templateLoader = handlebars.NewLoader()
|
2020-12-28 12:15:56 +00:00
|
|
|
}
|
2020-12-29 16:22:19 +00:00
|
|
|
return c.templateLoader
|
2020-12-28 12:15:56 +00:00
|
|
|
}
|
2021-01-02 11:29:21 +00:00
|
|
|
|
2021-01-16 18:37:10 +00:00
|
|
|
func (c *Container) Parser() *markdown.Parser {
|
|
|
|
return markdown.NewParser()
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:48:23 +00:00
|
|
|
func (c *Container) NoteFinder(tx sqlite.Transaction, opts fzf.NoteFinderOpts) *fzf.NoteFinder {
|
2021-01-23 12:29:14 +00:00
|
|
|
notes := sqlite.NewNoteDAO(tx, c.Logger)
|
2021-02-07 16:48:23 +00:00
|
|
|
return fzf.NewNoteFinder(opts, notes, c.Terminal)
|
2021-01-23 12:29:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 21:44:31 +00:00
|
|
|
// Database returns the DB instance for the given notebook, after executing any
|
2021-01-02 11:29:21 +00:00
|
|
|
// pending migration.
|
2021-01-03 17:03:18 +00:00
|
|
|
func (c *Container) Database(path string) (*sqlite.DB, error) {
|
|
|
|
db, err := sqlite.Open(path)
|
2021-01-02 11:29:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = db.Migrate()
|
|
|
|
return db, err
|
|
|
|
}
|
2021-01-16 13:13:57 +00:00
|
|
|
|
|
|
|
// Paginate creates an auto-closing io.Writer which will be automatically
|
|
|
|
// paginated if noPager is false, using the user's pager.
|
|
|
|
//
|
|
|
|
// You can write to the pager only in the run callback.
|
|
|
|
func (c *Container) Paginate(noPager bool, config zk.Config, run func(out io.Writer) error) error {
|
2021-02-13 21:16:11 +00:00
|
|
|
pager, err := c.pager(noPager || config.Tool.Pager.IsEmpty(), config)
|
2021-01-16 13:13:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = run(pager)
|
|
|
|
pager.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) pager(noPager bool, config zk.Config) (*pager.Pager, error) {
|
2021-02-10 19:53:25 +00:00
|
|
|
if noPager || !c.Terminal.IsInteractive() {
|
2021-01-16 13:13:57 +00:00
|
|
|
return pager.PassthroughPager, nil
|
|
|
|
} else {
|
2021-02-13 21:16:11 +00:00
|
|
|
return pager.New(config.Tool.Pager, c.Logger)
|
2021-01-16 13:13:57 +00:00
|
|
|
}
|
|
|
|
}
|