add template
Cette révision appartient à :
Parent
4c5f0018b3
révision
af45000c0b
38
main.go
38
main.go
@ -5,6 +5,7 @@ import (
|
||||
"encoding/csv"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
@ -13,30 +14,53 @@ func main() {
|
||||
var file = flag.String("file", "", "File to import")
|
||||
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 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()
|
||||
|
||||
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()
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
r := csv.NewReader(bufio.NewReader(f))
|
||||
|
||||
type JobDetails struct {
|
||||
Start string
|
||||
End string
|
||||
Description string
|
||||
}
|
||||
|
||||
type ViewData struct {
|
||||
Jobs []JobDetails
|
||||
}
|
||||
|
||||
var data ViewData
|
||||
|
||||
for {
|
||||
record, err := r.Read()
|
||||
row, err := r.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
for value := range record {
|
||||
fmt.Printf(" %v\n", record[value])
|
||||
job := JobDetails{
|
||||
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
Fichier normal
9
template.html
Fichier normal
@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<table>
|
||||
{{ range .Jobs }}
|
||||
<tr>
|
||||
<td>{{ .Description }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
</html>
|
Chargement…
Référencer dans un nouveau ticket
Block a user