Skip to content

Add support for ~/.datafusionrc and cli option for overriding it to datafusion-cli #1875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion datafusion-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ datafusion = { path = "../datafusion", version = "7.0.0" }
arrow = { version = "9.1" }
ballista = { path = "../ballista/rust/client", version = "0.6.0", optional=true }
env_logger = "0.9"
mimalloc = { version = "*", default-features = false }
mimalloc = { version = "*", default-features = false }
dirs = "4.0.0"
15 changes: 15 additions & 0 deletions datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ pub async fn exec_from_lines(
}
}

pub async fn exec_from_files(
files: Vec<String>,
ctx: &mut Context,
print_options: &PrintOptions,
) {
let files = files
.into_iter()
.map(|file_path| File::open(file_path).unwrap())
.collect::<Vec<_>>();
for file in files {
let mut reader = BufReader::new(file);
exec_from_lines(ctx, &mut reader, print_options).await;
}
}

/// run and execute SQL statements and commands against a context with the given print options
pub async fn exec_from_repl(ctx: &mut Context, print_options: &mut PrintOptions) {
let mut rl = Editor::<CliHelper>::new();
Expand Down
38 changes: 28 additions & 10 deletions datafusion-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ use datafusion_cli::{
};
use mimalloc::MiMalloc;
use std::env;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

#[global_allocator]
Expand Down Expand Up @@ -59,6 +57,16 @@ struct Args {
)]
file: Vec<String>,

#[clap(
short = 'r',
long,
multiple_values = true,
help = "Run the provided files on startup instead of ~/.datafusionrc",
validator(is_valid_file),
conflicts_with = "file"
)]
rc: Option<Vec<String>>,

#[clap(long, arg_enum, default_value_t = PrintFormat::Table)]
format: PrintFormat,

Expand Down Expand Up @@ -107,16 +115,26 @@ pub async fn main() -> Result<()> {
};

let files = args.file;
if !files.is_empty() {
let files = files
.into_iter()
.map(|file_path| File::open(file_path).unwrap())
.collect::<Vec<_>>();
for file in files {
let mut reader = BufReader::new(file);
exec::exec_from_lines(&mut ctx, &mut reader, &print_options).await;
let rc = match args.rc {
Some(file) => file,
None => {
let mut files = Vec::new();
let home = dirs::home_dir();
if let Some(p) = home {
let home_rc = p.join(".datafusionrc");
if home_rc.exists() {
files.push(home_rc.into_os_string().into_string().unwrap());
}
}
files
}
};
if !files.is_empty() {
exec::exec_from_files(files, &mut ctx, &print_options).await
} else {
if !rc.is_empty() {
exec::exec_from_files(rc, &mut ctx, &print_options).await
}
exec::exec_from_repl(&mut ctx, &mut print_options).await;
}

Expand Down