@@ -77,6 +77,26 @@ impl TomlOrJson {
7777 } ;
7878 }
7979
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+
80100 fn to_string ( & self ) -> Result < String > {
81101 Ok ( match self {
82102 TomlOrJson :: Toml ( t) => t. to_string ( ) ,
@@ -236,9 +256,18 @@ pub fn command(options: Options) -> Result<()> {
236256 }
237257
238258 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+ }
242271 }
243272
244273 Ok ( ( ) )
0 commit comments