2021-04-14 18:14:01 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-04-11 15:22:06 +00:00
|
|
|
"strings"
|
2021-04-14 18:14:01 +00:00
|
|
|
|
|
|
|
"github.com/mickael-menu/zk/internal/adapter/editor"
|
|
|
|
"github.com/mickael-menu/zk/internal/adapter/fs"
|
|
|
|
"github.com/mickael-menu/zk/internal/adapter/fzf"
|
|
|
|
"github.com/mickael-menu/zk/internal/adapter/handlebars"
|
2021-04-18 14:37:54 +00:00
|
|
|
hbhelpers "github.com/mickael-menu/zk/internal/adapter/handlebars/helpers"
|
2021-04-14 18:14:01 +00:00
|
|
|
"github.com/mickael-menu/zk/internal/adapter/markdown"
|
|
|
|
"github.com/mickael-menu/zk/internal/adapter/sqlite"
|
|
|
|
"github.com/mickael-menu/zk/internal/adapter/term"
|
|
|
|
"github.com/mickael-menu/zk/internal/core"
|
|
|
|
"github.com/mickael-menu/zk/internal/util"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/errors"
|
|
|
|
osutil "github.com/mickael-menu/zk/internal/util/os"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/pager"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/paths"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/rand"
|
|
|
|
)
|
|
|
|
|
2021-04-17 09:28:38 +00:00
|
|
|
type Dirs struct {
|
|
|
|
NotebookDir string
|
|
|
|
WorkingDir string
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
type Container struct {
|
|
|
|
Version string
|
|
|
|
Config core.Config
|
|
|
|
Logger *util.ProxyLogger
|
2022-01-10 13:28:14 +00:00
|
|
|
Styler *core.ProxyStyler
|
2021-04-14 18:14:01 +00:00
|
|
|
Terminal *term.Terminal
|
|
|
|
FS *fs.FileStorage
|
2021-04-25 10:40:53 +00:00
|
|
|
TemplateLoader core.TemplateLoader
|
2021-04-14 18:14:01 +00:00
|
|
|
WorkingDir string
|
|
|
|
Notebooks *core.NotebookStore
|
|
|
|
currentNotebook *core.Notebook
|
|
|
|
currentNotebookErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContainer(version string) (*Container, error) {
|
|
|
|
wrap := errors.Wrapper("initialization")
|
|
|
|
|
|
|
|
term := term.New()
|
2022-01-10 13:28:14 +00:00
|
|
|
styler := core.NewProxyStyler(term)
|
2021-04-14 18:14:01 +00:00
|
|
|
logger := util.NewProxyLogger(util.NewStdLogger("zk: ", 0))
|
|
|
|
fs, err := fs.NewFileStorage("", logger)
|
|
|
|
config := core.NewDefaultConfig()
|
|
|
|
|
|
|
|
handlebars.Init(term.SupportsUTF8(), logger)
|
2021-04-25 10:40:53 +00:00
|
|
|
// Template loader used for embedded templates (e.g. default config, fzf
|
|
|
|
// line, etc.).
|
|
|
|
templateLoader := handlebars.NewLoader(handlebars.LoaderOpts{
|
|
|
|
LookupPaths: []string{},
|
|
|
|
Styler: styler,
|
|
|
|
})
|
|
|
|
templateLoader.RegisterHelper("style", hbhelpers.NewStyleHelper(styler, logger))
|
2021-04-14 18:14:01 +00:00
|
|
|
|
|
|
|
// Load global user config
|
|
|
|
configPath, err := locateGlobalConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
if configPath != "" {
|
2023-04-11 15:22:06 +00:00
|
|
|
config, err = core.OpenConfig(configPath, config, fs, true)
|
2021-04-14 18:14:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 15:22:06 +00:00
|
|
|
// Set the default notebook if not already set
|
|
|
|
// might be overrided if --notebook-dir flag is present
|
|
|
|
if osutil.GetOptEnv("ZK_NOTEBOOK_DIR").IsNull() && !config.Notebook.Dir.IsNull() {
|
|
|
|
// Expand in case there are environment variables on the path
|
|
|
|
notebookDir := os.Expand(config.Notebook.Dir.Unwrap(), os.Getenv)
|
|
|
|
if strings.HasPrefix(notebookDir, "~") {
|
|
|
|
dirname, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
notebookDir = filepath.Join(dirname, notebookDir[1:])
|
|
|
|
}
|
|
|
|
os.Setenv("ZK_NOTEBOOK_DIR", notebookDir)
|
|
|
|
}
|
|
|
|
|
2023-03-28 19:29:18 +00:00
|
|
|
// Set the default shell if not already set
|
|
|
|
if osutil.GetOptEnv("ZK_SHELL").IsNull() && !config.Tool.Shell.IsEmpty() {
|
|
|
|
os.Setenv("ZK_SHELL", config.Tool.Shell.Unwrap())
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
return &Container{
|
2022-01-10 13:28:14 +00:00
|
|
|
Version: version,
|
|
|
|
Config: config,
|
|
|
|
Logger: logger,
|
|
|
|
Styler: styler,
|
|
|
|
Terminal: term,
|
|
|
|
FS: fs,
|
|
|
|
TemplateLoader: templateLoader,
|
2021-04-14 18:14:01 +00:00
|
|
|
Notebooks: core.NewNotebookStore(config, core.NotebookStorePorts{
|
2021-04-25 10:40:53 +00:00
|
|
|
FS: fs,
|
|
|
|
TemplateLoader: templateLoader,
|
2021-04-14 18:14:01 +00:00
|
|
|
NotebookFactory: func(path string, config core.Config) (*core.Notebook, error) {
|
|
|
|
dbPath := filepath.Join(path, ".zk/notebook.db")
|
|
|
|
db, err := sqlite.Open(dbPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
notebook := core.NewNotebook(path, config, core.NotebookPorts{
|
2022-05-22 14:39:13 +00:00
|
|
|
NoteIndex: sqlite.NewNoteIndex(path, db, logger),
|
2022-01-10 13:28:14 +00:00
|
|
|
NoteContentParser: markdown.NewParser(
|
|
|
|
markdown.ParserOpts{
|
|
|
|
HashtagEnabled: config.Format.Markdown.Hashtags,
|
|
|
|
MultiWordTagEnabled: config.Format.Markdown.MultiwordTags,
|
|
|
|
ColontagEnabled: config.Format.Markdown.ColonTags,
|
|
|
|
},
|
|
|
|
logger,
|
|
|
|
),
|
2021-04-14 18:14:01 +00:00
|
|
|
TemplateLoaderFactory: func(language string) (core.TemplateLoader, error) {
|
2021-04-18 14:37:54 +00:00
|
|
|
loader := handlebars.NewLoader(handlebars.LoaderOpts{
|
2021-04-14 18:14:01 +00:00
|
|
|
LookupPaths: []string{
|
|
|
|
filepath.Join(globalConfigDir(), "templates"),
|
|
|
|
filepath.Join(path, ".zk/templates"),
|
|
|
|
},
|
|
|
|
Styler: styler,
|
2021-04-18 14:37:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
loader.RegisterHelper("style", hbhelpers.NewStyleHelper(styler, logger))
|
|
|
|
loader.RegisterHelper("slug", hbhelpers.NewSlugHelper(language, logger))
|
|
|
|
|
|
|
|
linkFormatter, err := core.NewLinkFormatter(config.Format.Markdown, loader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
loader.RegisterHelper("format-link", hbhelpers.NewLinkHelper(linkFormatter, logger))
|
|
|
|
|
|
|
|
return loader, nil
|
2021-04-14 18:14:01 +00:00
|
|
|
},
|
|
|
|
IDGeneratorFactory: func(opts core.IDOptions) func() string {
|
|
|
|
return rand.NewIDGenerator(opts)
|
|
|
|
},
|
|
|
|
FS: fs,
|
|
|
|
Logger: logger,
|
|
|
|
OSEnv: func() map[string]string {
|
|
|
|
return osutil.Env()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return notebook, nil
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// locateGlobalConfig looks for the global zk config file following the
|
|
|
|
// XDG Base Directory specification
|
|
|
|
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
func locateGlobalConfig() (string, error) {
|
2022-05-28 18:03:06 +00:00
|
|
|
if _, ok := os.LookupEnv("RUNNING_TESH"); ok {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
configPath := filepath.Join(globalConfigDir(), "config.toml")
|
|
|
|
exists, err := paths.Exists(configPath)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return "", err
|
|
|
|
case exists:
|
|
|
|
return configPath, nil
|
|
|
|
default:
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// globalConfigDir returns the parent directory of the global configuration file.
|
|
|
|
func globalConfigDir() string {
|
|
|
|
path, ok := os.LookupEnv("XDG_CONFIG_HOME")
|
|
|
|
if !ok {
|
|
|
|
home, ok := os.LookupEnv("HOME")
|
|
|
|
if !ok {
|
|
|
|
home = "~/"
|
|
|
|
}
|
|
|
|
path = filepath.Join(home, ".config")
|
|
|
|
}
|
|
|
|
return filepath.Join(path, "zk")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCurrentNotebook sets the first notebook found in the given search paths
|
|
|
|
// as the current default one.
|
2021-05-16 20:11:31 +00:00
|
|
|
func (c *Container) SetCurrentNotebook(searchDirs []Dirs) error {
|
2021-04-17 09:28:38 +00:00
|
|
|
if len(searchDirs) == 0 {
|
2021-05-16 20:11:31 +00:00
|
|
|
return nil
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 09:28:38 +00:00
|
|
|
for _, dirs := range searchDirs {
|
|
|
|
notebookDir := c.FS.Canonical(dirs.NotebookDir)
|
|
|
|
workingDir := c.FS.Canonical(dirs.WorkingDir)
|
|
|
|
|
|
|
|
c.currentNotebook, c.currentNotebookErr = c.Notebooks.Open(notebookDir)
|
2021-04-14 18:14:01 +00:00
|
|
|
if c.currentNotebookErr == nil {
|
2021-04-17 09:28:38 +00:00
|
|
|
c.setWorkingDir(workingDir)
|
2021-04-14 18:14:01 +00:00
|
|
|
c.Config = c.currentNotebook.Config
|
|
|
|
// FIXME: Is there something to do to support multiple notebooks here?
|
|
|
|
os.Setenv("ZK_NOTEBOOK_DIR", c.currentNotebook.Path)
|
2021-05-16 20:11:31 +00:00
|
|
|
}
|
|
|
|
// Report the error only if it's not the "notebook not found" one.
|
|
|
|
var errNotFound core.ErrNotebookNotFound
|
|
|
|
if !errors.As(c.currentNotebookErr, &errNotFound) {
|
|
|
|
return c.currentNotebookErr
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-16 20:11:31 +00:00
|
|
|
return nil
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 09:28:38 +00:00
|
|
|
// SetWorkingDir resets the current working directory.
|
|
|
|
func (c *Container) setWorkingDir(path string) {
|
|
|
|
path = c.FS.Canonical(path)
|
|
|
|
c.WorkingDir = path
|
|
|
|
c.FS.SetWorkingDir(path)
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
// CurrentNotebook returns the current default notebook.
|
|
|
|
func (c *Container) CurrentNotebook() (*core.Notebook, error) {
|
|
|
|
return c.currentNotebook, c.currentNotebookErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) NewNoteFilter(opts fzf.NoteFilterOpts) *fzf.NoteFilter {
|
2021-04-25 10:40:53 +00:00
|
|
|
opts.PreviewCmd = c.Config.Tool.FzfPreview
|
|
|
|
opts.LineTemplate = c.Config.Tool.FzfLine
|
2022-04-24 12:12:20 +00:00
|
|
|
opts.FzfOptions = c.Config.Tool.FzfOptions
|
|
|
|
opts.NewBinding = c.Config.Tool.FzfBindNew
|
2021-04-25 10:40:53 +00:00
|
|
|
return fzf.NewNoteFilter(opts, c.FS, c.Terminal, c.TemplateLoader)
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) NewNoteEditor(notebook *core.Notebook) (*editor.Editor, error) {
|
|
|
|
return editor.NewEditor(notebook.Config.Tool.Editor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, run func(out io.Writer) error) error {
|
|
|
|
pager, err := c.pager(noPager || c.Config.Tool.Pager.IsEmpty())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = run(pager)
|
|
|
|
pager.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) pager(noPager bool) (*pager.Pager, error) {
|
|
|
|
if noPager || !c.Terminal.IsInteractive() {
|
|
|
|
return pager.PassthroughPager, nil
|
|
|
|
} else {
|
|
|
|
return pager.New(c.Config.Tool.Pager, c.Logger)
|
|
|
|
}
|
|
|
|
}
|