@@ -3,7 +3,7 @@ use std::{process::Command, sync::Arc};
33use anyhow:: Result ;
44use bytes:: Bytes ;
55use chrono:: { DateTime , Local } ;
6- use log:: { debug, warn } ;
6+ use log:: { debug, error } ;
77
88use crate :: {
99 backend:: { FileType , ReadBackend , WriteBackend } ,
@@ -68,7 +68,7 @@ impl ReadBackend for LockBackend {
6868 }
6969}
7070
71- fn path ( tpe : FileType , id : & Id ) -> String {
71+ fn path_to_id_from_file_type ( tpe : FileType , id : & Id ) -> String {
7272 let hex_id = id. to_hex ( ) ;
7373 match tpe {
7474 FileType :: Config => "config" . into ( ) ,
@@ -95,19 +95,33 @@ impl WriteBackend for LockBackend {
9595 }
9696
9797 fn lock ( & self , tpe : FileType , id : & Id , until : Option < DateTime < Local > > ) -> Result < ( ) > {
98+ if !self . can_lock ( ) {
99+ return Err ( anyhow:: anyhow!( "No locking configured." ) ) ;
100+ }
101+
98102 let until = until. map_or_else ( String :: new, |u| u. to_rfc3339 ( ) ) ;
99- let path = path ( tpe, id) ;
103+
104+ let path = path_to_id_from_file_type ( tpe, id) ;
105+
100106 let args = self . command . args ( ) . iter ( ) . map ( |c| {
101107 c. replace ( "%id" , & id. to_hex ( ) )
102108 . replace ( "%type" , tpe. dirname ( ) )
103109 . replace ( "%path" , & path)
104110 . replace ( "%until" , & until)
105111 } ) ;
112+
106113 debug ! ( "calling {:?}..." , self . command) ;
114+
107115 let status = Command :: new ( self . command . command ( ) ) . args ( args) . status ( ) ?;
116+
108117 if !status. success ( ) {
109- warn ! ( "lock command was not successful for {tpe:?}, id: {id}. {status}" ) ;
118+ error ! ( "lock command was not successful for {tpe:?}, id: {id}. {status}" ) ;
119+
120+ return Err ( anyhow:: anyhow!(
121+ "lock command was not successful for {tpe:?}, id: {id}. {status}"
122+ ) ) ;
110123 }
124+
111125 Ok ( ( ) )
112126 }
113127}
0 commit comments