@@ -2,7 +2,7 @@ use std::{process::Command, sync::Arc};
22
33use bytes:: Bytes ;
44use chrono:: { DateTime , Local } ;
5- use log:: { debug, warn } ;
5+ use log:: debug;
66
77use crate :: {
88 CommandInput , ErrorKind , RusticError , RusticResult ,
@@ -67,7 +67,7 @@ impl ReadBackend for LockBackend {
6767 }
6868}
6969
70- fn path ( tpe : FileType , id : & Id ) -> String {
70+ fn path_to_id_from_file_type ( tpe : FileType , id : & Id ) -> String {
7171 let hex_id = id. to_hex ( ) ;
7272 match tpe {
7373 FileType :: Config => "config" . into ( ) ,
@@ -94,15 +94,26 @@ impl WriteBackend for LockBackend {
9494 }
9595
9696 fn lock ( & self , tpe : FileType , id : & Id , until : Option < DateTime < Local > > ) -> RusticResult < ( ) > {
97+ if !self . can_lock ( ) {
98+ return Err ( RusticError :: new (
99+ ErrorKind :: Backend ,
100+ "No locking configured on backend." ,
101+ ) ) ;
102+ }
103+
97104 let until = until. map_or_else ( String :: new, |u| u. to_rfc3339 ( ) ) ;
98- let path = path ( tpe, id) ;
105+
106+ let path = path_to_id_from_file_type ( tpe, id) ;
107+
99108 let args = self . command . args ( ) . iter ( ) . map ( |c| {
100109 c. replace ( "%id" , & id. to_hex ( ) )
101110 . replace ( "%type" , tpe. dirname ( ) )
102111 . replace ( "%path" , & path)
103112 . replace ( "%until" , & until)
104113 } ) ;
114+
105115 debug ! ( "calling {:?}..." , self . command) ;
116+
106117 let status = Command :: new ( self . command . command ( ) )
107118 . args ( args)
108119 . status ( )
@@ -115,9 +126,17 @@ impl WriteBackend for LockBackend {
115126 . attach_context ( "tpe" , tpe. to_string ( ) )
116127 . attach_context ( "id" , id. to_string ( ) )
117128 } ) ?;
129+
118130 if !status. success ( ) {
119- warn ! ( "lock command was not successful for {tpe:?}, id: {id}. {status}" ) ;
131+ return Err ( RusticError :: new (
132+ ErrorKind :: Backend ,
133+ "lock command was not successful for {tpe}, id: {id}. {status}" ,
134+ )
135+ . attach_context ( "tpe" , tpe. to_string ( ) )
136+ . attach_context ( "id" , id. to_string ( ) )
137+ . attach_context ( "status" , status. to_string ( ) ) ) ;
120138 }
139+
121140 Ok ( ( ) )
122141 }
123142}
0 commit comments