generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (118 loc) · 2.76 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"encoding/csv"
"errors"
"flag"
"fmt"
"io"
"os"
ff "github.com/peterbourgon/ff/v3"
"moul.io/banner"
"moul.io/climan"
"moul.io/mdtable/mdtable"
)
func main() {
if err := run(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
}
os.Exit(1)
}
}
type opts struct {
mdt mdtable.Opts
csv struct {
noHeader bool
}
// json struct {}
}
var o opts
func (o *opts) commonFlagBuilder(fs *flag.FlagSet) {
fs.StringVar(&o.mdt.BodyFormat, "md-body", o.mdt.BodyFormat, "mdtable body format")
fs.StringVar(&o.mdt.HeaderFormat, "md-header", o.mdt.HeaderFormat, "mdtable header format")
}
func run(args []string) error {
// default opts
o.mdt.BodyFormat = `{{range .Cols}}{{.}}\t{{end}}`
o.mdt.HeaderFormat = `{{range .Cols}}{{.}}\t{{end}}`
// parse CLI
root := &climan.Command{
Name: "mdtable",
ShortUsage: "mdtable [global flags] <subcommand> [flags] [args]",
ShortHelp: "More info on https://moul.io/mdtable.",
FlagSetBuilder: o.commonFlagBuilder,
FFOptions: []ff.Option{ff.WithEnvVarPrefix("mdtable")},
LongHelp: banner.Inline("mdtable"),
Subcommands: []*climan.Command{
{
Name: "csv",
ShortUsage: "mdtable csv [flags]",
ShortHelp: "CSV to markdown table",
FlagSetBuilder: func(fs *flag.FlagSet) {
o.commonFlagBuilder(fs)
fs.BoolVar(&o.csv.noHeader, "csv-no-header", o.csv.noHeader, "csv: no header, just body")
},
Exec: doCSV,
},
/*{
Name: "json",
ShortUsage: "mdtable json [flags]",
ShortHelp: "JSON to markdown table",
FlagSetBuilder: func(fs *flag.FlagSet) {
o.commonFlagBuilder(fs)
},
},*/
},
}
if err := root.Parse(args); err != nil {
return fmt.Errorf("parse error: %w", err)
}
if err := root.Run(context.Background()); err != nil {
return fmt.Errorf("%w", err)
}
return nil
}
func doCSV(_ context.Context, args []string) error {
if len(args) > 0 {
return flag.ErrHelp
}
data := mdtable.Data{
Header: mdtable.Row{},
Rows: []mdtable.Row{},
}
csvFile := os.Stdin
reader := csv.NewReader(csvFile)
// Read CSV data
for i := 0; ; i++ {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("reading CSV: %w", err)
}
row := mdtable.Row{
Cols: record,
}
if i == 0 && !o.csv.noHeader {
data.Header = row
} else {
data.Rows = append(data.Rows, row)
}
}
if o.csv.noHeader {
data.Header = mdtable.Row{
Cols: make([]string, len(data.Rows[0].Cols)),
}
for i := range data.Rows[0].Cols {
data.Header.Cols[i] = "<!-- -->"
}
}
output, err := mdtable.Generate(data, o.mdt)
if err != nil {
return fmt.Errorf("mdtable generate: %w", err)
}
fmt.Print(output)
return nil
}