-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (117 loc) · 2.56 KB
/
main.go
File metadata and controls
132 lines (117 loc) · 2.56 KB
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
132
package main
import (
"encoding/csv"
"fmt"
"os"
"sort"
"strings"
"github.com/urfave/cli/v2"
)
var (
infile = "-"
outfile = "-"
tabs bool
)
func main() {
app := cli.App{
Name: "sortcsv",
Usage: "Sort CSV file",
Commands: buildCommands(),
HideVersion: true,
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "error running app: %v\n", err)
}
}
func buildCommands() []*cli.Command {
return []*cli.Command{
sortCmd(),
}
}
func sortCmd() *cli.Command {
return &cli.Command{
Name: "sort",
Aliases: []string{"s"},
Action: sortcsv,
Flags: []cli.Flag{
&cli.StringFlag{Name: "infile", Aliases: []string{"in", "i"}, Usage: "Defaults to stdin (-) if not specified.", Destination: &infile, DefaultText: infile},
&cli.StringFlag{Name: "outfile", Aliases: []string{"out", "o"}, Usage: "Defaults to stdout (-) if not specified.", Destination: &outfile, DefaultText: outfile},
&cli.StringSliceFlag{Name: "sortby", Aliases: []string{"s"}, Usage: "Columns to sort by, repeat for sub-sort"},
&cli.BoolFlag{Name: "tab", Aliases: []string{"t"}, Usage: "Input is tab delimited, rather than comma delimited", Destination: &tabs},
},
}
}
func sortcsv(ctx *cli.Context) error {
var err error
in := os.Stdin
switch infile {
case "", "-":
default:
if in, err = os.Open(infile); err != nil {
return err
}
defer in.Close()
}
reader := csv.NewReader(in)
reader.LazyQuotes = true
if tabs {
reader.Comma = '\t'
}
records, err := reader.ReadAll()
if err != nil {
return err
}
sortKeys := ctx.StringSlice("sortby")
order := sortOrder(sortKeys, records[0])
sortRecords := func(i, j int) bool {
for _, ix := range order {
if records[i+1][ix] == records[j+1][ix] {
continue
}
return records[i+1][ix] < records[j+1][ix]
}
return false
}
sort.Slice(records[1:], sortRecords)
out := os.Stdout
switch outfile {
case "", "-":
default:
if out, err = os.Create(outfile); err != nil {
return err
}
defer out.Close()
}
writer := csv.NewWriter(out)
if tabs {
writer.Comma = '\t'
}
return writer.WriteAll(records)
}
func sortOrder(keys []string, header []string) []int {
ret := []int{}
for _, lhs := range keys {
k := key(lhs)
for ix, rhs := range header {
if k == key(rhs) {
ret = append(ret, ix)
}
}
}
if len(ret) == 0 {
ret = []int{0}
}
return ret
}
func key(name string) string {
reduce := func(r rune) rune {
switch {
case r >= 'A' && r <= 'Z':
return r + 32
case r >= 'a' && r <= 'z':
return r
}
return -1
}
return strings.Map(reduce, name)
}