-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (84 loc) · 2.05 KB
/
main.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
package main
import (
"encoding/csv"
"fmt"
"github.com/alexflint/go-arg"
"github.com/jedib0t/go-pretty/v6/table"
"io"
"log"
"os"
"strings"
)
type args struct {
Header bool `arg:"-H" help:"treat the first row as a header row"`
Footer bool `arg:"-F" help:"treat the last row as a footer row"`
Style *string `arg:"-S" default:"DEFAULT" help:"set the table style: DEFAULT/DOUBLE/LIGHT/ROUNDED/BOLD/COLORED_BRIGHT/COLORED_DARK"`
File *string `arg:"positional"`
}
var styles = map[string]table.Style{
"DEFAULT": table.StyleDefault,
"DOUBLE": table.StyleDouble,
"LIGHT": table.StyleLight,
"ROUNDED": table.StyleRounded,
"BOLD": table.StyleBold,
"COLORED_BRIGHT": table.StyleColoredBright,
"COLORED_DARK": table.StyleColoredDark,
}
func (args) Description() string {
return "Reads a CSV input and outputs it formatted as a table"
}
func main() {
var args args
p := arg.MustParse(&args)
var inputReader io.Reader
if args.File == nil {
fi, _ := os.Stdin.Stat()
if (fi.Mode() & os.ModeCharDevice) != 0 {
p.Fail("No input given")
}
inputReader = os.Stdin
} else {
file, err := os.Open(*args.File)
if err != nil {
p.Fail(fmt.Sprint("Failed to read input file:\n\t", err))
}
inputReader = file
}
reader := csv.NewReader(inputReader)
records, err := reader.ReadAll()
log.SetFlags(0)
if err != nil {
log.Fatal("Failed to read CSV:\n\t", err)
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
row := 0
if args.Header {
t.AppendHeader(toRow(records[0]))
row++
}
if args.Style != nil {
style, exists := styles[strings.ToUpper(*args.Style)]
if exists {
t.SetStyle(style)
} else {
p.Fail(fmt.Sprint("Unknown style: ", *args.Style))
}
}
rowCount := len(records)
for ; row < rowCount; row++ {
appendFunc := t.AppendRow
if row == rowCount-1 && args.Footer {
appendFunc = t.AppendFooter
}
appendFunc(toRow(records[row]))
}
t.Render()
}
func toRow(rowStr []string) table.Row {
r := make(table.Row, len(rowStr))
for i, v := range rowStr {
r[i] = v
}
return r
}