@@ -6,8 +6,10 @@ use std::path::PathBuf;
6
6
7
7
/// `clusterdb` clusters all previously clustered tables in a database.
8
8
#[ derive( Clone , Debug , Default ) ]
9
+ #[ allow( clippy:: struct_excessive_bools) ]
9
10
pub struct ClusterDbBuilder {
10
11
program_dir : Option < PathBuf > ,
12
+ envs : Vec < ( OsString , OsString ) > ,
11
13
all : bool ,
12
14
dbname : Option < OsString > ,
13
15
echo : bool ,
@@ -26,12 +28,13 @@ pub struct ClusterDbBuilder {
26
28
}
27
29
28
30
impl ClusterDbBuilder {
29
- /// Create a new [ClusterDbBuilder]
31
+ /// Create a new [`ClusterDbBuilder`]
32
+ #[ must_use]
30
33
pub fn new ( ) -> Self {
31
34
Self :: default ( )
32
35
}
33
36
34
- /// Create a new [ClusterDbBuilder] from [Settings]
37
+ /// Create a new [` ClusterDbBuilder` ] from [Settings]
35
38
pub fn from ( settings : & dyn Settings ) -> Self {
36
39
Self :: new ( )
37
40
. program_dir ( settings. get_binary_dir ( ) )
@@ -42,96 +45,112 @@ impl ClusterDbBuilder {
42
45
}
43
46
44
47
/// Location of the program binary
48
+ #[ must_use]
45
49
fn program_dir < P : Into < PathBuf > > ( mut self , path : P ) -> Self {
46
50
self . program_dir = Some ( path. into ( ) ) ;
47
51
self
48
52
}
49
53
50
54
/// Cluster all databases
55
+ #[ must_use]
51
56
pub fn all ( mut self ) -> Self {
52
57
self . all = true ;
53
58
self
54
59
}
55
60
56
61
/// Database to cluster
62
+ #[ must_use]
57
63
pub fn dbname < S : AsRef < OsStr > > ( mut self , dbname : S ) -> Self {
58
64
self . dbname = Some ( dbname. as_ref ( ) . to_os_string ( ) ) ;
59
65
self
60
66
}
61
67
62
68
/// Show the commands being sent to the server
69
+ #[ must_use]
63
70
pub fn echo ( mut self ) -> Self {
64
71
self . echo = true ;
65
72
self
66
73
}
67
74
68
75
/// Don't write any messages
76
+ #[ must_use]
69
77
pub fn quiet ( mut self ) -> Self {
70
78
self . quiet = true ;
71
79
self
72
80
}
73
81
74
82
/// Cluster specific table(s) only
83
+ #[ must_use]
75
84
pub fn table < S : AsRef < OsStr > > ( mut self , table : S ) -> Self {
76
85
self . table = Some ( table. as_ref ( ) . to_os_string ( ) ) ;
77
86
self
78
87
}
79
88
80
89
/// Write a lot of output
90
+ #[ must_use]
81
91
pub fn verbose ( mut self ) -> Self {
82
92
self . verbose = true ;
83
93
self
84
94
}
85
95
86
96
/// Output version information, then exit
97
+ #[ must_use]
87
98
pub fn version ( mut self ) -> Self {
88
99
self . version = true ;
89
100
self
90
101
}
91
102
92
103
/// Show help, then exit
104
+ #[ must_use]
93
105
pub fn help ( mut self ) -> Self {
94
106
self . help = true ;
95
107
self
96
108
}
97
109
98
110
/// Database server host or socket directory
111
+ #[ must_use]
99
112
pub fn host < S : AsRef < OsStr > > ( mut self , host : S ) -> Self {
100
113
self . host = Some ( host. as_ref ( ) . to_os_string ( ) ) ;
101
114
self
102
115
}
103
116
104
117
/// Database server port
118
+ #[ must_use]
105
119
pub fn port ( mut self , port : u16 ) -> Self {
106
120
self . port = Some ( port) ;
107
121
self
108
122
}
109
123
110
124
/// User name to connect as
125
+ #[ must_use]
111
126
pub fn username < S : AsRef < OsStr > > ( mut self , username : S ) -> Self {
112
127
self . username = Some ( username. as_ref ( ) . to_os_string ( ) ) ;
113
128
self
114
129
}
115
130
116
131
/// Never prompt for password
132
+ #[ must_use]
117
133
pub fn no_password ( mut self ) -> Self {
118
134
self . no_password = true ;
119
135
self
120
136
}
121
137
122
138
/// Force password prompt
139
+ #[ must_use]
123
140
pub fn password ( mut self ) -> Self {
124
141
self . password = true ;
125
142
self
126
143
}
127
144
128
145
/// user password
146
+ #[ must_use]
129
147
pub fn pg_password < S : AsRef < OsStr > > ( mut self , pg_password : S ) -> Self {
130
148
self . pg_password = Some ( pg_password. as_ref ( ) . to_os_string ( ) ) ;
131
149
self
132
150
}
133
151
134
152
/// Alternate maintenance database
153
+ #[ must_use]
135
154
pub fn maintenance_db < S : AsRef < OsStr > > ( mut self , db : S ) -> Self {
136
155
self . maintenance_db = Some ( db. as_ref ( ) . to_os_string ( ) ) ;
137
156
self
@@ -189,7 +208,7 @@ impl CommandBuilder for ClusterDbBuilder {
189
208
190
209
if let Some ( host) = & self . host {
191
210
args. push ( "--host" . into ( ) ) ;
192
- args. push ( host. into ( ) )
211
+ args. push ( host. into ( ) ) ;
193
212
}
194
213
195
214
if let Some ( port) = & self . port {
@@ -220,14 +239,21 @@ impl CommandBuilder for ClusterDbBuilder {
220
239
221
240
/// Get the environment variables for the command
222
241
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 ( ) ;
224
243
225
244
if let Some ( password) = & self . pg_password {
226
245
envs. push ( ( "PGPASSWORD" . into ( ) , password. into ( ) ) ) ;
227
246
}
228
247
229
248
envs
230
249
}
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
+ }
231
257
}
232
258
233
259
#[ cfg( test) ]
@@ -258,6 +284,7 @@ mod tests {
258
284
#[ test]
259
285
fn test_builder ( ) {
260
286
let command = ClusterDbBuilder :: new ( )
287
+ . env ( "PGDATABASE" , "database" )
261
288
. all ( )
262
289
. dbname ( "dbname" )
263
290
. echo ( )
@@ -276,7 +303,7 @@ mod tests {
276
303
. build ( ) ;
277
304
278
305
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""# ,
280
307
command. to_command_string( )
281
308
) ;
282
309
}
0 commit comments