CLI: ensure distinction between REPL and non-REPL executions

next
Sayan Nandan 6 months ago
parent efa1540242
commit a1c55b2f16
No known key found for this signature in database
GPG Key ID: 0EBD769024B24F0A

@ -148,12 +148,12 @@ pub fn parse() -> CliResult<Task> {
}
};
let password = match args.remove("--password") {
Some(p) => p,
Some(p) => check_password(p, "cli arguments")?,
None => {
// let us check the environment variable to see if anything was set
match env::var(env_vars::SKYDB_PASSWORD) {
Ok(v) => v,
Err(_) => read_password("Enter password: ")?,
Ok(v) => check_password(v, "env")?,
Err(_) => check_password(read_password("Enter password: ")?, "env")?,
}
}
};
@ -169,6 +169,16 @@ pub fn parse() -> CliResult<Task> {
}
}
fn check_password(p: String, source: &str) -> CliResult<String> {
if p.is_empty() {
return Err(CliError::ArgsErr(format!(
"password value cannot be empty (currently set via {source})"
)));
} else {
Ok(p)
}
}
fn read_password(prompt: &str) -> Result<String, std::io::Error> {
print!("{prompt}");
io::stdout().flush()?;

@ -40,33 +40,44 @@ macro_rules! pprint {
}
}
pub fn format_response(resp: Response, print_special: bool, pretty_format: bool) -> bool {
pub fn format_response(resp: Response, print_special: bool, in_repl: bool) -> bool {
match resp {
Response::Empty => pprint!(pretty_format, "(Okay)".cyan()),
Response::Empty => {
if in_repl {
println!("{}", "(Okay)".cyan())
}
// for empty responses outside the repl, it's equivalent to an exit 0, so we don't output anything to stdout or stderr
}
Response::Error(e) => {
if in_repl {
println!("{}", format!("(server error code: {e})").red());
} else {
// outside the repl, just write the error code to stderr. note, the query was technically "successful" because the server received it
// and responded to it. but on the application end, it was not. so, no need for a nonzero exit code
eprintln!("{e}");
}
return false;
}
Response::Value(v) => {
print_value(v, print_special, pretty_format);
print_value(v, print_special, in_repl);
println!();
}
Response::Row(r) => {
print_row(r, pretty_format);
print_row(r, in_repl);
println!();
}
Response::Rows(rows) => {
if rows.is_empty() {
pprint!(pretty_format, "[0 rows returned]".grey().italic());
pprint!(in_repl, "[0 rows returned]".grey().italic());
} else {
for (i, row) in rows.into_iter().enumerate().map(|(i, r)| (i + 1, r)) {
if pretty_format {
if in_repl {
let fmt = format!("({i})").grey().italic();
print!("{fmt}")
} else {
print!("({i})")
}
print_row(row, pretty_format);
print_row(row, in_repl);
println!();
}
}

Loading…
Cancel
Save