@@ -77,6 +77,26 @@ impl TomlOrJson {
77
77
} ;
78
78
}
79
79
80
+ fn has_permission ( & self , identifier : & str ) -> bool {
81
+ ( || {
82
+ Some ( match self {
83
+ TomlOrJson :: Toml ( t) => t
84
+ . get ( "permissions" ) ?
85
+ . as_array ( ) ?
86
+ . iter ( )
87
+ . any ( |value| value. as_str ( ) == Some ( identifier) ) ,
88
+
89
+ TomlOrJson :: Json ( j) => j
90
+ . as_object ( ) ?
91
+ . get ( "permissions" ) ?
92
+ . as_array ( ) ?
93
+ . iter ( )
94
+ . any ( |value| value. as_str ( ) == Some ( identifier) ) ,
95
+ } )
96
+ } ) ( )
97
+ . unwrap_or_default ( )
98
+ }
99
+
80
100
fn to_string ( & self ) -> Result < String > {
81
101
Ok ( match self {
82
102
TomlOrJson :: Toml ( t) => t. to_string ( ) ,
@@ -236,9 +256,18 @@ pub fn command(options: Options) -> Result<()> {
236
256
}
237
257
238
258
for ( capability, path) in & mut capabilities {
239
- capability. insert_permission ( options. identifier . clone ( ) ) ;
240
- std:: fs:: write ( & * path, capability. to_string ( ) ?) ?;
241
- log:: info!( action = "Added" ; "permission `{}` to `{}` at {}" , options. identifier, capability. identifier( ) , dunce:: simplified( path) . display( ) ) ;
259
+ if capability. has_permission ( & options. identifier ) {
260
+ log:: info!(
261
+ "Permission `{}` already found in `{}` at {}" ,
262
+ options. identifier,
263
+ capability. identifier( ) ,
264
+ dunce:: simplified( path) . display( )
265
+ ) ;
266
+ } else {
267
+ capability. insert_permission ( options. identifier . clone ( ) ) ;
268
+ std:: fs:: write ( & * path, capability. to_string ( ) ?) ?;
269
+ log:: info!( action = "Added" ; "permission `{}` to `{}` at {}" , options. identifier, capability. identifier( ) , dunce:: simplified( path) . display( ) ) ;
270
+ }
242
271
}
243
272
244
273
Ok ( ( ) )
0 commit comments