zk/cmd/container.go

101 lines
2.5 KiB
Go
Raw Normal View History

2020-12-28 12:15:56 +00:00
package cmd
import (
"io"
"os"
2021-01-24 11:10:13 +00:00
"sync"
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"
"github.com/mickael-menu/zk/adapter/term"
"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"
"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
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.
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(".")
if c.zkErr == nil {
os.Setenv("ZK_PATH", c.zk.Path)
}
2021-01-24 11:10:13 +00:00
})
return c.zk, c.zkErr
}
func (c *Container) TemplateLoader(lang string) *handlebars.Loader {
2020-12-29 16:22:19 +00:00
if c.templateLoader == nil {
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()
}
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)
return fzf.NewNoteFinder(opts, notes, c.Terminal)
2021-01-23 12:29:14 +00:00
}
// Database returns the DB instance for the given notebook, after executing any
2021-01-02 11:29:21 +00:00
// pending migration.
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
}
// 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)
if err != nil {
return err
}
err = run(pager)
pager.Close()
return err
}
func (c *Container) pager(noPager bool, config zk.Config) (*pager.Pager, error) {
if noPager || !c.Terminal.IsInteractive() {
return pager.PassthroughPager, nil
} else {
2021-02-13 21:16:11 +00:00
return pager.New(config.Tool.Pager, c.Logger)
}
}