Skip to content

Commit 35b7623

Browse files
committed
Better as a param
1 parent 96b3463 commit 35b7623

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

streambed-confidant-cli/src/main.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ struct DecryptCommand {
7575
#[clap(env, short, long)]
7676
pub file: PathBuf,
7777

78+
/// The max number utf-8 characters to read from the file.
79+
#[clap(env, long, default_value_t = 8 * 1024)]
80+
pub max_line_len: u64,
81+
7882
/// By default, records are output to STDOUT as a BASE64 values followed newlines.
7983
/// This option can be used to write to a file.
8084
#[clap(env, short, long)]
@@ -88,6 +92,10 @@ struct EncryptCommand {
8892
#[clap(env, short, long)]
8993
pub file: PathBuf,
9094

95+
/// The max number utf-8 characters to read from the file.
96+
#[clap(env, long, default_value_t = 8 * 1024)]
97+
pub max_line_len: u64,
98+
9199
/// By default, records are output to STDOUT as a BASE64 values followed newlines.
92100
/// This option can be used to write to a file.
93101
#[clap(env, short, long)]
@@ -148,9 +156,11 @@ async fn secret_store(
148156
type LineReaderFn = Box<dyn FnMut() -> Result<Option<String>, io::Error> + Send>;
149157
type OutputFn = Box<dyn Write + Send>;
150158

151-
const LINE_LIMIT: u64 = 8 * 1024;
152-
153-
fn pipeline(file: PathBuf, output: Option<PathBuf>) -> Result<(LineReaderFn, OutputFn), Errors> {
159+
fn pipeline(
160+
file: PathBuf,
161+
output: Option<PathBuf>,
162+
max_line_len: u64,
163+
) -> Result<(LineReaderFn, OutputFn), Errors> {
154164
// A line reader function is provided rather than something based on the
155165
// Read trait as reading a line from stdin involves locking it via its
156166
// own read_line function. A Read trait will cause a lock to occur for
@@ -162,14 +172,15 @@ fn pipeline(file: PathBuf, output: Option<PathBuf>) -> Result<(LineReaderFn, Out
162172
let mut line = String::new();
163173
Box::new(move || {
164174
line.clear();
165-
if stdin.lock().take(LINE_LIMIT).read_line(&mut line)? > 0 {
175+
if stdin.lock().take(max_line_len).read_line(&mut line)? > 0 {
166176
Ok(Some(line.clone()))
167177
} else {
168178
Ok(None)
169179
}
170180
})
171181
} else {
172-
let mut buf = BufReader::new(fs::File::open(file).map_err(Errors::from)?).take(LINE_LIMIT);
182+
let mut buf =
183+
BufReader::new(fs::File::open(file).map_err(Errors::from)?).take(max_line_len);
173184
let mut line = String::new();
174185
Box::new(move || {
175186
line.clear();
@@ -209,11 +220,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
209220
let task = tokio::spawn(async move {
210221
match args.command {
211222
Command::Decrypt(command) => {
212-
let (line_reader, output) = pipeline(command.file, command.output)?;
223+
let (line_reader, output) =
224+
pipeline(command.file, command.output, command.max_line_len)?;
213225
cryptor::decrypt(ss, line_reader, output).await
214226
}
215227
Command::Encrypt(command) => {
216-
let (line_reader, output) = pipeline(command.file, command.output)?;
228+
let (line_reader, output) =
229+
pipeline(command.file, command.output, command.max_line_len)?;
217230
cryptor::encrypt(ss, line_reader, output).await
218231
}
219232
}

streambed-logged-cli/src/main.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ struct ProduceCommand {
4545
/// The file to consume records from, or `-` to indicate STDIN.
4646
#[clap(env, short, long)]
4747
pub file: PathBuf,
48+
49+
/// The max number utf-8 characters to read from the file.
50+
#[clap(env, long, default_value_t = 8 * 1024)]
51+
pub max_line_len: u64,
4852
}
4953

5054
/// Subscribe to topics and consume from them producing JSON-line records to a stream.
@@ -155,8 +159,6 @@ fn parse_offset(arg: &str) -> Result<Offset, OffsetParseError> {
155159
type LineReaderFn = Box<dyn FnMut() -> Result<Option<String>, io::Error> + Send>;
156160
type OutputFn = Box<dyn Write + Send>;
157161

158-
const LINE_LIMIT: u64 = 8 * 1024;
159-
160162
#[tokio::main]
161163
async fn main() -> Result<(), Box<dyn Error>> {
162164
let args = ProgramArgs::parse();
@@ -179,7 +181,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
179181
let mut line = String::new();
180182
Box::new(move || {
181183
line.clear();
182-
if stdin.lock().take(LINE_LIMIT).read_line(&mut line)? > 0 {
184+
if stdin
185+
.lock()
186+
.take(command.max_line_len)
187+
.read_line(&mut line)?
188+
> 0
189+
{
183190
Ok(Some(line.clone()))
184191
} else {
185192
Ok(None)
@@ -188,7 +195,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
188195
} else {
189196
let mut buf =
190197
BufReader::new(fs::File::open(command.file).map_err(Errors::from)?)
191-
.take(LINE_LIMIT);
198+
.take(command.max_line_len);
192199
let mut line = String::new();
193200
Box::new(move || {
194201
line.clear();

0 commit comments

Comments
 (0)