11use cli_table:: { print_stdout, Cell , Table } ;
2- use multisql:: { Cast , Payload } ;
2+ use multisql:: { Cast , Connection , Payload } ;
33use {
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} ;
913lazy_static ! {
1014 pub ( crate ) static ref PROGRESS_STYLE : ProgressStyle = ProgressStyle :: default_spinner( )
@@ -13,8 +17,42 @@ lazy_static! {
1317}
1418
1519fn 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
2058fn 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