mirror of
https://git.mills.io/prologic/zs.git
synced 2024-09-07 12:44:15 -04:00
Fix errors filling in variables (Fixes #2)
This commit is contained in:
parent
169a8ac62c
commit
7198656bcd
31
main.go
31
main.go
@ -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 _, err := exec.LookPath(m[0]); err == nil {
|
||||
if res, err := run(vars, m[0], m[1:]...); err == nil {
|
||||
out.WriteString(res)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
log.WithError(err).Warnf("error running command: %s", m[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -453,11 +462,13 @@ 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 := exec.LookPath("prehook"); err == nil {
|
||||
if _, err := run(vars, "prehook"); err != nil {
|
||||
log.WithError(err).Warn("error running prehook")
|
||||
}
|
||||
modified = true
|
||||
}
|
||||
}
|
||||
log.Debugf("build: %s", path)
|
||||
return build(path, nil, vars)
|
||||
}
|
||||
@ -465,11 +476,13 @@ func buildAll(ctx context.Context, watch bool) error {
|
||||
})
|
||||
if modified {
|
||||
// At least one file in this build cycle has been modified
|
||||
if _, err := exec.LookPath("posthook"); err == nil {
|
||||
if _, err := run(vars, "posthook"); err != nil {
|
||||
log.WithError(err).Warn("error running posthook")
|
||||
}
|
||||
modified = false
|
||||
}
|
||||
}
|
||||
if !watch {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user