Skip to content

Commit d950d8b

Browse files
authored
feat: support --file argument to execute SQL from a file (#38)
* feat: support --file argument to execute SQL from a file * docs: add --file usage to SKILL.md * chore: bump version to 0.7.0
1 parent 480265d commit d950d8b

5 files changed

Lines changed: 49 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repository = "https://github.com/sundy-li/arrow_cli"
77
edition = "2024"
88
license = "Apache-2.0"
99
name = "arrow_cli"
10-
version = "0.6.1"
10+
version = "0.7.0"
1111

1212

1313
[dependencies]

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Usage: arrow_cli [OPTIONS]
2323
2424
Options:
2525
-u, --user <USER> User name [default: root]
26-
-p, --password <PASSWORD> User password [default: ]
26+
-p, --password <PASSWORD> User password [default: ""]
2727
--host <HOST> Flight SQL Server host [default: 127.0.0.1]
2828
-P, --port <PORT> Flight SQL Server port [default: 4100]
2929
--tls
@@ -32,7 +32,9 @@ Options:
3232
--print-schema Print resultset schema
3333
--output <OUTPUT> Result output format [default: table] [possible values: table, json, csv, tsv, psv]
3434
-c, --command <COMMAND> Execute SQL command and exit
35+
-f, --file <FILE> Execute SQL commands from a file and exit
3536
-h, --help Print help
37+
-V, --version Print version
3638
```
3739

3840
## Examples
@@ -58,6 +60,19 @@ Options:
5860
{"number":2}
5961
```
6062

63+
### Execute SQL from a file
64+
65+
```bash
66+
❯ arrow_cli -h arch -u sundy -p abc --port 8900 --output table --file query.sql
67+
+-------------+
68+
| avg(number) |
69+
+-------------+
70+
| 4.5 |
71+
+-------------+
72+
73+
1 rows in set (tickets received in 0.036 sec, rows received in 0.036 sec)
74+
```
75+
6176
### StdIn pipe with CSV output
6277

6378
```bash

SKILL.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ Output:
3939
1 rows in set (tickets received in 0.008 sec, rows received in 0.012 sec)
4040
```
4141

42+
### Execute SQL from a file and exit
43+
44+
```bash
45+
arrow_cli --host localhost --port 8900 --user admin --password abc --file query.sql
46+
```
47+
48+
The file content is read as a single SQL statement and executed once, then the CLI exits.
49+
4250
### Pipe SQL through standard input
4351

4452
```bash

src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ struct Args {
5555
)]
5656
output: Output,
5757

58-
#[clap(short = 'c', long, help = "Execute SQL command and exit")]
58+
#[clap(
59+
short = 'c',
60+
long,
61+
help = "Execute SQL command and exit",
62+
conflicts_with = "file"
63+
)]
5964
command: Option<String>,
65+
66+
#[clap(short = 'f', long, help = "Execute SQL commands from a file and exit")]
67+
file: Option<String>,
6068
}
6169

6270
#[tokio::main]
@@ -67,7 +75,7 @@ pub async fn main() -> Result<(), FlightError> {
6775
// Authenticate
6876
let url = format!("{protocol}://{}:{}", args.host, args.port);
6977
let endpoint = endpoint(&args, url)?;
70-
let is_repl = atty::is(Stream::Stdin) && args.command.is_none();
78+
let is_repl = atty::is(Stream::Stdin) && args.command.is_none() && args.file.is_none();
7179
let mut session = session::Session::try_new(endpoint, is_repl, args).await?;
7280

7381
session.handle().await;

src/session.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ impl Session {
6262
} else if self.args.command.is_some() {
6363
let command = self.args.command.clone().unwrap();
6464
self.handle_command(&command).await;
65+
} else if self.args.file.is_some() {
66+
let file = self.args.file.clone().unwrap();
67+
self.handle_file(&file).await;
6568
} else {
6669
self.handle_stdin().await;
6770
}
@@ -144,6 +147,17 @@ impl Session {
144147
}
145148
}
146149

150+
pub async fn handle_file(&mut self, file: &str) {
151+
match std::fs::read_to_string(file) {
152+
Ok(query) => {
153+
self.handle_command(query.trim_end()).await;
154+
}
155+
Err(e) => {
156+
eprintln!("read file {file} err: {e}");
157+
}
158+
}
159+
}
160+
147161
pub async fn handle_stdin(&mut self) {
148162
let mut lines = std::io::stdin().lock().lines();
149163
while let Some(Ok(line)) = lines.next() {

0 commit comments

Comments
 (0)