From: James Mills <1290234+prologic@users.noreply.github.com> Date: Sun, 12 Mar 2023 07:43:50 +0000 (+1000) Subject: Fix errors filling in variables (Fixes #2) X-Git-Tag: 0.1.0~4 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=7198656bcd5aede6ebaa40348933fa36819387f0;p=aya.git Fix errors filling in variables (Fixes #2) --- diff --git a/main.go b/main.go index 8f2e11b..cdc85f9 100644 --- a/main.go +++ b/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 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 diff --git a/test.md b/test.md new file mode 100644 index 0000000..70671ef --- /dev/null +++ b/test.md @@ -0,0 +1,4 @@ + +url: "example.com/foo.html" +--- +Hello