2023-03-12 07:13:53 +03:00
|
|
|
// Package main is a command-lint tool `zs` called Zen Static for generating static websites
|
2014-12-05 19:59:56 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-03-12 07:13:53 +03:00
|
|
|
"context"
|
2023-03-18 10:40:46 +03:00
|
|
|
"errors"
|
2014-12-05 19:59:56 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-09-02 18:00:14 +03:00
|
|
|
"os/exec"
|
2023-03-12 07:13:53 +03:00
|
|
|
"os/signal"
|
2014-12-05 19:59:56 +03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2023-03-12 07:13:53 +03:00
|
|
|
"syscall"
|
2015-08-29 19:46:05 +03:00
|
|
|
"text/template"
|
2014-12-05 19:59:56 +03:00
|
|
|
"time"
|
|
|
|
|
2023-03-18 10:23:10 +03:00
|
|
|
embed "github.com/13rac1/goldmark-embed"
|
|
|
|
d2 "github.com/FurqanSoftware/goldmark-d2"
|
|
|
|
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
|
2023-03-12 14:52:14 +03:00
|
|
|
ignore "github.com/sabhiram/go-gitignore"
|
2023-03-12 09:34:38 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2023-03-21 17:17:14 +03:00
|
|
|
"github.com/spf13/viper"
|
2023-03-18 10:23:10 +03:00
|
|
|
fences "github.com/stefanfritsch/goldmark-fences"
|
2023-03-16 10:47:21 +03:00
|
|
|
"github.com/yuin/goldmark"
|
2023-03-18 10:23:10 +03:00
|
|
|
highlighting "github.com/yuin/goldmark-highlighting/v2"
|
2023-03-16 10:47:21 +03:00
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
|
"github.com/yuin/goldmark/parser"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
2023-03-18 10:23:10 +03:00
|
|
|
"go.abhg.dev/goldmark/anchor"
|
|
|
|
"go.abhg.dev/goldmark/wikilink"
|
2023-03-12 07:13:53 +03:00
|
|
|
"go.mills.io/static"
|
2023-03-12 09:34:38 +03:00
|
|
|
"golang.org/x/sync/errgroup"
|
2015-09-02 20:35:26 +03:00
|
|
|
"gopkg.in/yaml.v2"
|
2014-12-05 19:59:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-03-12 07:13:53 +03:00
|
|
|
// ZSDIR is the default directory for storing layouts and extensions
|
|
|
|
ZSDIR = ".zs"
|
|
|
|
|
2023-03-21 17:17:14 +03:00
|
|
|
// ZSCONFIG is the default configuration name (without extension)
|
|
|
|
ZSCONFIG = "config"
|
|
|
|
|
2023-03-12 14:52:14 +03:00
|
|
|
// ZSIGNORE is the default ignore file
|
|
|
|
ZSIGNORE = ".zsignore"
|
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
// PUBDIR is the default directory for publishing final built content
|
2014-12-05 19:59:56 +03:00
|
|
|
PUBDIR = ".pub"
|
2023-03-18 04:59:16 +03:00
|
|
|
|
|
|
|
// DefaultIgnore is the default set of ignore patterns if no .zsignore
|
|
|
|
DefaultIgnore = `*~
|
|
|
|
*.bak
|
|
|
|
|
|
|
|
COPYING
|
|
|
|
Dockerfile
|
|
|
|
LICENSE
|
|
|
|
Makefile
|
|
|
|
README.md`
|
2014-12-05 19:59:56 +03:00
|
|
|
)
|
|
|
|
|
2023-03-12 14:52:14 +03:00
|
|
|
// Ignore holds a set of patterns to ignore from parsing a .zsignore file
|
|
|
|
var Ignore *ignore.GitIgnore
|
|
|
|
|
2023-03-26 05:23:23 +03:00
|
|
|
// Parser holds a configured global instance of the goldmark markdown parser
|
|
|
|
var Parser goldmark.Markdown
|
|
|
|
|
2023-03-18 06:06:33 +03:00
|
|
|
var (
|
2023-03-26 05:23:23 +03:00
|
|
|
configFile string
|
|
|
|
enabledExtensions []string
|
2023-03-18 06:06:33 +03:00
|
|
|
)
|
|
|
|
|
2023-03-26 05:23:23 +03:00
|
|
|
// Extensions is a mapping of name to extension and the default set of extensions enabled
|
|
|
|
// which can be overridden with -e/--extension or the extensions key
|
|
|
|
// in ia config file such as .zs/config.yml
|
|
|
|
var Extensions = map[string]goldmark.Extender{
|
|
|
|
"table": extension.Table,
|
|
|
|
"strikethrough": extension.Strikethrough,
|
|
|
|
"linkify": extension.Linkify,
|
|
|
|
"tasklist": extension.TaskList,
|
|
|
|
"definitionlist": extension.DefinitionList,
|
|
|
|
"footnote": extension.Footnote,
|
|
|
|
"typography": extension.Typographer,
|
|
|
|
"cjk": extension.CJK,
|
|
|
|
"highlighting": highlighting.NewHighlighting(
|
|
|
|
highlighting.WithStyle("github"),
|
|
|
|
highlighting.WithFormatOptions(
|
|
|
|
chromahtml.WithLineNumbers(true),
|
|
|
|
),
|
|
|
|
),
|
2023-03-26 17:29:58 +03:00
|
|
|
"anchor": &anchor.Extender{Texter: anchor.Text(" ")},
|
2023-03-26 05:23:23 +03:00
|
|
|
"d2": &d2.Extender{},
|
|
|
|
"embed": embed.New(),
|
|
|
|
"fences": &fences.Extender{},
|
|
|
|
"wikilink": &wikilink.Extender{},
|
|
|
|
}
|
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
// Vars holds a map of global variables
|
2015-08-29 18:47:16 +03:00
|
|
|
type Vars map[string]string
|
2015-09-02 18:00:14 +03:00
|
|
|
|
2023-03-26 05:23:23 +03:00
|
|
|
// MapKeys returns a slice of keys from a map
|
|
|
|
func MapKeys[K comparable, V any](m map[K]V) []K {
|
|
|
|
r := make([]K, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
r = append(r, k)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
// NewTicker is a function that wraps a time.Ticker and ticks immediately instead of waiting for the first interval
|
|
|
|
func NewTicker(d time.Duration) *time.Ticker {
|
|
|
|
ticker := time.NewTicker(d)
|
|
|
|
oc := ticker.C
|
|
|
|
nc := make(chan time.Time, 1)
|
|
|
|
go func() {
|
|
|
|
nc <- time.Now()
|
|
|
|
for tm := range oc {
|
|
|
|
nc <- tm
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ticker.C = nc
|
|
|
|
return ticker
|
|
|
|
}
|
|
|
|
|
2023-03-12 09:34:38 +03:00
|
|
|
// RootCmd is the base command when called without any subcommands
|
|
|
|
var RootCmd = &cobra.Command{
|
|
|
|
Use: "zs",
|
|
|
|
Version: FullVersion(),
|
|
|
|
Short: "zs the zen static site generator",
|
|
|
|
Long: `zs is an extremely minimal static site generator written in Go.
|
|
|
|
|
|
|
|
- Keep your texts in markdown, or HTML format right in the main directory of your blog/site.
|
|
|
|
- Keep all service files (extensions, layout pages, deployment scripts etc) in the .zs subdirectory.
|
|
|
|
- Define variables in the header of the content files using YAML front matter:
|
|
|
|
- Use placeholders for variables and plugins in your markdown or html files, e.g. {{ title }} or {{ command arg1 arg2 }}.
|
|
|
|
- Write extensions in any language you like and put them into the .zs sub-directory.
|
|
|
|
- Everything the extensions prints to stdout becomes the value of the placeholder.
|
|
|
|
`,
|
2023-03-12 10:43:50 +03:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
debug, err := cmd.Flags().GetBool("debug")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting debug flag: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if debug {
|
2023-03-12 09:34:38 +03:00
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
}
|
2023-03-12 10:43:50 +03:00
|
|
|
|
2023-03-26 05:23:23 +03:00
|
|
|
var extensions []goldmark.Extender
|
2023-03-26 05:57:17 +03:00
|
|
|
for _, name := range viper.GetStringSlice("extensions") {
|
2023-03-26 05:23:23 +03:00
|
|
|
if extender, valid := Extensions[name]; valid {
|
|
|
|
extensions = append(extensions, extender)
|
|
|
|
} else {
|
|
|
|
log.Warnf("invalid extension: %s", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Parser = goldmark.New(
|
|
|
|
goldmark.WithExtensions(extensions...),
|
|
|
|
goldmark.WithParserOptions(
|
|
|
|
parser.WithAttribute(),
|
|
|
|
parser.WithAutoHeadingID(),
|
|
|
|
),
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithHardWraps(),
|
|
|
|
html.WithXHTML(),
|
|
|
|
html.WithUnsafe(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-03-12 10:43:50 +03:00
|
|
|
return nil
|
2023-03-12 09:34:38 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildCmd is the build sub-command that performs whole builds or single builds
|
|
|
|
var BuildCmd = &cobra.Command{
|
|
|
|
Use: "build [<file>]",
|
|
|
|
Short: "Builds the whole site or a single file",
|
|
|
|
Long: `The build command builds the entire site or a single file if specified.`,
|
|
|
|
Args: cobra.RangeArgs(0, 1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := buildAll(ctx, false); err != nil {
|
|
|
|
return fmt.Errorf("error building site: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := build(args[0], os.Stdout, globals()); err != nil {
|
|
|
|
return fmt.Errorf("error building file %q", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-01 03:47:02 +03:00
|
|
|
// GenerateCmd is the generate sub-command that builds partial fragments
|
|
|
|
var GenerateCmd = &cobra.Command{
|
|
|
|
Use: "generate",
|
|
|
|
Aliases: []string{"gen"},
|
|
|
|
Short: "Generates partial fragments",
|
|
|
|
Long: `The generate command parses and renders partial fragments from stdin and writes to stdout`,
|
|
|
|
Args: cobra.RangeArgs(0, 1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := generate(os.Stdin, os.Stdout, globals()); err != nil {
|
|
|
|
return fmt.Errorf("error generating fragment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-12 09:34:38 +03:00
|
|
|
// ServeCmd is the serve sub-command that performs whole builds or single builds
|
|
|
|
var ServeCmd = &cobra.Command{
|
|
|
|
Use: "serve [flags]",
|
|
|
|
Short: "Serves the site and rebuilds automatically",
|
|
|
|
Long: `The serve command serves the site and watches for rebuilds automatically`,
|
|
|
|
Args: cobra.RangeArgs(0, 1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
var wg errgroup.Group
|
|
|
|
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
bind, err := cmd.Flags().GetString("bind")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting bind flag: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := cmd.Flags().GetString("root")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting root flag: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Go(func() error {
|
|
|
|
if err := serve(ctx, bind, root); err != nil {
|
|
|
|
return fmt.Errorf("error serving site: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := wg.Wait(); err != nil {
|
|
|
|
return fmt.Errorf("error running serve: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// VarCmd is the var sub-command that performs whole builds or single builds
|
|
|
|
var VarCmd = &cobra.Command{
|
|
|
|
Use: "var <file> [<var>...]",
|
|
|
|
Aliases: []string{"vars"},
|
|
|
|
Short: "Display variables for the specified file",
|
|
|
|
Long: `The var command extracts and display sll teh variables defined in a file.
|
|
|
|
If the name of variables (optional) are passed as additional arguments, only those variables
|
|
|
|
are display instead of all variables (the default behavior).`,
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-03-30 15:22:21 +03:00
|
|
|
s := ""
|
2023-03-12 09:34:38 +03:00
|
|
|
|
2023-03-30 15:11:16 +03:00
|
|
|
vars, _, err := getVars(args[0], globals())
|
2023-03-12 09:34:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting variables from %s: %w", args[0], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
for _, a := range args[1:] {
|
|
|
|
s = s + vars[a] + "\n"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := range vars {
|
|
|
|
s = s + k + ":" + v + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(strings.TrimSpace(s))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchCmd is the watch sub-command that performs whole builds or single builds
|
|
|
|
var WatchCmd = &cobra.Command{
|
|
|
|
Use: "watch",
|
|
|
|
Short: "Watches for file changes and rebuilds modified files",
|
|
|
|
Long: `The watch command watches for any changes to files and rebuilds them automatically`,
|
|
|
|
Args: cobra.RangeArgs(0, 1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
var wg errgroup.Group
|
|
|
|
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
wg.Go(func() error {
|
|
|
|
if err := buildAll(ctx, true); err != nil {
|
|
|
|
return fmt.Errorf("error watching for changes: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := wg.Wait(); err != nil {
|
|
|
|
return fmt.Errorf("error running watch: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
// renameExt renames extension (if any) from oldext to newext
|
|
|
|
// If oldext is an empty string - extension is extracted automatically.
|
|
|
|
// If path has no extension - new extension is appended
|
|
|
|
func renameExt(path, oldext, newext string) string {
|
|
|
|
if oldext == "" {
|
|
|
|
oldext = filepath.Ext(path)
|
|
|
|
}
|
|
|
|
if oldext == "" || strings.HasSuffix(path, oldext) {
|
|
|
|
return strings.TrimSuffix(path, oldext) + newext
|
2015-09-02 18:00:14 +03:00
|
|
|
}
|
2021-10-28 19:10:15 +03:00
|
|
|
return path
|
2015-09-02 18:00:14 +03:00
|
|
|
}
|
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
// globals returns list of global OS environment variables that start
|
|
|
|
// with ZS_ prefix as Vars, so the values can be used inside templates
|
2015-09-02 18:00:14 +03:00
|
|
|
func globals() Vars {
|
2023-03-21 17:17:14 +03:00
|
|
|
vars := Vars{
|
|
|
|
"title": viper.GetString("title"),
|
|
|
|
"description": viper.GetString("description"),
|
|
|
|
"keywords": viper.GetString("keywords"),
|
|
|
|
}
|
2023-03-18 06:06:33 +03:00
|
|
|
|
2023-03-26 05:57:17 +03:00
|
|
|
if viper.GetBool("production") {
|
2023-03-18 06:06:33 +03:00
|
|
|
vars["production"] = "1"
|
|
|
|
}
|
|
|
|
|
2023-03-30 15:11:16 +03:00
|
|
|
// Variables from the environment in the form of ZS_<name>=<value>
|
2015-09-02 18:00:14 +03:00
|
|
|
for _, e := range os.Environ() {
|
|
|
|
pair := strings.Split(e, "=")
|
|
|
|
if strings.HasPrefix(pair[0], "ZS_") {
|
|
|
|
vars[strings.ToLower(pair[0][3:])] = pair[1]
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 15:11:16 +03:00
|
|
|
|
|
|
|
// Variables from the command-line -v/--vars (or env var as $ZS_VARS) or configuration
|
|
|
|
// Note: These will override the previous variables if names clash.
|
|
|
|
for _, e := range viper.GetStringSlice("vars") {
|
|
|
|
pair := strings.Split(e, "=")
|
|
|
|
vars[pair[0]] = pair[1]
|
|
|
|
}
|
|
|
|
|
2015-09-02 18:00:14 +03:00
|
|
|
return vars
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
// run executes a command or a script. Vars define the command environment,
|
2023-03-21 17:17:14 +03:00
|
|
|
// each zs var is converted into OS environment variable with ZS_ prefix
|
2015-09-02 20:05:09 +03:00
|
|
|
// prepended. Additional variable $ZS contains path to the zs binary. Command
|
|
|
|
// stderr is printed to zs stderr, command output is returned as a string.
|
|
|
|
func run(vars Vars, cmd string, args ...string) (string, error) {
|
2021-09-18 02:32:14 +03:00
|
|
|
// First check if partial exists (.html)
|
2015-09-02 20:05:09 +03:00
|
|
|
if b, err := ioutil.ReadFile(filepath.Join(ZSDIR, cmd+".html")); err == nil {
|
|
|
|
return string(b), nil
|
2015-09-02 18:00:14 +03:00
|
|
|
}
|
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
var errbuf, outbuf bytes.Buffer
|
2015-09-02 18:00:14 +03:00
|
|
|
c := exec.Command(cmd, args...)
|
2015-09-02 20:05:09 +03:00
|
|
|
env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
|
|
|
|
env = append(env, os.Environ()...)
|
|
|
|
for k, v := range vars {
|
2023-03-20 01:38:05 +03:00
|
|
|
if k != "content" {
|
|
|
|
env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
|
|
|
|
}
|
2015-09-02 20:05:09 +03:00
|
|
|
}
|
|
|
|
c.Env = env
|
|
|
|
c.Stdout = &outbuf
|
2015-09-02 18:00:14 +03:00
|
|
|
c.Stderr = &errbuf
|
|
|
|
|
2023-03-18 10:23:10 +03:00
|
|
|
if err := c.Run(); err != nil {
|
2023-03-12 10:43:50 +03:00
|
|
|
log.Errorf("error running command: %s", cmd)
|
|
|
|
log.Error(errbuf.String())
|
2015-09-02 20:05:09 +03:00
|
|
|
return "", err
|
2015-09-02 18:00:14 +03:00
|
|
|
}
|
2023-03-18 10:23:10 +03:00
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
return string(outbuf.Bytes()), nil
|
2015-09-02 18:00:14 +03:00
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
// getVars returns list of variables defined in a text file and actual file
|
|
|
|
// content following the variables declaration. Header is separated from
|
|
|
|
// content by an empty line. Header can be either YAML or JSON.
|
|
|
|
// If no empty newline is found - file is treated as content-only.
|
|
|
|
func getVars(path string, globals Vars) (Vars, string, error) {
|
2023-03-12 14:52:14 +03:00
|
|
|
if Ignore.MatchesPath(path) {
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
2015-08-29 18:47:16 +03:00
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
s := string(b)
|
2015-09-02 20:05:09 +03:00
|
|
|
|
2015-09-02 20:43:31 +03:00
|
|
|
// Pick some default values for content-dependent variables
|
2015-09-02 20:05:09 +03:00
|
|
|
v := Vars{}
|
2015-09-02 20:43:31 +03:00
|
|
|
title := strings.Replace(strings.Replace(path, "_", " ", -1), "-", " ", -1)
|
|
|
|
v["title"] = strings.ToTitle(title)
|
|
|
|
v["description"] = ""
|
2015-09-04 15:49:56 +03:00
|
|
|
v["file"] = path
|
|
|
|
v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html"
|
|
|
|
v["output"] = filepath.Join(PUBDIR, v["url"])
|
2015-09-02 20:43:31 +03:00
|
|
|
|
2015-09-04 15:49:56 +03:00
|
|
|
// Override default values with globals
|
2015-08-30 17:01:05 +03:00
|
|
|
for name, value := range globals {
|
|
|
|
v[name] = value
|
2014-12-05 21:26:15 +03:00
|
|
|
}
|
2015-09-02 20:05:09 +03:00
|
|
|
|
2015-09-04 15:49:56 +03:00
|
|
|
// Add layout if none is specified
|
|
|
|
if _, ok := v["layout"]; !ok {
|
2021-09-18 02:32:14 +03:00
|
|
|
v["layout"] = "layout.html"
|
2015-08-30 00:43:59 +03:00
|
|
|
}
|
|
|
|
|
2015-09-02 20:35:26 +03:00
|
|
|
delim := "\n---\n"
|
2023-03-12 07:13:53 +03:00
|
|
|
sep := strings.Index(s, delim)
|
|
|
|
if sep == -1 {
|
2015-08-29 20:54:55 +03:00
|
|
|
return v, s, nil
|
2023-03-12 07:13:53 +03:00
|
|
|
}
|
2015-09-02 20:35:26 +03:00
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
header := s[:sep]
|
|
|
|
body := s[sep+len(delim):]
|
|
|
|
|
|
|
|
vars := Vars{}
|
|
|
|
if err := yaml.Unmarshal([]byte(header), &vars); err != nil {
|
2023-03-12 10:43:50 +03:00
|
|
|
log.WithError(err).Warn("failed to parse header")
|
2023-03-12 07:13:53 +03:00
|
|
|
return v, s, nil
|
|
|
|
}
|
|
|
|
// Override default values + globals with the ones defines in the file
|
|
|
|
for key, value := range vars {
|
|
|
|
v[key] = value
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(v["url"], "./") {
|
|
|
|
v["url"] = v["url"][2:]
|
2014-12-05 20:09:10 +03:00
|
|
|
}
|
2023-03-12 07:13:53 +03:00
|
|
|
return v, body, nil
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
// Render expanding zs plugins and variables
|
2015-09-02 18:00:14 +03:00
|
|
|
func render(s string, vars Vars) (string, error) {
|
2023-08-19 20:41:02 +03:00
|
|
|
openingDelimiter := viper.GetString("opening-delim")
|
|
|
|
closingDelimiter := viper.GetString("closing-delim")
|
2015-09-02 20:05:09 +03:00
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
out := &bytes.Buffer{}
|
2015-09-02 20:05:09 +03:00
|
|
|
for {
|
2023-03-12 07:13:53 +03:00
|
|
|
from := strings.Index(s, openingDelimiter)
|
|
|
|
if from == -1 {
|
2015-09-02 20:05:09 +03:00
|
|
|
out.WriteString(s)
|
|
|
|
return out.String(), nil
|
2023-03-12 07:13:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
to := strings.Index(s, closingDelimiter)
|
|
|
|
if to == -1 {
|
2023-03-12 10:43:50 +03:00
|
|
|
return "", fmt.Errorf("closing delimiter not found")
|
2023-03-12 07:13:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
out.WriteString(s[:from])
|
|
|
|
cmd := s[from+len(openingDelimiter) : to]
|
|
|
|
s = s[to+len(closingDelimiter):]
|
2023-03-12 10:43:50 +03:00
|
|
|
m := strings.Fields(strings.TrimSpace(cmd))
|
2023-03-12 07:13:53 +03:00
|
|
|
if len(m) == 1 {
|
|
|
|
if v, ok := vars[m[0]]; ok {
|
|
|
|
out.WriteString(v)
|
|
|
|
continue
|
2015-09-02 20:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-12 10:43:50 +03:00
|
|
|
if _, err := exec.LookPath(m[0]); err == nil {
|
|
|
|
if res, err := run(vars, m[0], m[1:]...); err == nil {
|
|
|
|
out.WriteString(res)
|
|
|
|
} else {
|
|
|
|
log.WithError(err).Warnf("error running command: %s", m[0])
|
|
|
|
}
|
2023-03-30 14:15:08 +03:00
|
|
|
} else {
|
|
|
|
if !viper.GetBool("production") {
|
|
|
|
out.WriteString(fmt.Sprintf("%s: plugin or variable not found", m[0]))
|
|
|
|
}
|
2023-03-12 07:13:53 +03:00
|
|
|
}
|
2015-08-29 19:46:05 +03:00
|
|
|
}
|
2022-08-14 03:52:15 +03:00
|
|
|
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Renders markdown with the given layout into html expanding all the macros
|
2015-09-02 18:00:14 +03:00
|
|
|
func buildMarkdown(path string, w io.Writer, vars Vars) error {
|
2015-09-02 20:05:09 +03:00
|
|
|
v, body, err := getVars(path, vars)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-16 10:47:21 +03:00
|
|
|
|
2023-03-29 16:37:20 +03:00
|
|
|
source, err := render(body, v)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-29 16:37:20 +03:00
|
|
|
v["source"] = source
|
2023-03-16 10:47:21 +03:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2023-03-29 16:37:20 +03:00
|
|
|
if err := Parser.Convert([]byte(source), buf); err != nil {
|
2023-03-16 10:47:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
v["content"] = buf.String()
|
|
|
|
|
2015-08-30 15:20:35 +03:00
|
|
|
if w == nil {
|
|
|
|
out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
w = out
|
|
|
|
}
|
2023-03-16 10:47:21 +03:00
|
|
|
|
2021-09-18 02:32:14 +03:00
|
|
|
return buildHTML(filepath.Join(ZSDIR, v["layout"]), w, v)
|
2015-08-29 16:07:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Renders text file expanding all variable macros inside it
|
2015-09-02 18:00:14 +03:00
|
|
|
func buildHTML(path string, w io.Writer, vars Vars) error {
|
2015-09-02 20:05:09 +03:00
|
|
|
v, body, err := getVars(path, vars)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-02 20:05:09 +03:00
|
|
|
if body, err = render(body, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpl, err := template.New("").Delims("<%", "%>").Parse(body)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-30 15:22:00 +03:00
|
|
|
if w == nil {
|
|
|
|
f, err := os.Create(filepath.Join(PUBDIR, path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
w = f
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
2015-09-02 20:05:09 +03:00
|
|
|
return tmpl.Execute(w, vars)
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
|
2015-08-30 15:20:35 +03:00
|
|
|
// Copies file as is from path to writer
|
|
|
|
func buildRaw(path string, w io.Writer) error {
|
2023-04-01 14:18:28 +03:00
|
|
|
r, err := os.Open(path)
|
2015-08-30 15:20:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-08-29 20:54:55 +03:00
|
|
|
}
|
2023-04-01 14:18:28 +03:00
|
|
|
defer r.Close()
|
|
|
|
|
2015-08-30 15:20:35 +03:00
|
|
|
if w == nil {
|
2023-04-01 14:18:28 +03:00
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := filepath.Join(PUBDIR, path)
|
|
|
|
|
|
|
|
out, err := os.Create(fn)
|
2023-03-12 07:13:53 +03:00
|
|
|
if err != nil {
|
2015-08-30 15:20:35 +03:00
|
|
|
return err
|
2015-08-29 19:46:05 +03:00
|
|
|
}
|
2023-03-12 07:13:53 +03:00
|
|
|
defer out.Close()
|
2023-04-01 14:18:28 +03:00
|
|
|
|
|
|
|
if err := os.Chmod(fn, stat.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
w = out
|
2015-08-29 19:46:05 +03:00
|
|
|
}
|
2023-04-01 14:18:28 +03:00
|
|
|
|
|
|
|
if _, err := io.Copy(w, r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-08-30 00:43:59 +03:00
|
|
|
}
|
|
|
|
|
2015-09-02 18:00:14 +03:00
|
|
|
func build(path string, w io.Writer, vars Vars) error {
|
2023-03-12 14:52:14 +03:00
|
|
|
if Ignore.MatchesPath(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-30 15:20:35 +03:00
|
|
|
ext := filepath.Ext(path)
|
|
|
|
if ext == ".md" || ext == ".mkd" {
|
2015-09-02 18:00:14 +03:00
|
|
|
return buildMarkdown(path, w, vars)
|
2015-08-30 15:20:35 +03:00
|
|
|
} else if ext == ".html" || ext == ".xml" {
|
2015-09-02 18:00:14 +03:00
|
|
|
return buildHTML(path, w, vars)
|
2015-08-30 15:20:35 +03:00
|
|
|
} else {
|
|
|
|
return buildRaw(path, w)
|
2015-08-29 19:46:05 +03:00
|
|
|
}
|
2015-08-29 20:54:55 +03:00
|
|
|
}
|
|
|
|
|
2023-03-12 09:34:38 +03:00
|
|
|
func buildAll(ctx context.Context, watch bool) error {
|
2023-03-12 07:13:53 +03:00
|
|
|
ticker := NewTicker(time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2015-08-29 20:54:55 +03:00
|
|
|
lastModified := time.Unix(0, 0)
|
|
|
|
modified := false
|
|
|
|
|
|
|
|
vars := globals()
|
2014-12-05 19:59:56 +03:00
|
|
|
for {
|
2023-03-12 07:13:53 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2023-03-12 09:34:38 +03:00
|
|
|
return ctx.Err()
|
2023-03-12 07:13:53 +03:00
|
|
|
case <-ticker.C:
|
|
|
|
os.Mkdir(PUBDIR, 0755)
|
|
|
|
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
2023-03-18 04:59:16 +03:00
|
|
|
// rebuild if changes to .zs/ or .zsignore
|
|
|
|
if (filepath.Base(path) == ZSIGNORE || filepath.Dir(path) == ZSDIR) && info.ModTime().After(lastModified) {
|
2023-03-18 10:40:46 +03:00
|
|
|
if filepath.Base(path) == ZSIGNORE {
|
|
|
|
Ignore = ParseIgnoreFile(path)
|
|
|
|
}
|
2023-03-21 17:17:14 +03:00
|
|
|
// reset lastModified to 0 so everything rebuilds
|
2023-03-18 04:59:16 +03:00
|
|
|
lastModified = time.Unix(0, 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-12 14:52:14 +03:00
|
|
|
// ignore hidden files and directories and ignored patterns
|
|
|
|
if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") || Ignore.MatchesPath(path) {
|
2023-03-12 07:13:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-12 14:52:14 +03:00
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
// inform user about fs walk errors, but continue iteration
|
|
|
|
if err != nil {
|
2023-03-12 09:34:38 +03:00
|
|
|
log.WithError(err).Warn("error walking directory")
|
2023-03-12 07:13:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
if info.IsDir() {
|
|
|
|
os.Mkdir(filepath.Join(PUBDIR, path), 0755)
|
|
|
|
return nil
|
|
|
|
} else if info.ModTime().After(lastModified) {
|
|
|
|
if !modified {
|
|
|
|
// First file in this build cycle is about to be modified
|
2023-03-12 10:43:50 +03:00
|
|
|
if _, err := exec.LookPath("prehook"); err == nil {
|
|
|
|
if _, err := run(vars, "prehook"); err != nil {
|
|
|
|
log.WithError(err).Warn("error running prehook")
|
|
|
|
}
|
|
|
|
modified = true
|
2023-03-12 09:34:38 +03:00
|
|
|
}
|
2023-03-12 07:13:53 +03:00
|
|
|
}
|
2023-03-12 09:34:38 +03:00
|
|
|
log.Debugf("build: %s", path)
|
2023-03-12 07:13:53 +03:00
|
|
|
return build(path, nil, vars)
|
2014-12-05 22:03:33 +03:00
|
|
|
}
|
2023-03-12 07:13:53 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if modified {
|
|
|
|
// At least one file in this build cycle has been modified
|
2023-03-12 10:43:50 +03:00
|
|
|
if _, err := exec.LookPath("posthook"); err == nil {
|
|
|
|
if _, err := run(vars, "posthook"); err != nil {
|
|
|
|
log.WithError(err).Warn("error running posthook")
|
|
|
|
}
|
|
|
|
modified = false
|
2023-03-12 09:34:38 +03:00
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
2023-03-12 07:13:53 +03:00
|
|
|
if !watch {
|
2023-03-12 09:34:38 +03:00
|
|
|
return nil
|
2023-03-12 07:13:53 +03:00
|
|
|
}
|
|
|
|
lastModified = time.Now()
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 03:47:02 +03:00
|
|
|
// gen generates partial fragments
|
|
|
|
func generate(r io.Reader, w io.Writer, v Vars) error {
|
|
|
|
data, err := io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
body := string(data)
|
|
|
|
|
|
|
|
source, err := render(body, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Parser.Convert([]byte(source), w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-12 07:13:53 +03:00
|
|
|
// serve runs a static web server and builds and continuously watches for changes to rebuild
|
2023-03-12 07:24:34 +03:00
|
|
|
func serve(ctx context.Context, bind, root string) error {
|
|
|
|
os.Mkdir(root, 0755)
|
2023-03-12 07:13:53 +03:00
|
|
|
|
|
|
|
svr, err := static.NewServer(
|
|
|
|
static.WithBind(bind),
|
2023-03-12 07:24:34 +03:00
|
|
|
static.WithRoot(root),
|
2023-04-01 18:02:08 +03:00
|
|
|
static.WithCGI(true),
|
|
|
|
static.WithDir(true),
|
2023-03-12 07:13:53 +03:00
|
|
|
static.WithSPA(true),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go svr.Run(ctx)
|
|
|
|
go buildAll(ctx, true)
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-12 14:23:22 +03:00
|
|
|
func ensureFirstPath(p string) {
|
|
|
|
paths := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
|
|
|
|
if len(paths) > 0 && paths[0] != p {
|
|
|
|
paths = append([]string{p}, paths...)
|
|
|
|
os.Setenv("PATH", strings.Join(paths, string(os.PathListSeparator)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
func init() {
|
2023-03-21 17:17:14 +03:00
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
RootCmd.PersistentFlags().BoolP("debug", "D", false, "enable debug logging $($ZS_DEBUG)")
|
2023-08-19 20:41:02 +03:00
|
|
|
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "C", "", "config file (default: .zs/config.yml)")
|
2023-03-30 15:11:16 +03:00
|
|
|
|
2023-03-26 05:57:17 +03:00
|
|
|
RootCmd.PersistentFlags().StringSliceP("extensions", "e", MapKeys(Extensions), "override and enable specific extensions")
|
|
|
|
RootCmd.PersistentFlags().BoolP("production", "p", false, "enable production mode ($ZS_PRODUCTION)")
|
|
|
|
RootCmd.PersistentFlags().StringP("title", "t", "", "site title ($ZS_TITLE)")
|
|
|
|
RootCmd.PersistentFlags().StringP("description", "d", "", "site description ($ZS_DESCRIPTION)")
|
|
|
|
RootCmd.PersistentFlags().StringP("keywords", "k", "", "site keywords ($ZS_KEYWORDS)")
|
2023-03-30 15:11:16 +03:00
|
|
|
RootCmd.PersistentFlags().StringSliceP("vars", "v", nil, "additional variables")
|
2023-03-21 17:17:14 +03:00
|
|
|
|
2023-08-19 20:41:02 +03:00
|
|
|
RootCmd.PersistentFlags().StringP("opening-delim", "o", "{{", "opening delimiter for plugins")
|
|
|
|
RootCmd.PersistentFlags().StringP("closing-delim", "c", "{{", "closing delimiter for plugins")
|
|
|
|
|
2023-03-21 17:17:14 +03:00
|
|
|
viper.BindPFlag("debug", RootCmd.PersistentFlags().Lookup("debug"))
|
|
|
|
viper.SetDefault("debug", false)
|
|
|
|
|
2023-03-26 05:57:17 +03:00
|
|
|
viper.BindPFlag("extensions", RootCmd.PersistentFlags().Lookup("extensions"))
|
|
|
|
viper.SetDefault("extensions", MapKeys(Extensions))
|
|
|
|
|
2023-03-21 17:17:14 +03:00
|
|
|
viper.BindPFlag("production", RootCmd.PersistentFlags().Lookup("production"))
|
|
|
|
viper.SetDefault("production", false)
|
|
|
|
|
|
|
|
viper.BindPFlag("title", RootCmd.PersistentFlags().Lookup("title"))
|
|
|
|
viper.SetDefault("title", "")
|
|
|
|
|
|
|
|
viper.BindPFlag("description", RootCmd.PersistentFlags().Lookup("description"))
|
|
|
|
viper.SetDefault("description", "")
|
|
|
|
|
|
|
|
viper.BindPFlag("keywords", RootCmd.PersistentFlags().Lookup("keywords"))
|
|
|
|
viper.SetDefault("keywords", "")
|
2023-03-12 09:34:38 +03:00
|
|
|
|
2023-03-30 15:11:16 +03:00
|
|
|
viper.BindPFlag("vars", RootCmd.PersistentFlags().Lookup("vars"))
|
|
|
|
viper.SetDefault("vars", "")
|
|
|
|
|
2023-08-19 20:41:02 +03:00
|
|
|
viper.BindPFlag("opening-delim", RootCmd.PersistentFlags().Lookup("opening-delim"))
|
|
|
|
viper.SetDefault("opening-delim", "{{")
|
|
|
|
viper.BindPFlag("closing-delim", RootCmd.PersistentFlags().Lookup("closing-delim"))
|
|
|
|
viper.SetDefault("closing-delim", "}}")
|
|
|
|
|
2023-03-18 06:06:33 +03:00
|
|
|
ServeCmd.Flags().StringP("bind", "b", ":8000", "set the [<address>]:<port> to listen on")
|
|
|
|
ServeCmd.Flags().StringP("root", "r", PUBDIR, "set the root directory to serve")
|
2023-03-12 09:34:38 +03:00
|
|
|
|
|
|
|
RootCmd.AddCommand(BuildCmd)
|
2023-04-01 03:47:02 +03:00
|
|
|
RootCmd.AddCommand(GenerateCmd)
|
2023-03-12 09:34:38 +03:00
|
|
|
RootCmd.AddCommand(ServeCmd)
|
|
|
|
RootCmd.AddCommand(VarCmd)
|
|
|
|
RootCmd.AddCommand(WatchCmd)
|
|
|
|
|
2015-09-02 20:05:09 +03:00
|
|
|
// prepend .zs to $PATH, so plugins will be found before OS commands
|
2022-08-14 03:52:15 +03:00
|
|
|
w, _ := os.Getwd()
|
2023-03-12 14:23:22 +03:00
|
|
|
ensureFirstPath(filepath.Join(w, ZSDIR))
|
2015-09-02 20:05:09 +03:00
|
|
|
}
|
|
|
|
|
2023-03-21 17:17:14 +03:00
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if configFile == "" {
|
|
|
|
// Use config file from .zs/config.yml
|
|
|
|
viper.AddConfigPath(ZSDIR)
|
|
|
|
viper.SetConfigName(ZSCONFIG)
|
|
|
|
} else {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// from the environment
|
|
|
|
viper.SetEnvPrefix("ZS")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
2023-03-26 17:46:09 +03:00
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
|
|
|
log.WithError(err).Warnf("error reading config %s (using defaults)", viper.ConfigFileUsed())
|
|
|
|
}
|
2023-03-21 17:17:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 10:23:10 +03:00
|
|
|
// ParseIgnoreFile parsers a .zsignore file or uses the default if an error occurred
|
2023-03-18 04:59:16 +03:00
|
|
|
func ParseIgnoreFile(fn string) *ignore.GitIgnore {
|
|
|
|
obj, err := ignore.CompileIgnoreFile(ZSIGNORE)
|
|
|
|
if err != nil {
|
2023-03-18 10:40:46 +03:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
|
|
log.WithError(err).Warnf("error parsing .zsignore: %s (using defaults)s", fn)
|
|
|
|
}
|
2023-03-18 04:59:16 +03:00
|
|
|
return ignore.CompileIgnoreLines(DefaultIgnore)
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2014-12-05 19:59:56 +03:00
|
|
|
func main() {
|
2023-03-12 14:23:22 +03:00
|
|
|
// prepend .zs to $PATH, so plugins will be found before OS commands
|
2023-03-21 17:17:14 +03:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("error getting current working directory")
|
|
|
|
}
|
|
|
|
ensureFirstPath(filepath.Join(cwd, ZSDIR))
|
2023-03-12 14:23:22 +03:00
|
|
|
|
2023-03-21 17:17:14 +03:00
|
|
|
// initializes Ignore (.zsignore) patterns
|
2023-03-18 04:59:16 +03:00
|
|
|
Ignore = ParseIgnoreFile(ZSIGNORE)
|
2023-03-12 14:52:14 +03:00
|
|
|
|
2023-03-12 09:34:38 +03:00
|
|
|
if err := RootCmd.Execute(); err != nil {
|
|
|
|
log.WithError(err).Error("error executing command")
|
2023-03-12 07:13:53 +03:00
|
|
|
os.Exit(1)
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
}
|