-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (68 loc) · 1.54 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
package main
import (
"flag"
"fmt"
"os"
"github.com/eiri/taon/pkg/taon"
)
const (
exitOK = iota
exitOpenFile
exitParseError
)
var (
version = "dev"
columns taon.Columns
md bool
showVer bool
)
func init() {
flag.Var(&columns, "columns", "List of columns to display")
flag.Var(&columns, "c", "List of columns to display")
flag.BoolVar(&md, "markdown", false, "Print as markdown table")
flag.BoolVar(&md, "m", false, "Print as markdown table")
flag.BoolVar(&showVer, "version", false, "Show application version")
flag.Usage = func() {
fmt.Fprint(os.Stderr, `Transform JSON into ASCII table
Usage: taon [flags] [file]
Flags:
-c, --columns=COL1,COL2 List of columns to display
-m, --markdown Print as markdown table
-h, --help Show help
--version Show application version
Args:
<file> Path to file to read, stdin when missing
`)
}
}
func main() {
flag.Parse()
if showVer {
fmt.Printf("taon version %s\n", version)
os.Exit(0)
}
var err error
reader := os.Stdin
if flag.NArg() > 0 && flag.Arg(0) != "-" {
file := flag.Arg(0)
reader, err = os.Open(file)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open file: %s\n", err)
os.Exit(exitOpenFile)
}
defer reader.Close()
}
t := taon.NewTable(reader, os.Stdout)
if md {
t.SetModeMarkdown()
}
if len(columns) > 0 {
t.SetColumns(columns)
}
err = t.Render()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to render table: %s\n", err)
os.Exit(exitParseError)
}
os.Exit(exitOK)
}