Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: expand tilde ~/paths in repository paths #1056

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/spfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
serde_qs = "0.10.1"
shellexpand = { workspace = true }
spfs-encoding = { workspace = true }
spfs-proto = { path = "../spfs-proto", features = ["serde"] }
strum = { workspace = true, features = ["derive"] }
Expand Down
18 changes: 18 additions & 0 deletions crates/spfs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,27 @@ impl Default for User {
}
}

/// Expand tilde ~/paths and deserialize into a PathBuf.
pub(crate) mod pathbuf_deserialize_with_tilde_expansion {
use serde::{Deserialize, Deserializer};

pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<std::path::PathBuf, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
if value.starts_with('~') {
let expanded = shellexpand::tilde(&value);
return Ok(std::path::PathBuf::from(expanded.as_ref()));
}
Ok(std::path::PathBuf::from(value))
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default)]
pub struct Storage {
#[serde(deserialize_with = "pathbuf_deserialize_with_tilde_expansion::deserialize")]
pub root: PathBuf,
/// If true, when rendering payloads, allow hard links even if the payload
/// is owned by a different user than the current user. Only applies to
Expand Down
47 changes: 46 additions & 1 deletion crates/spfs/src/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use rstest::rstest;

use super::{Config, RemoteConfig};
use super::{Config, Remote, RemoteConfig, RepositoryConfig};
use crate::storage::prelude::*;
use crate::storage::RepositoryHandle;
use crate::{get_config, load_config};
Expand Down Expand Up @@ -73,6 +73,51 @@ fn test_remote_config_or_address(#[case] source: &str) {
let _config: Config = serde_json::from_str(source).expect("config should have loaded properly");
}

#[rstest]
fn test_config_expands_tilde_in_paths() {
let source = r#"
{
"storage": {
"root": "~/root"
},
"remote": {
"fs": {
"scheme": "fs",
"path": "~/path"
},
"tar": {
"scheme": "tar",
"path": "~/tar"
}
}
}"#;
let config: Config = serde_json::from_str(source).expect("config is valid");
assert!(!config.storage.root.to_string_lossy().starts_with('~'));
assert!(config.storage.root.is_absolute());

let remote = config.remote.get("fs").expect("fs remote should exist");
let path = match remote {
Remote::Config(remote_config) => match &remote_config.inner {
RepositoryConfig::Fs(fs_config) => &fs_config.path,
_ => panic!("Not a RepositoryConfig::Fs"),
},
_ => panic!("Missing configuration"),
};
assert!(!path.to_string_lossy().starts_with('~'));
assert!(path.is_absolute());

let remote = config.remote.get("tar").expect("tar remote should exist");
let path = match remote {
Remote::Config(remote_config) => match &remote_config.inner {
RepositoryConfig::Tar(tar_config) => &tar_config.path,
_ => panic!("Not a RepositoryConfig::Fs"),
},
_ => panic!("Missing configuration"),
};
assert!(!path.to_string_lossy().starts_with('~'));
assert!(path.is_absolute());
}

#[rstest]
fn test_make_current_updates_config() {
let config1 = Config::default();
Expand Down
3 changes: 2 additions & 1 deletion crates/spfs/src/storage/fs/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use arc_swap::ArcSwap;
use super::hash_store::PROXY_DIRNAME;
use super::migrations::{MigrationError, MigrationResult};
use super::FsHashStore;
use crate::config::ToAddress;
use crate::config::{pathbuf_deserialize_with_tilde_expansion, ToAddress};
use crate::runtime::makedirs_with_perms;
use crate::storage::prelude::*;
use crate::storage::{
Expand All @@ -35,6 +35,7 @@ pub const DURABLE_EDITS_DIR: &str = "durable_edits";
/// Configuration for an fs repository
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Config {
#[serde(deserialize_with = "pathbuf_deserialize_with_tilde_expansion::deserialize")]
pub path: std::path::PathBuf,
#[serde(flatten)]
pub params: Params,
Expand Down
3 changes: 2 additions & 1 deletion crates/spfs/src/storage/tar/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use futures::Stream;
use relative_path::RelativePath;
use tar::{Archive, Builder};

use crate::config::ToAddress;
use crate::config::{pathbuf_deserialize_with_tilde_expansion, ToAddress};
use crate::graph::ObjectProto;
use crate::prelude::*;
use crate::storage::fs::DURABLE_EDITS_DIR;
Expand All @@ -32,6 +32,7 @@ use crate::{encoding, graph, storage, tracking, Error, Result};
/// Configuration for a tar repository
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Config {
#[serde(deserialize_with = "pathbuf_deserialize_with_tilde_expansion::deserialize")]
pub path: std::path::PathBuf,
}

Expand Down