Skip to content

Commit c5c65f5

Browse files
Add support for ~/.datafusionrc and cli option for overriding it to datafusion-cli (#1875)
* Add new option to run and keep df open * Rename options and clippy * Move to .datafusionrc * Clippy
1 parent 447b40d commit c5c65f5

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

datafusion-cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ datafusion = { path = "../datafusion", version = "7.0.0" }
3535
arrow = { version = "9.1" }
3636
ballista = { path = "../ballista/rust/client", version = "0.6.0", optional=true }
3737
env_logger = "0.9"
38-
mimalloc = { version = "*", default-features = false }
38+
mimalloc = { version = "*", default-features = false }
39+
dirs = "4.0.0"

datafusion-cli/src/exec.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ pub async fn exec_from_lines(
7272
}
7373
}
7474

75+
pub async fn exec_from_files(
76+
files: Vec<String>,
77+
ctx: &mut Context,
78+
print_options: &PrintOptions,
79+
) {
80+
let files = files
81+
.into_iter()
82+
.map(|file_path| File::open(file_path).unwrap())
83+
.collect::<Vec<_>>();
84+
for file in files {
85+
let mut reader = BufReader::new(file);
86+
exec_from_lines(ctx, &mut reader, print_options).await;
87+
}
88+
}
89+
7590
/// run and execute SQL statements and commands against a context with the given print options
7691
pub async fn exec_from_repl(ctx: &mut Context, print_options: &mut PrintOptions) {
7792
let mut rl = Editor::<CliHelper>::new();

datafusion-cli/src/main.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ use datafusion_cli::{
2424
};
2525
use mimalloc::MiMalloc;
2626
use std::env;
27-
use std::fs::File;
28-
use std::io::BufReader;
2927
use std::path::Path;
3028

3129
#[global_allocator]
@@ -59,6 +57,16 @@ struct Args {
5957
)]
6058
file: Vec<String>,
6159

60+
#[clap(
61+
short = 'r',
62+
long,
63+
multiple_values = true,
64+
help = "Run the provided files on startup instead of ~/.datafusionrc",
65+
validator(is_valid_file),
66+
conflicts_with = "file"
67+
)]
68+
rc: Option<Vec<String>>,
69+
6270
#[clap(long, arg_enum, default_value_t = PrintFormat::Table)]
6371
format: PrintFormat,
6472

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

109117
let files = args.file;
110-
if !files.is_empty() {
111-
let files = files
112-
.into_iter()
113-
.map(|file_path| File::open(file_path).unwrap())
114-
.collect::<Vec<_>>();
115-
for file in files {
116-
let mut reader = BufReader::new(file);
117-
exec::exec_from_lines(&mut ctx, &mut reader, &print_options).await;
118+
let rc = match args.rc {
119+
Some(file) => file,
120+
None => {
121+
let mut files = Vec::new();
122+
let home = dirs::home_dir();
123+
if let Some(p) = home {
124+
let home_rc = p.join(".datafusionrc");
125+
if home_rc.exists() {
126+
files.push(home_rc.into_os_string().into_string().unwrap());
127+
}
128+
}
129+
files
118130
}
131+
};
132+
if !files.is_empty() {
133+
exec::exec_from_files(files, &mut ctx, &print_options).await
119134
} else {
135+
if !rc.is_empty() {
136+
exec::exec_from_files(rc, &mut ctx, &print_options).await
137+
}
120138
exec::exec_from_repl(&mut ctx, &mut print_options).await;
121139
}
122140

0 commit comments

Comments
 (0)