11use clap:: { Arg , ArgAction , Command } ;
2+ use std:: path:: PathBuf ;
23use std:: process:: exit;
34
5+ #[ derive( Debug , Clone ) ]
6+ pub enum QuerySource {
7+ Query ( String ) ,
8+ File ( PathBuf ) ,
9+ Stdin ,
10+ Interactive ,
11+ }
12+
413pub struct DatabaseConfig {
514 pub driver_name : String ,
615 pub uri : Option < String > ,
716 pub username : Option < String > ,
817 pub password : Option < String > ,
918 pub options : Vec < ( String , String ) > ,
19+ pub query_source : QuerySource ,
20+ }
21+
22+ fn is_stdin_piped ( ) -> bool {
23+ use std:: io:: IsTerminal ;
24+ !std:: io:: stdin ( ) . is_terminal ( )
1025}
1126
1227pub fn parse_args ( ) -> DatabaseConfig {
@@ -28,6 +43,14 @@ pub fn parse_args() -> DatabaseConfig {
2843 . long ( "option" )
2944 . help ( "Driver-specific database option" )
3045 . action ( ArgAction :: Append ) ,
46+ Arg :: new ( "query" )
47+ . long ( "query" )
48+ . help ( "Execute query and exit" )
49+ . conflicts_with ( "file" ) ,
50+ Arg :: new ( "file" )
51+ . long ( "file" )
52+ . help ( "Read and execute file and exit" )
53+ . conflicts_with ( "query" ) ,
3154 ] ;
3255 let command = Command :: new ( "adbcli" )
3356 . version ( env ! ( "CARGO_PKG_VERSION" ) )
@@ -61,12 +84,23 @@ pub fn parse_args() -> DatabaseConfig {
6184 }
6285 }
6386
87+ let query_source = if let Some ( query) = matches. get_one :: < String > ( "query" ) {
88+ QuerySource :: Query ( query. clone ( ) )
89+ } else if let Some ( file) = matches. get_one :: < String > ( "file" ) {
90+ QuerySource :: File ( PathBuf :: from ( file) )
91+ } else if is_stdin_piped ( ) {
92+ QuerySource :: Stdin
93+ } else {
94+ QuerySource :: Interactive
95+ } ;
96+
6497 DatabaseConfig {
6598 driver_name,
6699 uri,
67100 username,
68101 password,
69102 options,
103+ query_source,
70104 }
71105}
72106
@@ -128,6 +162,7 @@ mod tests {
128162 username : Some ( "test_user" . to_string ( ) ) ,
129163 password : Some ( "test_pass" . to_string ( ) ) ,
130164 options : vec ! [ ( "key1" . to_string( ) , "val1" . to_string( ) ) ] ,
165+ query_source : QuerySource :: Interactive ,
131166 } ;
132167
133168 assert_eq ! ( config. driver_name, "test_driver" ) ;
@@ -146,6 +181,7 @@ mod tests {
146181 username : None ,
147182 password : None ,
148183 options : vec ! [ ] ,
184+ query_source : QuerySource :: Interactive ,
149185 } ;
150186
151187 assert_eq ! ( config. driver_name, "test_driver" ) ;
0 commit comments