Fix errors filling in variables (Fixes #2)

This commit is contained in:
James Mills 2023-03-12 17:43:50 +10:00
parent 169a8ac62c
commit 7198656bcd
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 35 additions and 18 deletions

49
main.go
View File

@ -19,7 +19,6 @@ import (
"github.com/russross/blackfriday/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.mills.io/static"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
@ -65,13 +64,19 @@ var RootCmd = &cobra.Command{
- 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.
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// set logging level
if viper.GetBool("debug") {
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 {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
return nil
},
}
@ -244,7 +249,8 @@ func run(vars Vars, cmd string, args ...string) (string, error) {
err := c.Run()
if errbuf.Len() > 0 {
log.Println("ERROR:", errbuf.String())
log.Errorf("error running command: %s", cmd)
log.Error(errbuf.String())
}
if err != nil {
return "", err
@ -293,7 +299,7 @@ func getVars(path string, globals Vars) (Vars, string, error) {
vars := Vars{}
if err := yaml.Unmarshal([]byte(header), &vars); err != nil {
fmt.Println("WARN: failed to parse header", err)
log.WithError(err).Warn("failed to parse header")
return v, s, nil
}
// Override default values + globals with the ones defines in the file
@ -321,23 +327,26 @@ func render(s string, vars Vars) (string, error) {
to := strings.Index(s, closingDelimiter)
if to == -1 {
return "", fmt.Errorf("Close delim not found")
return "", fmt.Errorf("closing delimiter not found")
}
out.WriteString(s[:from])
cmd := s[from+len(openingDelimiter) : to]
s = s[to+len(closingDelimiter):]
m := strings.Fields(cmd)
m := strings.Fields(strings.TrimSpace(cmd))
if len(m) == 1 {
log.Debugf("vars: #%v", vars)
if v, ok := vars[m[0]]; ok {
out.WriteString(v)
continue
}
}
if res, err := run(vars, m[0], m[1:]...); err == nil {
out.WriteString(res)
} else {
fmt.Println(err)
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])
}
}
}
@ -453,10 +462,12 @@ func buildAll(ctx context.Context, watch bool) error {
} else if info.ModTime().After(lastModified) {
if !modified {
// First file in this build cycle is about to be modified
if _, err := run(vars, "prehook"); err != nil {
log.WithError(err).Warn("error running prehook")
if _, err := exec.LookPath("prehook"); err == nil {
if _, err := run(vars, "prehook"); err != nil {
log.WithError(err).Warn("error running prehook")
}
modified = true
}
modified = true
}
log.Debugf("build: %s", path)
return build(path, nil, vars)
@ -465,10 +476,12 @@ func buildAll(ctx context.Context, watch bool) error {
})
if modified {
// At least one file in this build cycle has been modified
if _, err := run(vars, "posthook"); err != nil {
log.WithError(err).Warn("error running posthook")
if _, err := exec.LookPath("posthook"); err == nil {
if _, err := run(vars, "posthook"); err != nil {
log.WithError(err).Warn("error running posthook")
}
modified = false
}
modified = false
}
if !watch {
return nil

4
test.md Normal file
View File

@ -0,0 +1,4 @@
url: "example.com/foo.html"
---
Hello