Skip to content

Commit ee17417

Browse files
committed
Connection memory
1 parent 3ae6a0d commit ee17417

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ repository = "https://github.com/KyGost/multisql-cli"
1010
[dependencies]
1111
dialoguer = "^0"
1212
indicatif = "^0"
13-
multisql = "0.0.3"
13+
multisql = "0.0.5"
1414
lazy_static = "^1"
1515
console = "^0"
16-
cli-table = "^0"
16+
cli-table = "^0"
17+
serde_json = "^1"
18+
dirs = "^4"

src/main.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use cli_table::{print_stdout, Cell, Table};
2-
use multisql::{Cast, Payload};
2+
use multisql::{Cast, Connection, Payload};
33
use {
44
dialoguer::{theme::ColorfulTheme, Input},
55
indicatif::{ProgressBar, ProgressStyle},
66
lazy_static::lazy_static,
77
multisql::Glue,
8+
std::{
9+
fs::OpenOptions,
10+
io::{prelude::*, SeekFrom},
11+
},
812
};
913
lazy_static! {
1014
pub(crate) static ref PROGRESS_STYLE: ProgressStyle = ProgressStyle::default_spinner()
@@ -13,8 +17,42 @@ lazy_static! {
1317
}
1418

1519
fn main() {
16-
let mut glue = Glue::new_multi(vec![]);
17-
while prompt(&mut glue) {}
20+
let mut connection_file_path = dirs::home_dir().unwrap();
21+
connection_file_path.push(".multisql-cli.json");
22+
23+
let mut connection_file = OpenOptions::new()
24+
.read(true)
25+
.write(true)
26+
.create(true)
27+
.open(&connection_file_path)
28+
.unwrap();
29+
let mut connection_json = String::new();
30+
connection_file
31+
.read_to_string(&mut connection_json)
32+
.unwrap();
33+
if connection_json == "" {
34+
connection_json = String::from("[]")
35+
};
36+
37+
let connections: Vec<(String, Connection)> = serde_json::from_str(&connection_json).unwrap();
38+
let databases = connections
39+
.into_iter()
40+
.map(|(name, connection)| (name, connection.try_into().unwrap()))
41+
.collect();
42+
43+
let mut glue = Glue::new_multi(databases);
44+
45+
prompt(&mut glue);
46+
47+
let connection_json = serde_json::to_string(&glue.into_connections()).unwrap();
48+
connection_file.set_len(0).unwrap();
49+
connection_file.seek(SeekFrom::Start(0)).unwrap();
50+
connection_file
51+
.write_all(&connection_json.into_bytes())
52+
.unwrap();
53+
connection_file.flush().unwrap();
54+
55+
main();
1856
}
1957

2058
fn prompt(glue: &mut Glue) -> bool {
@@ -46,7 +84,12 @@ fn prompt(glue: &mut Glue) -> bool {
4684
})
4785
.collect::<Vec<Vec<_>>>()
4886
.table()
49-
.title(labels.into_iter().map(|label| label.cell()).collect::<Vec<_>>());
87+
.title(
88+
labels
89+
.into_iter()
90+
.map(|label| label.cell())
91+
.collect::<Vec<_>>(),
92+
);
5093
print_stdout(table).unwrap()
5194
}
5295
Ok(payload) => println!("{:?}", payload),

0 commit comments

Comments
 (0)