Skip to content

Commit

Permalink
fix: Removes use of dirs in favor of etcetera
Browse files Browse the repository at this point in the history
The `dirs` crate doesn't actually respect XDG vars, so this swaps out to
etcetera. This also has the side effect of removing a transitive dep that
had an MPL licenses (which can cause headaches for CNCF and other
projects).

Also adds a global path for the file cache

Fixes #111
Fixes #98

Signed-off-by: Taylor Thomas <[email protected]>
  • Loading branch information
thomastaylor312 committed Oct 11, 2024
1 parent 6c7ee21 commit 1d54a17
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 15 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ license = "Apache-2.0 WITH LLVM-exception"
anyhow = "1"
base64 = "0.22.0"
bytes = "1.7"
dirs = "5.0.1"
docker_credential = "1.2.1"
etcetera = "0.8"
futures-util = "0.3.30"
oci-client = { version = "0.12", default-features = false, features = [
"rustls-tls",
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ The default location is `$XDG_CONFIG_HOME/wasm-pkg/config.toml` on unix-like sys
`{FOLDERID_RoamingAppData}\wasm-pkg\config.toml` on Windows but this can be overridden with the
`--config` flag. Examples of this are found below:

| Platform | Path |
| -------- | ----------------------------------------------- |
| Linux | `/home/<username>/.config` |
| macOS | `/Users/<username>/Library/Application Support` |
| Windows | `C:\Users\<username>\AppData\Roaming` |
| Platform | Path |
| -------- | ------------------------------------- |
| Linux | `/home/<username>/.config` |
| macOS | `/home/<username>/.config` |
| Windows | `C:\Users\<username>\AppData\Roaming` |

The configuration file is TOML and can be edited manually.

Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-pkg-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ anyhow = { workspace = true }
async-trait = "0.1.77"
base64 = { workspace = true }
bytes = { workspace = true }
dirs = { workspace = true }
docker_credential = { workspace = true }
etcetera = { workspace = true }
futures-util = { workspace = true, features = ["io"] }
oci-client = { workspace = true }
oci-wasm = { workspace = true }
Expand Down
15 changes: 15 additions & 0 deletions crates/wasm-pkg-client/src/caching/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::path::{Path, PathBuf};

use anyhow::Context;
use etcetera::BaseStrategy;
use futures_util::{StreamExt, TryStreamExt};
use tokio_util::io::{ReaderStream, StreamReader};
use wasm_pkg_common::{
Expand All @@ -20,6 +21,7 @@ pub struct FileCache {
}

impl FileCache {
/// Creates a new file cache that stores data in the given directory.
pub async fn new(root: impl AsRef<Path>) -> anyhow::Result<Self> {
tokio::fs::create_dir_all(&root)
.await
Expand All @@ -28,6 +30,19 @@ impl FileCache {
root: root.as_ref().to_path_buf(),
})
}

/// Returns a cache setup to use the global default cache path if it can be determined,
/// otherwise this will error
pub async fn global_cache() -> anyhow::Result<Self> {
Self::new(Self::global_cache_path().context("couldn't find global cache path")?).await
}

/// Returns the global default cache path if it can be determined, otherwise returns None
pub fn global_cache_path() -> Option<PathBuf> {
etcetera::choose_base_strategy()
.ok()
.map(|strat| strat.cache_dir().join("wasm-pkg"))
}
}

#[derive(serde::Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-pkg-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tokio = ["tokio/io-util"]
[dependencies]
anyhow = { workspace = true }
bytes = { workspace = true }
dirs = { workspace = true }
etcetera = { workspace = true }
futures-util = { workspace = true }
http = "1.1.0"
reqwest = { version = "0.12.0", default-features = false, features = [
Expand Down
5 changes: 4 additions & 1 deletion crates/wasm-pkg-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
path::{Path, PathBuf},
};

use etcetera::BaseStrategy;
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -104,7 +105,9 @@ impl Config {

/// Returns the default global config file location
pub fn global_config_path() -> Option<PathBuf> {
dirs::config_dir().map(|path| path.join("wasm-pkg").join("config.toml"))
etcetera::choose_base_strategy()
.ok()
.map(|strat| strat.config_dir().join("wasm-pkg").join("config.toml"))
}

/// Reads config from the default global config file location
Expand Down
1 change: 0 additions & 1 deletion crates/wkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ _local = []
[dependencies]
anyhow = { workspace = true }
clap = { version = "4.5", features = ["derive", "wrap_help", "env"] }
dirs = { workspace = true }
docker_credential = { workspace = true }
futures-util = { workspace = true, features = ["io"] }
oci-client = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions crates/wkg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ impl Common {
let dir = if let Some(dir) = self.cache.as_ref() {
dir.clone()
} else {
dirs::cache_dir().context("unable to find cache directory")?
FileCache::global_cache_path().context("unable to find cache directory")?
};
let dir = dir.join("wkg");
FileCache::new(dir).await
}

Expand Down

0 comments on commit 1d54a17

Please sign in to comment.