Skip to content

Commit

Permalink
feat(libscoop|config): support SCOOP_CACHE and SCOOP_GLOBAL envs
Browse files Browse the repository at this point in the history
Note: global apps are not supported by libscoop currently.

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
  • Loading branch information
chawyehsu committed Jul 26, 2023
1 parent 0520ae8 commit cf2a2a5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 56 deletions.
71 changes: 46 additions & 25 deletions crates/libscoop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ pub struct ConfigInner {
#[serde(skip_serializing_if = "Option::is_none")]
gh_token: Option<String>,

/// The global path
#[serde(alias = "globalPath")]
#[serde(default = "default::global_path")]
#[serde(skip_serializing_if = "default::is_default_global_path")]
Expand All @@ -141,12 +140,11 @@ pub struct ConfigInner {

#[serde(skip_serializing_if = "Option::is_none")]
proxy: Option<String>,
/// This is the root directory of a Scoop installation, by default the value
/// is `$HOME/scoop`.

#[serde(alias = "rootPath")]
#[serde(default = "default::root_path")]
#[serde(skip_serializing_if = "default::is_default_root_path")]
pub root_path: PathBuf,
root_path: PathBuf,

#[serde(skip_serializing_if = "Option::is_none")]
scoop_branch: Option<String>,
Expand All @@ -166,29 +164,40 @@ pub struct ConfigInner {
}

impl Config {
/// Initialize the config file with default values.
pub fn init() -> Config {
/// Initialize the config with default values.
///
/// This function will try to write the default config to the default path,
/// located in the XDG_CONFIG_HOME directory.
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);
config
}

/// Get the `cache` directory of Scoop.
#[inline]
pub fn cache_path(&self) -> &Path {
self.cache_path.as_path()
}

/// Get the root directory of Scoop.
///
/// This is the root directory of a Scoop installation, by default the value
/// is `$HOME/scoop`. It may be changed by setting the `SCOOP` environment
/// variable.
#[inline]
pub fn root_path(&self) -> &Path {
self.root_path.as_path()
}

/// Get the `proxy` setting.
#[inline]
pub fn proxy(&self) -> Option<&str> {
self.proxy.as_deref()
}

/// Get the `cat_style` setting.
#[inline]
pub fn cat_style(&self) -> &str {
self.cat_style.as_deref().unwrap_or_default()
Expand Down Expand Up @@ -376,46 +385,58 @@ mod default {
/// Join the given `path` to `$HOME` and return a new [`PathBuf`].
#[inline]
fn home_join<P: AsRef<Path>>(path: P) -> PathBuf {
dirs::home_dir()
.map(|p| normalize_path(p.join(path.as_ref())))
.unwrap()
}

/// Check if the given `path` is equal to the `default` one.
#[inline]
fn is_default(default: &Path, path: &Path) -> bool {
path.eq(default)
dirs::home_dir().map(|p| p.join(path.as_ref())).unwrap()
}

/// Get the default Scoop config path: `$HOME/.config/scoop/config.json`.
#[inline]
pub(super) fn config_path() -> PathBuf {
home_join(".config/scoop/config.json")
normalize_path(home_join(".config/scoop/config.json"))
}

/// Get the default Scoop root path.
#[inline]
pub(super) fn root_path() -> PathBuf {
if let Some(path) = std::env::var_os("SCOOP") {
return PathBuf::from(path);
}
let path = if let Some(path) = std::env::var_os("SCOOP") {
PathBuf::from(path)
} else {
home_join("scoop")
};

home_join("scoop")
normalize_path(path)
}

/// Get the default Scoop cache path.
#[inline]
pub(super) fn cache_path() -> PathBuf {
root_path().join("cache")
let path = if let Some(path) = std::env::var_os("SCOOP_CACHE") {
PathBuf::from(path)
} else {
root_path().join("cache")
};

normalize_path(path)
}

/// Get the default Scoop global path.
#[inline]
pub(super) fn global_path() -> PathBuf {
std::env::var_os("ProgramData")
.map(PathBuf::from)
.map(|p| p.join("scoop"))
.unwrap_or(PathBuf::from("C:\\ProgramData\\scoop"))
let path = if let Some(path) = std::env::var_os("SCOOP_GLOBAL") {
return PathBuf::from(path);
} else {
std::env::var_os("ProgramData")
.map(PathBuf::from)
.map(|p| p.join("scoop"))
.unwrap_or(PathBuf::from("C:/ProgramData/scoop"))
};

normalize_path(path)
}

/// Check if the given `path` is equal to the `default` one.
#[inline]
fn is_default(default: &Path, path: &Path) -> bool {
path.eq(default)
}

/// Check if the given `path` is equal to the `default` Scoop root path.
Expand Down
56 changes: 28 additions & 28 deletions crates/libscoop/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn bucket_add(session: &Session, name: &str, remote_url: &str) -> Fallible<(
/// I/O errors will be returned if the `buckets` directory is not readable.
pub fn bucket_list(session: &Session) -> Fallible<Vec<Bucket>> {
let mut buckets = vec![];
let buckets_dir = session.config().root_path.join("buckets");
let buckets_dir = session.config().root_path().join("buckets");

if buckets_dir.exists() {
let entries = buckets_dir
Expand Down Expand Up @@ -226,34 +226,35 @@ pub fn cache_list(session: &Session, query: &str) -> Fallible<Vec<CacheFile>> {
let is_wildcard_query = query.eq("*") || query.is_empty();
let config = session.config();
let cache_dir = config.cache_path();
let mut entries = vec![];

internal::fs::ensure_dir(&cache_dir)?;

let entries = cache_dir
.read_dir()?
.filter_map(Result::ok)
.filter_map(|de| {
let is_file = de.file_type().unwrap().is_file();
if is_file {
if let Ok(item) = CacheFile::from(de.path()) {
if !is_wildcard_query {
let matched = item
.package_name()
.to_lowercase()
.contains(&query.to_lowercase());
if matched {
return Some(item);
} else {
return None;
if cache_dir.exists() {
entries = cache_dir
.read_dir()?
.filter_map(Result::ok)
.filter_map(|de| {
let is_file = de.file_type().unwrap().is_file();
if is_file {
if let Ok(item) = CacheFile::from(de.path()) {
if !is_wildcard_query {
let matched = item
.package_name()
.to_lowercase()
.contains(&query.to_lowercase());
if matched {
return Some(item);
} else {
return None;
}
}
}

return Some(item);
return Some(item);
}
}
}
None
})
.collect::<Vec<_>>();
None
})
.collect::<Vec<_>>();
}

Ok(entries)
}
Expand All @@ -268,8 +269,7 @@ pub fn cache_remove(session: &Session, query: &str) -> Fallible<()> {
match query {
"*" => {
let config = session.config();
let cache_dir = config.cache_path();
Ok(internal::fs::empty_dir(cache_dir)?)
Ok(internal::fs::empty_dir(config.cache_path())?)
}
query => {
let files = cache_list(session, query)?;
Expand Down Expand Up @@ -327,7 +327,7 @@ pub fn config_set(session: &Session, key: &str, value: &str) -> Fallible<()> {
///
/// [1]: crate::Error::PackageHoldBrokenInstall
pub fn package_hold(session: &Session, name: &str, flag: bool) -> Fallible<()> {
let mut path = session.config().root_path.clone();
let mut path = session.config().root_path().to_owned();
path.push("apps");
path.push(name);

Expand Down
4 changes: 2 additions & 2 deletions crates/libscoop/src/package/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub(crate) fn query_installed(
) -> Fallible<Vec<Package>> {
let is_explicit_mode = options.contains(&QueryOption::Explicit);
let is_wildcard_query = query.eq("*") || query.is_empty();
let root_path = session.config().root_path.clone();
let root_path = session.config().root_path().to_owned();
let apps_dir = root_path.join("apps");
// build matchers
let mut matcher: Vec<(Option<String>, Box<dyn Matcher + Send + Sync>)> = vec![];
Expand Down Expand Up @@ -271,7 +271,7 @@ pub(crate) fn query_synced(
let is_explicit_mode = options.contains(&QueryOption::Explicit);
let is_wildcard_query = query.eq("*") || query.is_empty();
let buckets = crate::operation::bucket_list(session)?;
let apps_dir = session.config().root_path.join("apps");
let apps_dir = session.config().root_path().join("apps");
// build matchers
let mut matcher: Vec<(Option<String>, Box<dyn Matcher + Send + Sync>)> = vec![];

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::Result;

pub fn cmd_cleanup(matches: &ArgMatches, session: &Session) -> Result<()> {
let config = session.config();
let apps_path = config.root_path.join("apps");
let apps_path = config.root_path().join("apps");
// let running_apps = running_apps(&apps_path);

let query = matches
Expand Down

0 comments on commit cf2a2a5

Please sign in to comment.