@@ -3,8 +3,8 @@ mod read_csv;
3
3
mod record_type;
4
4
5
5
use crate :: { generate_xml:: generate_xml, read_csv:: CsvSource , record_type:: RecordType } ;
6
+ use flate2:: { bufread:: GzDecoder , GzBuilder } ;
6
7
use indicatif:: { ProgressBar , ProgressStyle } ;
7
- use libflate:: gzip;
8
8
use quicli:: prelude:: * ;
9
9
use std:: { fs:: File , io, path:: Path } ;
10
10
use structopt:: StructOpt ;
@@ -19,17 +19,17 @@ struct Cli {
19
19
#[ structopt( ) ]
20
20
category : String ,
21
21
/// Path to input file. If ommited STDIN is used for input.
22
- #[ structopt( long = "input" , short = "i" , parse( from_os_str) ) ]
22
+ #[ structopt( long, short = "i" , parse( from_os_str) ) ]
23
23
input : Option < std:: path:: PathBuf > ,
24
24
/// Path to output file. If ommited output is written to STDOUT.
25
- #[ structopt( long = "output" , short = "o" , parse( from_os_str) ) ]
25
+ #[ structopt( long, short = "o" , parse( from_os_str) ) ]
26
26
output : Option < std:: path:: PathBuf > ,
27
27
/// Record type of generated XML. Should be either Record, DeleteRecord, DeleteAllRecords.
28
28
#[ structopt( long = "record-type" , short = "r" , default_value = "Record" ) ]
29
29
record_type : RecordType ,
30
30
/// Character used as delimiter between csv columns. While this tool assumes utf8 encoding,
31
31
/// only ASCII delimiters are supported.
32
- #[ structopt( long = "delimiter" , short = "d" , default_value = "," ) ]
32
+ #[ structopt( long, short = "d" , default_value = "," ) ]
33
33
delimiter : char ,
34
34
}
35
35
@@ -46,6 +46,12 @@ fn main() -> CliResult {
46
46
let input: Box < dyn io:: Read > = if let Some ( input) = args. input {
47
47
// Path argument specified. Open file and initialize progress bar.
48
48
let file = File :: open ( & input) ?;
49
+ // Only show Progress bar, if both input and output are files.
50
+ //
51
+ // * We need the input to so we have the file metadata and therefore file length, to know
52
+ // the amount of data we are going to proccess. Otherwise we can't set the length of the
53
+ // progress bar.
54
+ // * We don't want the Progress bar to interfere with the output, if writing to stdout.
49
55
// Progress bar interferes with formatting if stdout and stderr both go to console
50
56
if args. output . is_some ( ) {
51
57
let len = file. metadata ( ) ?. len ( ) ;
@@ -59,20 +65,22 @@ fn main() -> CliResult {
59
65
let file_with_pbar = progress_bar. wrap_read ( file) ;
60
66
61
67
if has_gz_extension ( & input) {
62
- Box :: new ( gzip :: Decoder :: new ( file_with_pbar) ? )
68
+ Box :: new ( GzDecoder :: new ( io :: BufReader :: new ( file_with_pbar) ) )
63
69
} else {
64
70
Box :: new ( file_with_pbar)
65
71
}
66
72
} else {
73
+ // Input file, but writing to stdout
74
+
67
75
// Repeat if to avoid extra Box.
68
76
if has_gz_extension ( & input) {
69
- Box :: new ( gzip :: Decoder :: new ( file) ? )
77
+ Box :: new ( GzDecoder :: new ( io :: BufReader :: new ( file) ) )
70
78
} else {
71
79
Box :: new ( file)
72
80
}
73
81
}
74
82
} else {
75
- // just use stdin
83
+ // Input path not set => Just use stdin
76
84
Box :: new ( io:: stdin ( ) )
77
85
} ;
78
86
@@ -82,7 +90,7 @@ fn main() -> CliResult {
82
90
let writer = io:: BufWriter :: new ( File :: create ( & output) ?) ;
83
91
84
92
if has_gz_extension ( & output) {
85
- Box :: new ( gzip :: Encoder :: new ( writer) ? )
93
+ Box :: new ( GzBuilder :: new ( ) . write ( writer, Default :: default ( ) ) )
86
94
} else {
87
95
Box :: new ( writer)
88
96
}
0 commit comments