1
1
use cli_table:: { print_stdout, Cell , Table } ;
2
- use multisql:: { Cast , Payload } ;
2
+ use multisql:: { Cast , Connection , Payload } ;
3
3
use {
4
4
dialoguer:: { theme:: ColorfulTheme , Input } ,
5
5
indicatif:: { ProgressBar , ProgressStyle } ,
6
6
lazy_static:: lazy_static,
7
7
multisql:: Glue ,
8
+ std:: {
9
+ fs:: OpenOptions ,
10
+ io:: { prelude:: * , SeekFrom } ,
11
+ } ,
8
12
} ;
9
13
lazy_static ! {
10
14
pub ( crate ) static ref PROGRESS_STYLE : ProgressStyle = ProgressStyle :: default_spinner( )
@@ -13,8 +17,42 @@ lazy_static! {
13
17
}
14
18
15
19
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 ( ) ;
18
56
}
19
57
20
58
fn prompt ( glue : & mut Glue ) -> bool {
@@ -46,7 +84,12 @@ fn prompt(glue: &mut Glue) -> bool {
46
84
} )
47
85
. collect :: < Vec < Vec < _ > > > ( )
48
86
. 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
+ ) ;
50
93
print_stdout ( table) . unwrap ( )
51
94
}
52
95
Ok ( payload) => println ! ( "{:?}" , payload) ,
0 commit comments