@@ -17,7 +17,8 @@ use clap::{Arg, ArgAction, ArgMatches, Command};
17
17
use std:: fs;
18
18
use std:: os:: unix:: fs:: MetadataExt ;
19
19
20
- use uucore:: locale:: get_message;
20
+ use std:: collections:: HashMap ;
21
+ use uucore:: locale:: { get_message, get_message_with_args} ;
21
22
22
23
fn parse_gid_uid_and_filter ( matches : & ArgMatches ) -> UResult < GidUidOwnerFilter > {
23
24
let filter = if let Some ( spec) = matches. get_one :: < String > ( options:: FROM ) {
@@ -35,8 +36,12 @@ fn parse_gid_uid_and_filter(matches: &ArgMatches) -> UResult<GidUidOwnerFilter>
35
36
let dest_gid: Option < u32 > ;
36
37
let raw_owner: String ;
37
38
if let Some ( file) = matches. get_one :: < String > ( options:: REFERENCE ) {
38
- let meta = fs:: metadata ( file)
39
- . map_err_context ( || format ! ( "failed to get attributes of {}" , file. quote( ) ) ) ?;
39
+ let meta = fs:: metadata ( file) . map_err_context ( || {
40
+ get_message_with_args (
41
+ "chown-error-failed-to-get-attributes" ,
42
+ HashMap :: from ( [ ( "file" . to_string ( ) , file. quote ( ) . to_string ( ) ) ] ) ,
43
+ )
44
+ } ) ?;
40
45
let gid = meta. gid ( ) ;
41
46
let uid = meta. uid ( ) ;
42
47
dest_gid = Some ( gid) ;
@@ -84,56 +89,51 @@ pub fn uu_app() -> Command {
84
89
. arg (
85
90
Arg :: new ( options:: HELP )
86
91
. long ( options:: HELP )
87
- . help ( "Print help information." )
92
+ . help ( get_message ( "chown- help-print-help" ) )
88
93
. action ( ArgAction :: Help ) ,
89
94
)
90
95
. arg (
91
96
Arg :: new ( options:: verbosity:: CHANGES )
92
97
. short ( 'c' )
93
98
. long ( options:: verbosity:: CHANGES )
94
- . help ( "like verbose but report only when a change is made" )
99
+ . help ( get_message ( "chown-help-changes" ) )
95
100
. action ( ArgAction :: SetTrue ) ,
96
101
)
97
102
. arg (
98
103
Arg :: new ( options:: FROM )
99
104
. long ( options:: FROM )
100
- . help (
101
- "change the owner and/or group of each file only if its \
102
- current owner and/or group match those specified here. \
103
- Either may be omitted, in which case a match is not required \
104
- for the omitted attribute",
105
- )
105
+ . help ( get_message ( "chown-help-from" ) )
106
106
. value_name ( "CURRENT_OWNER:CURRENT_GROUP" ) ,
107
107
)
108
108
. arg (
109
109
Arg :: new ( options:: preserve_root:: PRESERVE )
110
110
. long ( options:: preserve_root:: PRESERVE )
111
- . help ( "fail to operate recursively on '/'" )
111
+ . help ( get_message ( "chown-help-preserve-root" ) )
112
112
. action ( ArgAction :: SetTrue ) ,
113
113
)
114
114
. arg (
115
115
Arg :: new ( options:: preserve_root:: NO_PRESERVE )
116
116
. long ( options:: preserve_root:: NO_PRESERVE )
117
- . help ( "do not treat '/' specially (the default)" )
117
+ . help ( get_message ( "chown-help-no-preserve-root" ) )
118
118
. action ( ArgAction :: SetTrue ) ,
119
119
)
120
120
. arg (
121
121
Arg :: new ( options:: verbosity:: QUIET )
122
122
. long ( options:: verbosity:: QUIET )
123
- . help ( "suppress most error messages" )
123
+ . help ( get_message ( "chown-help-quiet" ) )
124
124
. action ( ArgAction :: SetTrue ) ,
125
125
)
126
126
. arg (
127
127
Arg :: new ( options:: RECURSIVE )
128
128
. short ( 'R' )
129
129
. long ( options:: RECURSIVE )
130
- . help ( "operate on files and directories recursively" )
130
+ . help ( get_message ( "chown-help-recursive" ) )
131
131
. action ( ArgAction :: SetTrue ) ,
132
132
)
133
133
. arg (
134
134
Arg :: new ( options:: REFERENCE )
135
135
. long ( options:: REFERENCE )
136
- . help ( "use RFILE's owner and group rather than specifying OWNER:GROUP values" )
136
+ . help ( get_message ( "chown-help-reference" ) )
137
137
. value_name ( "RFILE" )
138
138
. value_hint ( clap:: ValueHint :: FilePath )
139
139
. num_args ( 1 ..) ,
@@ -148,7 +148,7 @@ pub fn uu_app() -> Command {
148
148
Arg :: new ( options:: verbosity:: VERBOSE )
149
149
. long ( options:: verbosity:: VERBOSE )
150
150
. short ( 'v' )
151
- . help ( "output a diagnostic for every file processed" )
151
+ . help ( get_message ( "chown-help-verbose" ) )
152
152
. action ( ArgAction :: SetTrue ) ,
153
153
)
154
154
// Add common arguments with chgrp, chown & chmod
@@ -177,7 +177,10 @@ fn parse_uid(user: &str, spec: &str, sep: char) -> UResult<Option<u32>> {
177
177
Ok ( uid) => Ok ( Some ( uid) ) ,
178
178
Err ( _) => Err ( USimpleError :: new (
179
179
1 ,
180
- format ! ( "invalid user: {}" , spec. quote( ) ) ,
180
+ get_message_with_args (
181
+ "chown-error-invalid-user" ,
182
+ HashMap :: from ( [ ( "user" . to_string ( ) , spec. quote ( ) . to_string ( ) ) ] ) ,
183
+ ) ,
181
184
) ) ,
182
185
}
183
186
}
@@ -196,7 +199,10 @@ fn parse_gid(group: &str, spec: &str) -> UResult<Option<u32>> {
196
199
Ok ( gid) => Ok ( Some ( gid) ) ,
197
200
Err ( _) => Err ( USimpleError :: new (
198
201
1 ,
199
- format ! ( "invalid group: {}" , spec. quote( ) ) ,
202
+ get_message_with_args (
203
+ "chown-error-invalid-group" ,
204
+ HashMap :: from ( [ ( "group" . to_string ( ) , spec. quote ( ) . to_string ( ) ) ] ) ,
205
+ ) ,
200
206
) ) ,
201
207
} ,
202
208
}
@@ -231,7 +237,10 @@ fn parse_spec(spec: &str, sep: char) -> UResult<(Option<u32>, Option<u32>)> {
231
237
// we should fail with an error
232
238
return Err ( USimpleError :: new (
233
239
1 ,
234
- format ! ( "invalid spec: {}" , spec. quote( ) ) ,
240
+ get_message_with_args (
241
+ "chown-error-invalid-spec" ,
242
+ HashMap :: from ( [ ( "spec" . to_string ( ) , spec. quote ( ) . to_string ( ) ) ] ) ,
243
+ ) ,
235
244
) ) ;
236
245
}
237
246
@@ -241,9 +250,15 @@ fn parse_spec(spec: &str, sep: char) -> UResult<(Option<u32>, Option<u32>)> {
241
250
#[ cfg( test) ]
242
251
mod test {
243
252
use super :: * ;
253
+ use std:: env;
254
+ use uucore:: locale;
244
255
245
256
#[ test]
246
257
fn test_parse_spec ( ) {
258
+ unsafe {
259
+ env:: set_var ( "LANG" , "C" ) ;
260
+ }
261
+ let _ = locale:: setup_localization ( "chown" ) ;
247
262
assert ! ( matches!( parse_spec( ":" , ':' ) , Ok ( ( None , None ) ) ) ) ;
248
263
assert ! ( matches!( parse_spec( "." , ':' ) , Ok ( ( None , None ) ) ) ) ;
249
264
assert ! ( matches!( parse_spec( "." , '.' ) , Ok ( ( None , None ) ) ) ) ;
0 commit comments