mirror of
https://git.mills.io/prologic/zs.git
synced 2024-11-03 01:38:30 -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"
|
"github.com/russross/blackfriday/v2"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.mills.io/static"
|
"go.mills.io/static"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
"gopkg.in/yaml.v2"
|
"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.
|
- 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.
|
- Everything the extensions prints to stdout becomes the value of the placeholder.
|
||||||
`,
|
`,
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
// set logging level
|
debug, err := cmd.Flags().GetBool("debug")
|
||||||
if viper.GetBool("debug") {
|
if err != nil {
|
||||||
|
return fmt.Errorf("error getting debug flag: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if debug {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
} else {
|
} else {
|
||||||
log.SetLevel(log.InfoLevel)
|
log.SetLevel(log.InfoLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,7 +249,8 @@ func run(vars Vars, cmd string, args ...string) (string, error) {
|
|||||||
err := c.Run()
|
err := c.Run()
|
||||||
|
|
||||||
if errbuf.Len() > 0 {
|
if errbuf.Len() > 0 {
|
||||||
log.Println("ERROR:", errbuf.String())
|
log.Errorf("error running command: %s", cmd)
|
||||||
|
log.Error(errbuf.String())
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -293,7 +299,7 @@ func getVars(path string, globals Vars) (Vars, string, error) {
|
|||||||
|
|
||||||
vars := Vars{}
|
vars := Vars{}
|
||||||
if err := yaml.Unmarshal([]byte(header), &vars); err != nil {
|
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
|
return v, s, nil
|
||||||
}
|
}
|
||||||
// Override default values + globals with the ones defines in the file
|
// 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)
|
to := strings.Index(s, closingDelimiter)
|
||||||
if to == -1 {
|
if to == -1 {
|
||||||
return "", fmt.Errorf("Close delim not found")
|
return "", fmt.Errorf("closing delimiter not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
out.WriteString(s[:from])
|
out.WriteString(s[:from])
|
||||||
cmd := s[from+len(openingDelimiter) : to]
|
cmd := s[from+len(openingDelimiter) : to]
|
||||||
s = s[to+len(closingDelimiter):]
|
s = s[to+len(closingDelimiter):]
|
||||||
m := strings.Fields(cmd)
|
m := strings.Fields(strings.TrimSpace(cmd))
|
||||||
if len(m) == 1 {
|
if len(m) == 1 {
|
||||||
|
log.Debugf("vars: #%v", vars)
|
||||||
if v, ok := vars[m[0]]; ok {
|
if v, ok := vars[m[0]]; ok {
|
||||||
out.WriteString(v)
|
out.WriteString(v)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if _, err := exec.LookPath(m[0]); err == nil {
|
||||||
if res, err := run(vars, m[0], m[1:]...); err == nil {
|
if res, err := run(vars, m[0], m[1:]...); err == nil {
|
||||||
out.WriteString(res)
|
out.WriteString(res)
|
||||||
} else {
|
} 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) {
|
} else if info.ModTime().After(lastModified) {
|
||||||
if !modified {
|
if !modified {
|
||||||
// First file in this build cycle is about to be 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 {
|
if _, err := run(vars, "prehook"); err != nil {
|
||||||
log.WithError(err).Warn("error running prehook")
|
log.WithError(err).Warn("error running prehook")
|
||||||
}
|
}
|
||||||
modified = true
|
modified = true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
log.Debugf("build: %s", path)
|
log.Debugf("build: %s", path)
|
||||||
return build(path, nil, vars)
|
return build(path, nil, vars)
|
||||||
}
|
}
|
||||||
@ -465,11 +476,13 @@ func buildAll(ctx context.Context, watch bool) error {
|
|||||||
})
|
})
|
||||||
if modified {
|
if modified {
|
||||||
// At least one file in this build cycle has been 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 {
|
if _, err := run(vars, "posthook"); err != nil {
|
||||||
log.WithError(err).Warn("error running posthook")
|
log.WithError(err).Warn("error running posthook")
|
||||||
}
|
}
|
||||||
modified = false
|
modified = false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if !watch {
|
if !watch {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user