"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"
- 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
},
}
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
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
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])
+ }
}
}
} 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)
})
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