This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new c5c65f5  Add support for `~/.datafusionrc` and cli option for 
overriding it to datafusion-cli (#1875)
c5c65f5 is described below

commit c5c65f541ff015ef74618c0d940f1b7c3c16cf0c
Author: Matthew Turner <[email protected]>
AuthorDate: Thu Mar 3 05:39:46 2022 -0500

    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
---
 datafusion-cli/Cargo.toml  |  3 ++-
 datafusion-cli/src/exec.rs | 15 +++++++++++++++
 datafusion-cli/src/main.rs | 38 ++++++++++++++++++++++++++++----------
 3 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/datafusion-cli/Cargo.toml b/datafusion-cli/Cargo.toml
index dab0d68..6dae9d4 100644
--- a/datafusion-cli/Cargo.toml
+++ b/datafusion-cli/Cargo.toml
@@ -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 }
\ No newline at end of file
+mimalloc = { version = "*", default-features = false }
+dirs = "4.0.0"
diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rs
index 17b329b..dc3c73e 100644
--- a/datafusion-cli/src/exec.rs
+++ b/datafusion-cli/src/exec.rs
@@ -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();
diff --git a/datafusion-cli/src/main.rs b/datafusion-cli/src/main.rs
index 1d91f13..08878f9 100644
--- a/datafusion-cli/src/main.rs
+++ b/datafusion-cli/src/main.rs
@@ -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]
@@ -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,
 
@@ -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;
     }
 

Reply via email to