/
output.go
55 lines (47 loc) · 989 Bytes
/
output.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
package querycost
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
type Output interface {
DisplayResult(res FormattedResult)
}
type StandardOutput struct{}
type CSVOutput struct {
file string
}
func (s StandardOutput) DisplayResult(res FormattedResult) {
for _, row := range res.Value {
fmt.Println(strings.Join(row, ","))
}
}
func (s CSVOutput) DisplayResult(res FormattedResult) {
f, err := os.Create(s.file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
for _, row := range res.Value {
f.WriteString(strings.Join(row, ","))
f.WriteString("\n")
}
}
func NewCSVOutput(_output string) CSVOutput {
parent := filepath.Dir(_output)
if err := os.MkdirAll(parent, os.ModePerm); err != nil {
panic(err)
}
return CSVOutput{file: _output}
}
type ArrayOutput struct {
Array *[][]string
}
func NewArrayOutput(a *[][]string) ArrayOutput {
return ArrayOutput{Array: a}
}
func (a ArrayOutput) DisplayResult(res FormattedResult) {
*a.Array = res.Value
}