Skip to content

Commit

Permalink
lib: expose SetOutput for default logger
Browse files Browse the repository at this point in the history
xprint: various updates to template functions, tracking etc
  • Loading branch information
nexus166 committed Jul 22, 2021
1 parent a8e2c35 commit 829aa8f
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 243 deletions.
23 changes: 16 additions & 7 deletions cmd/xprint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"text/template"

log "github.com/szampardi/msg"
Expand All @@ -27,6 +27,7 @@ var (
_template *template.Template //
unsafe *bool = flag.Bool("u", false, "allow evaluation of dangerous template functions such as cmd,env") //
showFns *bool = flag.Bool("F", false, "print available template functions and exit") //
debug *bool = flag.Bool("D", false, "debug template rendering activities") //
output *os.File //
argsfirst *bool = flag.Bool("a", false, "output arguments (if any) before stdin (if any), instead of the opposite") //
showVersion *bool = flag.Bool("v", false, "print build version/date and exit")
Expand Down Expand Up @@ -61,11 +62,14 @@ func setFlags() {
"t",
"template (string or file)",
func(value string) error {
if *debug {
go usageDebugger()
}
_, err := os.Stat(value)
if err == nil {
_template, err = template.New(value).Funcs(tplFuncMap).ParseFiles(value)
_template, err = template.New(value).Funcs(buildFuncMap(*unsafe)).ParseFiles(value)
} else {
_template, err = template.New(os.Args[0]).Funcs(tplFuncMap).Parse(value)
_template, err = template.New(os.Args[0]).Funcs(buildFuncMap(*unsafe)).Parse(value)
}
return err
},
Expand Down Expand Up @@ -108,11 +112,13 @@ func init() {
os.Exit(0)
}
if *showFns {
fns := []string{}
for s := range tplFuncMap {
fns = append(fns, s)
enc := json.NewEncoder(os.Stderr)
enc.SetIndent("", " ")
err := enc.Encode(templateFnsInfo)
if err != nil {
panic(err)
}
fmt.Fprintf(os.Stderr, "%v", strings.Join(fns, "\n"))
os.Exit(0)
}
if err := log.IsValidLevel(int(loglvl)); err != nil {
panic(err)
Expand Down Expand Up @@ -148,6 +154,9 @@ func main() {
if err := _template.Execute(buf, data); err != nil {
panic(err)
}
if *debug {
trackWg.Wait()
}
} else {
for _, s := range dataIndex {
_, err := fmt.Fprintf(buf, "%s", data[s])
Expand Down
Loading

0 comments on commit 829aa8f

Please sign in to comment.