Skip to content

Commit

Permalink
Initial checkin.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwald committed Oct 16, 2015
1 parent e6b976e commit 18b37fd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md

This file was deleted.

97 changes: 97 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"encoding/csv"
"fmt"
"os"
"text/template"

"github.com/spf13/cobra"
)

var templateFile string

func main() {

var rootCmd = &cobra.Command{
Use: "tmplgen",
Run: run,
}

rootCmd.Flags().StringVarP(&templateFile, "template-file", "t", "", "template in text/template format")

rootCmd.Execute()
}

func exitWithError(msg string, err error) {
fmt.Fprintf(os.Stderr, "%v: %v\n", msg, err)
os.Exit(1)
}

func run(cmd *cobra.Command, args []string) {

if templateFile == "" {
exitWithError("parameter error", fmt.Errorf("template-file not specified"))
}

var data csvData
var funcs = template.FuncMap{
"field": data.lookupValue,
}

t, err := template.New("main").Funcs(funcs).ParseFiles(templateFile)
if err != nil {
exitWithError("error parsing template", err)
}

rdr := csv.NewReader(os.Stdin)
rdr.Comment = '#'
rdr.LazyQuotes = true
rdr.TrimLeadingSpace = true

records, err := rdr.ReadAll()
if err != nil {
exitWithError("error reading csv input", err)
}

err = data.init(records)
if err != nil {
exitWithError("error processing input data", err)
}

err = t.Templates()[0].Execute(os.Stdout, data)
if err != nil {
exitWithError("error executing template", err)
}
}

type csvData struct {
Fields []string
Records [][]string

fieldMap map[string]int
}

func (d *csvData) init(records [][]string) error {
if len(records) == 0 {
return fmt.Errorf("no records provided")
}

d.Fields = records[0]
d.Records = records[1:]

d.fieldMap = make(map[string]int)
for i, f := range d.Fields {
d.fieldMap[f] = i
}

return nil
}

func (d *csvData) lookupValue(record []string, field string) (string, error) {
if idx, ok := d.fieldMap[field]; ok {
return record[idx], nil
} else {
return "", fmt.Errorf("could not find field %q", field)
}
}

0 comments on commit 18b37fd

Please sign in to comment.