add template

This commit is contained in:
Florian Brinker 2019-11-07 23:50:52 +01:00
parent 4c5f0018b3
commit af45000c0b
2 changed files with 40 additions and 7 deletions

38
main.go
View File

@ -5,6 +5,7 @@ import (
"encoding/csv" "encoding/csv"
"flag" "flag"
"fmt" "fmt"
"html/template"
"io" "io"
"os" "os"
) )
@ -13,30 +14,53 @@ func main() {
var file = flag.String("file", "", "File to import") var file = flag.String("file", "", "File to import")
var columnStart = flag.Int("columnStart", -1, "Column number with a start Datetime") var columnStart = flag.Int("columnStart", -1, "Column number with a start Datetime")
var columnEnd = flag.Int("columnEnd", -1, "Column number with a end Datetime") var columnEnd = flag.Int("columnEnd", -1, "Column number with a end Datetime")
var descriptiveColumn = flag.Int("columnDescription", -1, "Column number with a descriptive name or similar") var columnDescription = flag.Int("columnDescription", -1, "Column number with a descriptive name or similar")
flag.Parse() flag.Parse()
if _, err := os.Stat(*file); os.IsNotExist(err) || *columnStart == -1 || *columnEnd == -1 || *descriptiveColumn == -1 { if _, err := os.Stat(*file); os.IsNotExist(err) || *columnStart == -1 || *columnEnd == -1 || *columnDescription == -1 {
flag.Usage() flag.Usage()
return return
} }
fmt.Println("Scanning file " + *file + "...") fmt.Println("Scanning file " + *file + "...")
readCsvFile(*file, *columnStart, *columnEnd, *descriptiveColumn) readCsvFile(*file, *columnStart, *columnEnd, *columnDescription)
} }
func readCsvFile(csvFile string, columnStart int, columnEnd int, descriptiveColumn int) { func readCsvFile(csvFile string, columnStart int, columnEnd int, columnDescription int) {
f, _ := os.Open(csvFile) f, _ := os.Open(csvFile)
r := csv.NewReader(bufio.NewReader(f)) r := csv.NewReader(bufio.NewReader(f))
type JobDetails struct {
Start string
End string
Description string
}
type ViewData struct {
Jobs []JobDetails
}
var data ViewData
for { for {
record, err := r.Read() row, err := r.Read()
if err == io.EOF { if err == io.EOF {
break break
} }
for value := range record { job := JobDetails{
fmt.Printf(" %v\n", record[value]) Start: row[columnStart],
End: row[columnEnd],
Description: row[columnDescription],
} }
data.Jobs = append(data.Jobs, job)
} }
tpl, err := template.ParseFiles("template.html")
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v\n", data)
tpl.Execute(os.Stdout, data)
} }

9
template.html Normal file
View File

@ -0,0 +1,9 @@
<html>
<table>
{{ range .Jobs }}
<tr>
<td>{{ .Description }}</td>
</tr>
{{ end }}
</table>
</html>