Browse Source

Improvements: Add pipe ability. Add default prefix. Generate default output file if input file has .tpl extension.

master
merkulov 7 years ago
parent
commit
872731fe15
  1. 78
      main.go

78
main.go

@ -5,31 +5,24 @@ import (
"os" "os"
"strings" "strings"
"text/template" "text/template"
"bufio"
"io"
) )
const envDelimiter string = "=" const defaultParamPrefix = "GEV"
const paramsListDelimiter string = "," const envDelimiter = "="
const paramsListSuffix string = "LIST" const paramsListDelimiter = ","
const paramsListSuffix = "LIST"
const templateSuffix = ".tpl"
func getArgs() (string, string, string) { func getArgs() (string, string, string) {
inFileName := flag.String("i", "", "Input file name") inFileName := flag.String("i", "", "Input file name")
outFileName := flag.String("o", "", "Output file name") outFileName := flag.String("o", "", "Output file name")
prefix := flag.String("p", "", "Prefix for environment variables") prefix := flag.String("p", defaultParamPrefix, "Prefix for environment variables")
flag.Parse() flag.Parse()
if len(*inFileName) == 0 { if *outFileName == "" && strings.HasSuffix(*inFileName, templateSuffix) {
os.Stderr.WriteString("Input file name not present\n") *outFileName = strings.TrimSuffix(*inFileName, templateSuffix)
os.Exit(1)
}
if len(*outFileName) == 0 {
os.Stderr.WriteString("Output file name not present\n")
os.Exit(1)
}
if len(*prefix) == 0 {
os.Stderr.WriteString("Prefix not present\n")
os.Exit(1)
} }
return *inFileName, *outFileName, *prefix return *inFileName, *outFileName, *prefix
@ -43,9 +36,11 @@ func getParams(prefix string, suffix string, delimiter string) map[string]interf
for _, e := range os.Environ() { for _, e := range os.Environ() {
pair := strings.Split(e, envDelimiter) pair := strings.Split(e, envDelimiter)
if strings.HasPrefix(pair[0], prefix) { if strings.HasPrefix(pair[0], prefix) {
var paramName string var (
var paramValue string paramName string
var paramList []string paramValue string
paramList []string
)
if strings.HasSuffix(pair[0], suffix) { if strings.HasSuffix(pair[0], suffix) {
suffixShift := len(pair[0]) - suffixLen - 1 suffixShift := len(pair[0]) - suffixLen - 1
paramName = pair[0][prefixShift:suffixShift] paramName = pair[0][prefixShift:suffixShift]
@ -65,20 +60,45 @@ func getParams(prefix string, suffix string, delimiter string) map[string]interf
} }
func main() { func main() {
var inFileName string var (
var outFileName string inFileName string
var prefix string outFileName string
prefix string
out *os.File
tmpl *template.Template
err error
)
inFileName, outFileName, prefix = getArgs() inFileName, outFileName, prefix = getArgs()
tmpl, err := template.ParseFiles(inFileName) if inFileName != "" {
if err != nil { tmpl, err = template.ParseFiles(inFileName)
panic(err) if err != nil {
panic(err)
}
} else {
var (
content []rune
b rune
)
reader := bufio.NewReader(os.Stdin)
for {
b, _, err = reader.ReadRune()
if err != nil && err == io.EOF {
break
}
content = append(content, b)
}
tmpl, _ = template.New("piped").Parse(string(content))
} }
out, err := os.Create(outFileName) if outFileName != "" {
if err != nil { out, err = os.Create(outFileName)
panic(err) if err != nil {
panic(err)
}
} else {
out = os.Stdout
} }
tplParams := getParams(prefix, paramsListSuffix, paramsListDelimiter) tplParams := getParams(prefix, paramsListSuffix, paramsListDelimiter)

Loading…
Cancel
Save