-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (81 loc) · 1.7 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
package main
import (
"encoding/csv"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
)
const version = "0.0.1"
var revision = "HEAD"
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
func getFileNameWithoutExt(path string) string {
return filepath.Base(path[:len(path)-len(filepath.Ext(path))])
}
func writeWrapper(w *csv.Writer, record []string) error {
for i, field := range record {
record[i] = `"` + field + `"`
}
return w.Write(record)
}
func main() {
var showVersion bool
var showHelp bool
// options
fs := flag.NewFlagSet("csvdouble", flag.ExitOnError)
fs.BoolVar(&showVersion, "version", false, "show version")
fs.BoolVar(&showVersion, "v", false, "show version")
fs.BoolVar(&showHelp, "help", false, "show help")
fs.BoolVar(&showHelp, "h", false, "show help")
fs.Usage = func() {
fmt.Println(`Usage:
csvdouble <path>
Flags:`)
fs.PrintDefaults()
fmt.Println(`Repository:
https://github.com/zztkm/csvdouble`)
}
fs.Parse(os.Args[1:])
if showVersion {
fmt.Printf("version: %s, revision: %s\n", version, revision)
return
}
if showHelp {
fs.Usage()
return
}
if fs.NArg() < 1 {
fatal(errors.New("please specify csv file path. `csvdouble -h` for more details"))
}
filename := fs.Arg(0)
// 読み込みファイル
rf, err := os.Open(filename)
if err != nil {
fatal(err)
}
defer rf.Close()
r := csv.NewReader(rf)
// 書き込みファイル
wf, err := os.Create(getFileNameWithoutExt(filename) + "-double.csv")
if err != nil {
fatal(err)
}
defer wf.Close()
w := csv.NewWriter(wf)
defer w.Flush()
for {
records, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
fatal(err)
}
writeWrapper(w, records)
}
}