Skip to content

Commit

Permalink
error logging on getting saved_state from the config
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj authored and FlorentinDUBOIS committed Jul 13, 2022
1 parent bbd4f5f commit 78272fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
5 changes: 3 additions & 2 deletions bin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,12 @@ pub fn start(
})
.detach();

let saved_state = config.saved_state_path();
let saved_state_path = config.saved_state.clone();

let mut server = CommandServer::new(fd, config, tx, command_rx, workers, accept_cancel_tx)?;
server.load_static_application_configuration().await;

if let Some(path) = saved_state {
if let Some(path) = saved_state_path {
server
.load_state(None, "INITIALIZATION".to_string(), &path)
.await?;
Expand Down
62 changes: 45 additions & 17 deletions command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,14 @@ fn default_accept_queue_timeout() -> u32 {
}

impl Config {
pub fn load_from_path(path: &str) -> io::Result<Config> {
FileConfig::load_from_path(path).map(|config| config.into(path))
pub fn load_from_path(path: &str) -> anyhow::Result<Config> {
let mut config = FileConfig::load_from_path(path).map(|config| config.into(path))?;

config.saved_state = config
.saved_state_path()
.with_context(|| "Invalid saved_state in the config. Check your config file")?;

Ok(config)
}

pub fn generate_config_messages(&self) -> Vec<CommandRequest> {
Expand Down Expand Up @@ -1283,22 +1289,44 @@ impl Config {
.with_context(|| "could not parse command socket path")
}

pub fn saved_state_path(&self) -> Option<String> {
self.saved_state.as_ref().and_then(|path| {
let config_path_buf = PathBuf::from(self.config_path.clone());
let config_folder = config_path_buf
.parent()
.expect("could not get parent folder of configuration file");
let mut saved_state_path_raw = config_folder.to_path_buf();
saved_state_path_raw.push(path);
pub fn saved_state_path(&self) -> anyhow::Result<Option<String>> {
let path = match self.saved_state.as_ref() {
Some(path) => path,
None => return Ok(None),
};

debug!("saved_stated path in the config: {}", path);

let config_path_buf = PathBuf::from(self.config_path.clone());
debug!("Config path buffer: {:?}", config_path_buf);

let config_folder = config_path_buf
.parent()
.with_context(|| "could not get parent folder of configuration file")?;

debug!("Config folder: {:?}", config_folder);

let mut saved_state_path_raw = config_folder.to_path_buf();

saved_state_path_raw.push(path);
debug!(
"Looking for saved state on the path {:?}",
saved_state_path_raw
.canonicalize()
.map_err(|e| {
error!("could not get saved state path: {}", e);
})
.ok()
.and_then(|path| path.to_str().map(|s| s.to_string()))
})
);

saved_state_path_raw.canonicalize().with_context(|| {
format!(
"could not get saved state path from config file input {:?}",
self.saved_state
)
})?;

let stringified_path = saved_state_path_raw
.to_str()
.ok_or(anyhow::Error::msg("Unvalid UTF8"))?
.to_string();

return Ok(Some(stringified_path));
}

pub fn load_file(path: &str) -> io::Result<String> {
Expand Down

0 comments on commit 78272fc

Please sign in to comment.