-
Notifications
You must be signed in to change notification settings - Fork 0
/
toGraph.go
108 lines (86 loc) · 2.41 KB
/
toGraph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bufio"
"io"
"log"
"os"
"path/filepath"
"strings"
"text/template"
datarecord "github.com/nikolainp/toGraph/datarecord"
state "github.com/nikolainp/toGraph/statecontext"
)
var checkErr = func(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
state.InitState()
state.Configure(os.Args)
run()
}
func run() {
config := state.Config()
for _, fileName := range config.InputFiles {
inputFile := getInputStream(fileName)
outputFile, outName := getOutputStream(fileName, config)
log.Printf("file being processed: %s", fileName)
processFile(inputFile, outputFile, config, outName)
}
}
func getInputStream(fileName string) io.Reader {
if fileName == "" {
return os.Stdin
}
inputFile, err := os.Open(fileName)
checkErr(err)
return inputFile
}
func getOutputStream(fileName string, config state.Configuration) (io.Writer, string) {
var outputName string
var outputPath string
if len(config.OutputFile) == 0 {
outputPath, outputName = getOutFileName(fileName)
} else {
_, outputName = getOutFileName(config.OutputFile)
outputPath = config.OutputFile
}
outputFile, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0660)
checkErr(err)
return outputFile, outputName
}
func getOutFileName(fileName string) (outFilePath string, outFileName string) {
outFilePath = filepath.Dir(fileName)
outFileName = filepath.Base(fileName)
outFileName = strings.TrimSuffix(outFileName, filepath.Ext(outFileName))
return filepath.Join(outFilePath, outFileName+".html"), outFileName
}
func processFile(sIn io.Reader, sOut io.Writer, config state.Configuration, title string) error {
scanner := bufio.NewScanner(sIn)
reader := datarecord.GetDataReader()
reader.WithDateFormat(config.DateFormat)
reader.WithDateColumn(config.DateColumn)
reader.WithPivotColumn(config.PivotColumn)
reader.WithDelimiter(config.Delimiter)
reader.WithColumnNames(config.ColumnNames)
reader.WithColumnNamesInFirstRow(config.IsColumnNamesInFirstRow)
dataGraph, err := template.New("dataGraph").Parse(graphTemplate)
checkErr(err)
for i := 0; scanner.Scan(); i++ {
dataString := scanner.Text()
reader.ReadDataRecord(dataString)
}
data := struct {
Title string
Columns []datarecord.ColumnStatistic
DataRows []string
}{
Title: title,
Columns: reader.GetColumns(),
DataRows: reader.GetDataRows(),
}
err = dataGraph.Execute(sOut, data)
checkErr(err)
return nil
}