Skip to content

Commit 1d54a17

Browse files
fix: Removes use of dirs in favor of etcetera
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 <taylor@cosmonic.com>
1 parent 6c7ee21 commit 1d54a17

File tree

9 files changed

+30
-15
lines changed

9 files changed

+30
-15
lines changed

Cargo.lock

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ license = "Apache-2.0 WITH LLVM-exception"
1212
anyhow = "1"
1313
base64 = "0.22.0"
1414
bytes = "1.7"
15-
dirs = "5.0.1"
1615
docker_credential = "1.2.1"
16+
etcetera = "0.8"
1717
futures-util = "0.3.30"
1818
oci-client = { version = "0.12", default-features = false, features = [
1919
"rustls-tls",

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ The default location is `$XDG_CONFIG_HOME/wasm-pkg/config.toml` on unix-like sys
5858
`{FOLDERID_RoamingAppData}\wasm-pkg\config.toml` on Windows but this can be overridden with the
5959
`--config` flag. Examples of this are found below:
6060

61-
| Platform | Path |
62-
| -------- | ----------------------------------------------- |
63-
| Linux | `/home/<username>/.config` |
64-
| macOS | `/Users/<username>/Library/Application Support` |
65-
| Windows | `C:\Users\<username>\AppData\Roaming` |
61+
| Platform | Path |
62+
| -------- | ------------------------------------- |
63+
| Linux | `/home/<username>/.config` |
64+
| macOS | `/home/<username>/.config` |
65+
| Windows | `C:\Users\<username>\AppData\Roaming` |
6666

6767
The configuration file is TOML and can be edited manually.
6868

crates/wasm-pkg-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ anyhow = { workspace = true }
1818
async-trait = "0.1.77"
1919
base64 = { workspace = true }
2020
bytes = { workspace = true }
21-
dirs = { workspace = true }
2221
docker_credential = { workspace = true }
22+
etcetera = { workspace = true }
2323
futures-util = { workspace = true, features = ["io"] }
2424
oci-client = { workspace = true }
2525
oci-wasm = { workspace = true }

crates/wasm-pkg-client/src/caching/file.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::path::{Path, PathBuf};
44

55
use anyhow::Context;
6+
use etcetera::BaseStrategy;
67
use futures_util::{StreamExt, TryStreamExt};
78
use tokio_util::io::{ReaderStream, StreamReader};
89
use wasm_pkg_common::{
@@ -20,6 +21,7 @@ pub struct FileCache {
2021
}
2122

2223
impl FileCache {
24+
/// Creates a new file cache that stores data in the given directory.
2325
pub async fn new(root: impl AsRef<Path>) -> anyhow::Result<Self> {
2426
tokio::fs::create_dir_all(&root)
2527
.await
@@ -28,6 +30,19 @@ impl FileCache {
2830
root: root.as_ref().to_path_buf(),
2931
})
3032
}
33+
34+
/// Returns a cache setup to use the global default cache path if it can be determined,
35+
/// otherwise this will error
36+
pub async fn global_cache() -> anyhow::Result<Self> {
37+
Self::new(Self::global_cache_path().context("couldn't find global cache path")?).await
38+
}
39+
40+
/// Returns the global default cache path if it can be determined, otherwise returns None
41+
pub fn global_cache_path() -> Option<PathBuf> {
42+
etcetera::choose_base_strategy()
43+
.ok()
44+
.map(|strat| strat.cache_dir().join("wasm-pkg"))
45+
}
3146
}
3247

3348
#[derive(serde::Serialize)]

crates/wasm-pkg-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tokio = ["tokio/io-util"]
1515
[dependencies]
1616
anyhow = { workspace = true }
1717
bytes = { workspace = true }
18-
dirs = { workspace = true }
18+
etcetera = { workspace = true }
1919
futures-util = { workspace = true }
2020
http = "1.1.0"
2121
reqwest = { version = "0.12.0", default-features = false, features = [

crates/wasm-pkg-common/src/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
path::{Path, PathBuf},
55
};
66

7+
use etcetera::BaseStrategy;
78
use serde::{Deserialize, Serialize};
89

910
use crate::{
@@ -104,7 +105,9 @@ impl Config {
104105

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

110113
/// Reads config from the default global config file location

crates/wkg/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ _local = []
1616
[dependencies]
1717
anyhow = { workspace = true }
1818
clap = { version = "4.5", features = ["derive", "wrap_help", "env"] }
19-
dirs = { workspace = true }
2019
docker_credential = { workspace = true }
2120
futures-util = { workspace = true, features = ["io"] }
2221
oci-client = { workspace = true }

crates/wkg/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ impl Common {
6464
let dir = if let Some(dir) = self.cache.as_ref() {
6565
dir.clone()
6666
} else {
67-
dirs::cache_dir().context("unable to find cache directory")?
67+
FileCache::global_cache_path().context("unable to find cache directory")?
6868
};
69-
let dir = dir.join("wkg");
7069
FileCache::new(dir).await
7170
}
7271

0 commit comments

Comments
 (0)