Skip to content

Commit

Permalink
fix(libscoop|config): default config path should be always returned
Browse files Browse the repository at this point in the history
and the portable config path should not be returned when the frontend
implementation is not located in the expected directory.

Signed-off-by: Chawye Hsu <[email protected]>
  • Loading branch information
chawyehsu committed Aug 16, 2023
1 parent 0a01f1e commit d3040ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 11 additions & 8 deletions crates/libscoop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Deref;
use std::path::{Path, PathBuf};

use crate::error::{Error, Fallible};
use crate::internal::fs::write_json;
use crate::internal;

/// Builder pattern for generating [`Config`].
pub struct ConfigBuilder {
Expand Down Expand Up @@ -205,7 +205,7 @@ impl Config {
pub(crate) fn init() -> Config {
let config = Config::default();
// try to write the default config to the default path, error is ignored
let _ = write_json(default::config_path(), &config.inner);
let _ = internal::fs::write_json(default::config_path(), &config.inner);
config
}

Expand Down Expand Up @@ -298,7 +298,7 @@ impl Config {

/// Commit config changes and save to the config file
pub(crate) fn commit(&self) -> Fallible<()> {
write_json(&self.path, &self.inner)
internal::fs::write_json(&self.path, &self.inner)
}

/// Pretty print the config
Expand Down Expand Up @@ -395,9 +395,12 @@ pub(crate) fn possible_config_paths() -> Vec<PathBuf> {
// <app>\<current>\<app_name>\apps\<root> (in theory)
// ^^^^
if path.pop() {
let check = internal::path::leaf(&path)
.map(|n| n == "apps")
.unwrap_or_default();
// <app>\<current>\<app_name>\apps\<root> (in theory)
// ^^^^^
if path.pop() {
if check && path.pop() {
path.push("config.json");

// 2) config.json located in the `root` directory of
Expand All @@ -407,12 +410,12 @@ pub(crate) fn possible_config_paths() -> Vec<PathBuf> {
}
}
}

// 3) config.json located in the XDG_CONFIG_HOME directory, i.e.,
// `~/.config/scoop/config.json`
ret.push(default::config_path());
}

// 3) config.json located in the XDG_CONFIG_HOME directory, i.e.,
// `~/.config/scoop/config.json`
ret.push(default::config_path());

ret
}

Expand Down
2 changes: 2 additions & 0 deletions crates/libscoop/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use flume::{Receiver, Sender};
use log::debug;
use once_cell::unsync::OnceCell;
use std::cell::{Ref, RefCell, RefMut};
use std::path::Path;
Expand Down Expand Up @@ -43,6 +44,7 @@ impl Session {
// Try to load config from the possible config paths, once a successful
// load is done, return the session immediately.
for path in possible_config_paths() {
debug!("Trying to load config from {}", path.display());
if let Ok(session) = Self::new_with(path) {
return session;
}
Expand Down

0 comments on commit d3040ad

Please sign in to comment.