@@ -6,8 +6,10 @@ use std::path::PathBuf;
66
77/// `clusterdb` clusters all previously clustered tables in a database.
88#[ derive( Clone , Debug , Default ) ]
9+ #[ allow( clippy:: struct_excessive_bools) ]
910pub struct ClusterDbBuilder {
1011 program_dir : Option < PathBuf > ,
12+ envs : Vec < ( OsString , OsString ) > ,
1113 all : bool ,
1214 dbname : Option < OsString > ,
1315 echo : bool ,
@@ -26,12 +28,13 @@ pub struct ClusterDbBuilder {
2628}
2729
2830impl ClusterDbBuilder {
29- /// Create a new [ClusterDbBuilder]
31+ /// Create a new [`ClusterDbBuilder`]
32+ #[ must_use]
3033 pub fn new ( ) -> Self {
3134 Self :: default ( )
3235 }
3336
34- /// Create a new [ClusterDbBuilder] from [Settings]
37+ /// Create a new [` ClusterDbBuilder` ] from [Settings]
3538 pub fn from ( settings : & dyn Settings ) -> Self {
3639 Self :: new ( )
3740 . program_dir ( settings. get_binary_dir ( ) )
@@ -42,96 +45,112 @@ impl ClusterDbBuilder {
4245 }
4346
4447 /// Location of the program binary
48+ #[ must_use]
4549 fn program_dir < P : Into < PathBuf > > ( mut self , path : P ) -> Self {
4650 self . program_dir = Some ( path. into ( ) ) ;
4751 self
4852 }
4953
5054 /// Cluster all databases
55+ #[ must_use]
5156 pub fn all ( mut self ) -> Self {
5257 self . all = true ;
5358 self
5459 }
5560
5661 /// Database to cluster
62+ #[ must_use]
5763 pub fn dbname < S : AsRef < OsStr > > ( mut self , dbname : S ) -> Self {
5864 self . dbname = Some ( dbname. as_ref ( ) . to_os_string ( ) ) ;
5965 self
6066 }
6167
6268 /// Show the commands being sent to the server
69+ #[ must_use]
6370 pub fn echo ( mut self ) -> Self {
6471 self . echo = true ;
6572 self
6673 }
6774
6875 /// Don't write any messages
76+ #[ must_use]
6977 pub fn quiet ( mut self ) -> Self {
7078 self . quiet = true ;
7179 self
7280 }
7381
7482 /// Cluster specific table(s) only
83+ #[ must_use]
7584 pub fn table < S : AsRef < OsStr > > ( mut self , table : S ) -> Self {
7685 self . table = Some ( table. as_ref ( ) . to_os_string ( ) ) ;
7786 self
7887 }
7988
8089 /// Write a lot of output
90+ #[ must_use]
8191 pub fn verbose ( mut self ) -> Self {
8292 self . verbose = true ;
8393 self
8494 }
8595
8696 /// Output version information, then exit
97+ #[ must_use]
8798 pub fn version ( mut self ) -> Self {
8899 self . version = true ;
89100 self
90101 }
91102
92103 /// Show help, then exit
104+ #[ must_use]
93105 pub fn help ( mut self ) -> Self {
94106 self . help = true ;
95107 self
96108 }
97109
98110 /// Database server host or socket directory
111+ #[ must_use]
99112 pub fn host < S : AsRef < OsStr > > ( mut self , host : S ) -> Self {
100113 self . host = Some ( host. as_ref ( ) . to_os_string ( ) ) ;
101114 self
102115 }
103116
104117 /// Database server port
118+ #[ must_use]
105119 pub fn port ( mut self , port : u16 ) -> Self {
106120 self . port = Some ( port) ;
107121 self
108122 }
109123
110124 /// User name to connect as
125+ #[ must_use]
111126 pub fn username < S : AsRef < OsStr > > ( mut self , username : S ) -> Self {
112127 self . username = Some ( username. as_ref ( ) . to_os_string ( ) ) ;
113128 self
114129 }
115130
116131 /// Never prompt for password
132+ #[ must_use]
117133 pub fn no_password ( mut self ) -> Self {
118134 self . no_password = true ;
119135 self
120136 }
121137
122138 /// Force password prompt
139+ #[ must_use]
123140 pub fn password ( mut self ) -> Self {
124141 self . password = true ;
125142 self
126143 }
127144
128145 /// user password
146+ #[ must_use]
129147 pub fn pg_password < S : AsRef < OsStr > > ( mut self , pg_password : S ) -> Self {
130148 self . pg_password = Some ( pg_password. as_ref ( ) . to_os_string ( ) ) ;
131149 self
132150 }
133151
134152 /// Alternate maintenance database
153+ #[ must_use]
135154 pub fn maintenance_db < S : AsRef < OsStr > > ( mut self , db : S ) -> Self {
136155 self . maintenance_db = Some ( db. as_ref ( ) . to_os_string ( ) ) ;
137156 self
@@ -189,7 +208,7 @@ impl CommandBuilder for ClusterDbBuilder {
189208
190209 if let Some ( host) = & self . host {
191210 args. push ( "--host" . into ( ) ) ;
192- args. push ( host. into ( ) )
211+ args. push ( host. into ( ) ) ;
193212 }
194213
195214 if let Some ( port) = & self . port {
@@ -220,14 +239,21 @@ impl CommandBuilder for ClusterDbBuilder {
220239
221240 /// Get the environment variables for the command
222241 fn get_envs ( & self ) -> Vec < ( OsString , OsString ) > {
223- let mut envs: Vec < ( OsString , OsString ) > = Vec :: new ( ) ;
242+ let mut envs: Vec < ( OsString , OsString ) > = self . envs . clone ( ) ;
224243
225244 if let Some ( password) = & self . pg_password {
226245 envs. push ( ( "PGPASSWORD" . into ( ) , password. into ( ) ) ) ;
227246 }
228247
229248 envs
230249 }
250+
251+ /// Set an environment variable for the command
252+ fn env < S : AsRef < OsStr > > ( mut self , key : S , value : S ) -> Self {
253+ self . envs
254+ . push ( ( key. as_ref ( ) . to_os_string ( ) , value. as_ref ( ) . to_os_string ( ) ) ) ;
255+ self
256+ }
231257}
232258
233259#[ cfg( test) ]
@@ -258,6 +284,7 @@ mod tests {
258284 #[ test]
259285 fn test_builder ( ) {
260286 let command = ClusterDbBuilder :: new ( )
287+ . env ( "PGDATABASE" , "database" )
261288 . all ( )
262289 . dbname ( "dbname" )
263290 . echo ( )
@@ -276,7 +303,7 @@ mod tests {
276303 . build ( ) ;
277304
278305 assert_eq ! (
279- r#"PGPASSWORD="password" "clusterdb" "--all" "--dbname" "dbname" "--echo" "--quiet" "--table" "table" "--verbose" "--version" "--help" "--host" "localhost" "--port" "5432" "--username" "postgres" "--no-password" "--password" "--maintenance-db" "postgres""# ,
306+ r#"PGDATABASE="database" PGPASSWORD="password" "clusterdb" "--all" "--dbname" "dbname" "--echo" "--quiet" "--table" "table" "--verbose" "--version" "--help" "--host" "localhost" "--port" "5432" "--username" "postgres" "--no-password" "--password" "--maintenance-db" "postgres""# ,
280307 command. to_command_string( )
281308 ) ;
282309 }
0 commit comments