Skip to content

Commit f486d87

Browse files
author
R. S. Doiel
committed
added useCRLF option to better support Windows, see issue #24
1 parent eb5bc10 commit f486d87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+242
-97
lines changed

cmd/csv2tab/csv2tab.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"os"
1212
"path"
13+
"runtime"
1314

1415
// Caltech Library Packages
1516
"github.com/caltechlibrary/datatools"
@@ -89,6 +90,7 @@ func main() {
8990
license := datatools.LicenseText
9091
releaseDate := datatools.ReleaseDate
9192
releaseHash := datatools.ReleaseHash
93+
useCRLF := (runtime.GOOS == "windows")
9294

9395
flag.BoolVar(&showHelp, "h", false, "display help")
9496
flag.BoolVar(&showHelp, "help", false, "display help")
@@ -100,6 +102,7 @@ func main() {
100102
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quoting for reader")
101103
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trims leading space read")
102104
flag.BoolVar(&reuseRecord, "reuse-record", false, "re-uses the backing array on reader")
105+
flag.BoolVar(&useCRLF, "crlf", useCRLF, "use a CRLF for end of line (EOL)")
103106

104107
// Parse Environment and Options
105108
flag.Parse()
@@ -133,6 +136,7 @@ func main() {
133136
exitCode := 0
134137
w := csv.NewWriter(out)
135138
w.Comma = '\t'
139+
w.UseCRLF = useCRLF
136140
/*
137141
if delimiter != "" {
138142
w.Comma = datatools.NormalizeDelimiterRune(delimiter)

cmd/csvcleaner/csvcleaner.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"os"
2626
"path"
27+
"runtime"
2728
"strings"
2829

2930
// Caltech Library Packages
@@ -113,7 +114,6 @@ minimal memory is used to operate on the file.
113114
-use-lazy-quotes
114115
: use lazy quotes for CSV input
115116
116-
117117
# EXAMPLES
118118
119119
Normalizing a spread sheet's column count to 5 padding columns as needed per row.
@@ -180,6 +180,7 @@ func main() {
180180
license := datatools.LicenseText
181181
releaseDate := datatools.ReleaseDate
182182
releaseHash := datatools.ReleaseHash
183+
useCRLF = (runtime.GOOS == "windows")
183184

184185
// Standard options
185186
flag.BoolVar(&showHelp, "help", false, "display help")
@@ -202,7 +203,7 @@ func main() {
202203
flag.StringVar(&comma, "comma", "", "if set use this character in place of a comma for delimiting cells")
203204
flag.StringVar(&rowComment, "comment-char", "", "if set, rows starting with this character will be ignored as comments")
204205
flag.StringVar(&commaOut, "output-comma", "", "if set use this character in place of a comma for delimiting output cells")
205-
flag.BoolVar(&useCRLF, "use-crlf", false, "if set use a charage return and line feed in output")
206+
flag.BoolVar(&useCRLF, "use-crlf", useCRLF, "if set use a charage return and line feed in output")
206207
flag.BoolVar(&stopOnError, "stop-on-error", false, "exit on error, useful if you're trying to debug a problematic CSV file")
207208
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for CSV input")
208209
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space from field(s) for CSV input")

cmd/csvcols/csvcols.go

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"os"
2626
"path"
27+
"runtime"
2728
"strings"
2829

2930
// Caltech Library packages
@@ -94,6 +95,8 @@ listed on the commandline (first column is 1 not 0).
9495
-uuid
9596
: add a prefix row with generated UUID cell
9697
98+
-crlf
99+
: use a CRLF for end of line (EOL) on output (defualts to true on Windows)
97100
98101
# EXAMPLES
99102
@@ -149,6 +152,7 @@ Using options filter a 3 column CSV file for columns 1,3 into 2col.csv
149152
outputDelimiter string
150153
lazyQuotes bool
151154
trimLeadingSpace bool
155+
useCRLF bool
152156
)
153157

154158

@@ -185,6 +189,7 @@ func CSVColumns(in *os.File, out *os.File, eout *os.File, columnNos []int, prefi
185189
r.TrimLeadingSpace = trimLeadingSpace
186190

187191
w := csv.NewWriter(out)
192+
w.UseCRLF = useCRLF
188193
if delimiterIn != "" {
189194
r.Comma = datatools.NormalizeDelimiterRune(delimiterIn)
190195
}
@@ -222,6 +227,7 @@ func main() {
222227
license := datatools.LicenseText
223228
releaseDate := datatools.ReleaseDate
224229
releaseHash := datatools.ReleaseHash
230+
useCRLF = (runtime.GOOS == "windows")
225231

226232
// Standard Options
227233
flag.BoolVar(&showHelp, "help", false, "display help")
@@ -245,6 +251,7 @@ func main() {
245251
flag.BoolVar(&prefixUUID, "uuid", false, "add a prefix row with generated UUID cell")
246252
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes on CSV input")
247253
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in field(s) for CSV input")
254+
flag.BoolVar(&useCRLF, "crlf", useCRLF, "use a CRLF for end of line (EOL)")
248255

249256
// Parse env and options
250257
flag.Parse()
@@ -320,6 +327,7 @@ func main() {
320327
}
321328

322329
w := csv.NewWriter(out)
330+
w.UseCRLF = useCRLF
323331
if outputDelimiter != "" {
324332
w.Comma = datatools.NormalizeDelimiterRune(outputDelimiter)
325333
}

cmd/csvfind/csvfind.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"os"
2525
"path"
26+
"runtime"
2627
"strings"
2728

2829
// Caltech Library packages
@@ -94,7 +95,10 @@ zero. Supports exact match as well as some Levenshtein matching.
9495
: set the edit distance thresh hold for match, default 0
9596
9697
-nl, -newline
97-
: include trailing newline from output
98+
: include trailing newline from output for end of file (EOF)
99+
100+
-crlf
101+
: use CRLF for end of line (EOL) on write, defaults to true for Windows
98102
99103
-o, -output
100104
: output filename
@@ -182,6 +186,7 @@ You can also search for phrases in columns.
182186
delimiter string
183187
lazyQuotes bool
184188
trimLeadingSpace bool
189+
useCRLF bool
185190
)
186191

187192
func main() {
@@ -190,6 +195,7 @@ func main() {
190195
license := datatools.LicenseText
191196
releaseDate := datatools.ReleaseDate
192197
releaseHash := datatools.ReleaseHash
198+
useCRLF = (runtime.GOOS == "windows")
193199

194200
// Basic Options
195201
flag.BoolVar(&showHelp, "help", false, "display help")
@@ -224,6 +230,7 @@ func main() {
224230
flag.BoolVar(&trimSpaces, "trimspaces", false, "trim spaces around cell values before comparing")
225231
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes on CSV input")
226232
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leadings space in field(s) for CSV input")
233+
flag.BoolVar(&useCRLF, "crlf", useCRLF, "use CRLF for end of line (EOL) on write")
227234

228235
// Parse env and options
229236
flag.Parse()
@@ -300,6 +307,7 @@ func main() {
300307
csvIn.LazyQuotes = lazyQuotes
301308
csvIn.TrimLeadingSpace = trimLeadingSpace
302309
csvOut := csv.NewWriter(out)
310+
csvOut.UseCRLF = useCRLF
303311
if delimiter != "" {
304312
csvIn.Comma = datatools.NormalizeDelimiterRune(delimiter)
305313
csvOut.Comma = datatools.NormalizeDelimiterRune(delimiter)

cmd/csvjoin/csvjoin.go

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"os"
2525
"path"
26+
"runtime"
2627
"strings"
2728

2829
// My packages
@@ -120,6 +121,9 @@ rather than zero.
120121
-use-lazy-quotes
121122
: use lazy quotes for CSV input
122123
124+
-crlf
125+
: use CRLF for end of line (EOL) on write, defaults to true on Windows
126+
123127
-verbose
124128
: output processing count to stderr
125129
@@ -171,6 +175,7 @@ merged-data.csv..
171175
delimiter string
172176
lazyQuotes bool
173177
trimLeadingSpace bool
178+
useCRLF bool
174179
)
175180

176181

@@ -249,6 +254,7 @@ func main() {
249254
license := datatools.LicenseText
250255
releaseDate := datatools.ReleaseDate
251256
releaseHash := datatools.ReleaseHash
257+
useCRLF = (runtime.GOOS == "windows")
252258

253259
// Standard Options
254260
flag.BoolVar(&showHelp, "help", false, "display help")
@@ -280,6 +286,7 @@ func main() {
280286
flag.StringVar(&delimiter, "delimiter", "", "set delimiter character")
281287
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for CSV input")
282288
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in field(s) for CSV input")
289+
flag.BoolVar(&useCRLF, "crlf", useCRLF, "use CRLF for end of line (EOL) on write")
283290

284291
// Parse env and options
285292
flag.Parse()
@@ -370,6 +377,7 @@ func main() {
370377
csv2.TrimLeadingSpace = trimLeadingSpace
371378

372379
w := csv.NewWriter(out)
380+
w.UseCRLF = useCRLF
373381
if delimiter != "" {
374382
csv1.Comma = datatools.NormalizeDelimiterRune(delimiter)
375383
csv2.Comma = datatools.NormalizeDelimiterRune(delimiter)

cmd/csvrows/csvrows.go

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"os"
2525
"path"
26+
"runtime"
2627
"strings"
2728

2829
// Caltech Library packages
@@ -95,6 +96,8 @@ and {app_name} makes it easy to output only the data rows.
9596
-use-lazy-quotes
9697
: use lazy quotes for CSV input
9798
99+
-crlf
100+
: use a CRLF for end of line (EOL) on write, defaults to true on Windows
98101
99102
# EXAMPLES
100103
@@ -159,6 +162,7 @@ a header row from 10row.csv.
159162
randomRows int
160163
lazyQuotes bool
161164
trimLeadingSpace bool
165+
useCRLF bool
162166
)
163167

164168
func main() {
@@ -167,6 +171,7 @@ func main() {
167171
license := datatools.LicenseText
168172
releaseDate := datatools.ReleaseDate
169173
releaseHash := datatools.ReleaseHash
174+
useCRLF = (runtime.GOOS == "windows")
170175

171176
// Standard options
172177
flag.BoolVar(&showHelp, "help", false, "display help")
@@ -189,6 +194,7 @@ func main() {
189194
flag.IntVar(&randomRows, "random", 0, "return N randomly selected rows")
190195
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for CSV input")
191196
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in field(s) for CSV input")
197+
flag.BoolVar(&useCRLF, "crlf", useCRLF, "use a CRLF for end of line (EOL) on write")
192198

193199
// Parse env and options
194200
flag.Parse()
@@ -276,6 +282,7 @@ func main() {
276282

277283
// Clean up cells removing outer quotes if necessary
278284
w := csv.NewWriter(out)
285+
w.UseCRLF = useCRLF
279286
if delimiter != "" {
280287
w.Comma = datatools.NormalizeDelimiterRune(delimiter)
281288
}

cmd/jsoncols/jsoncols.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io/ioutil"
1515
"os"
1616
"path"
17+
"runtime"
1718

1819
// Caltech Library Packages
1920
"github.com/caltechlibrary/datatools"
@@ -69,7 +70,10 @@ extracted is a comma. This can be overridden with an option.
6970
: input filename
7071
7172
-nl, -newline
72-
: if true add a trailing newline
73+
: if true add a trailing newline for end of file (EOF)
74+
75+
-crlf
76+
: use CRLF for CSV writes, defaults to true on Windows
7377
7478
-o, -output
7579
: output filename
@@ -156,6 +160,7 @@ Would yield
156160
delimiter = ","
157161
expressions []string
158162
quote bool
163+
useCRLF bool
159164
)
160165

161166

@@ -170,6 +175,7 @@ func main() {
170175
license := datatools.LicenseText
171176
releaseDate := datatools.ReleaseDate
172177
releaseHash := datatools.ReleaseHash
178+
useCRLF = (runtime.GOOS == "windows")
173179

174180
// Basic Options
175181
flag.BoolVar(&showHelp, "help", false, "display help")
@@ -193,6 +199,7 @@ func main() {
193199
flag.BoolVar(&quote, "quote", true, "quote strings and JSON notation")
194200
flag.BoolVar(&prettyPrint, "p", false, "pretty print JSON output")
195201
flag.BoolVar(&prettyPrint, "pretty", false, "pretty print JSON output")
202+
flag.BoolVar(&useCRLF, "crlf", useCRLF, "use a CRLF for end of line (EOL) on CSV write")
196203

197204
// Parse Environment and Options
198205
flag.Parse()
@@ -294,6 +301,7 @@ func main() {
294301

295302
// Setup the CSV output
296303
w := csv.NewWriter(out)
304+
w.UseCRLF = useCRLF
297305
if delimiter != "" {
298306
w.Comma = datatools.NormalizeDelimiterRune(delimiter)
299307
}

cmd/sql2csv/sql2csv.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path"
10+
"runtime"
1011

1112
// Caltech Library Packages
1213
"github.com/caltechlibrary/datatools"
@@ -113,9 +114,10 @@ configuration file
113114
-delimiter
114115
: Set the delimiter to use, default is comma
115116
116-
-use-cdlf
117+
-use-crlf, -crlf
117118
: Force the line ending per row to carage return and
118-
line feed if true, false use line feed
119+
line feed if true, false use line feed. Defaults to true
120+
on Windows, false otherwise.
119121
120122
-sql FILENAME
121123
: Read sql statement from a file instead of the command line.
@@ -156,7 +158,7 @@ func main() {
156158
sqlCfg := new(datatools.SQLCfg)
157159
sqlCfg.Delimiter = ","
158160
sqlCfg.WriteHeaderRow = true
159-
sqlCfg.UseCRLF = false
161+
sqlCfg.UseCRLF = (runtime.GOOS == "windows")
160162

161163
// Running details
162164
appName := path.Base(os.Args[0])
@@ -177,6 +179,7 @@ func main() {
177179
flag.BoolVar(&showVersion, "version", showVersion, "display version")
178180
flag.BoolVar(&writeHeaderRow, "header-row", writeHeaderRow, "write a header row if true")
179181
flag.BoolVar(&useCRLF, "use-crlf", useCRLF, "delimited rows with a carriage return and line feed")
182+
flag.BoolVar(&useCRLF, "crlf", useCRLF, "delimited rows with a carriage return and line feed")
180183
flag.StringVar(&dsn, "dsn", dsn, "connect using the data source name provided in URL form")
181184
flag.StringVar(&delimiter, "delimiter", "", "set the delimiter, defaults to comma")
182185
flag.StringVar(&sqlFName, "sql", "", "read the SQL statement from a file, '-' will cause a read from standard input")

0 commit comments

Comments
 (0)