8000 feat!: coalesce default dir computation across crates by twitchyliquid64 · Pull Request #619 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content
8000
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
4 changes: 2 additions & 2 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 crates/mctx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ publish.workspace = true
anyhow.workspace = true
check.workspace = true
codespan-reporting.workspace = true
dirs.workspace = true
google-cloud-auth.workspace = true
google-cloud-storage.workspace = true
toml.workspace = true
Expand All @@ -34,6 +33,7 @@ graph.workspace = true
mfile.workspace = true
op.workspace = true
ot.workspace = true
paths.workspace = true
orchestrator.workspace = true
stdlib.workspace = true

Expand Down
28 changes: 8 additions & 20 deletions crates/mctx/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,17 @@ impl ConfigBuilder {
.num_parallel_builds
.unwrap_or_else(common::default_parallelism),

// TODO: Update default to
//
// dirs::state_dir()
// .unwrap_or_else(|| dirs::home_dir().unwrap().join(".local/state"))
// .join("minimal")
//
// Once Minimal One is the primary surface.
minimal_state_dir: self.minimal_state_dir.unwrap_or_else(|| {
dirs::cache_dir()
.unwrap_or_else(|| PathBuf::from("~/.cache"))
.join("minimal")
paths::minimal_state_dir()
.as_utf8_path()
.as_std_path()
.to_path_buf()
}),
// TODO: Update default to
//
// dirs::cache_dir()
// .unwrap_or_else(|| dirs::home_dir().unwrap().join(".local/cache"))
// .join("minimal")
//
// Once Minimal One is the primary surface.
minimal_cache_dir: self.minimal_cache_dir.unwrap_or_else(|| {
dirs::cache_dir()
.unwrap_or_else(|| PathBuf::from("~/.cache"))
.join("minimal")
paths::minimal_cache_dir()
.as_utf8_path()
.as_std_path()
.to_path_buf()
}),
Comment thread
twitchyliquid64 marked this conversation as resolved.
Resolved
stdlib_dir: self.stdlib_dir,
repo_dir: self.repo_dir,
Expand Down
13 changes: 4 additions & 9 deletions crates/minimal/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,10 @@ pub fn resolve_socket_path(
if use_minvmd {
return minvmd::sock::resolve_uds_path();
}
let state = dirs::state_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".local/state")))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
"cannot determine state directory; set --minimal-dir",
)
})?;
Ok(state.join("minimal/providers/local-0/ssh.sock"))
Ok(paths::minimal_state_dir()
.as_utf8_path()
.as_std_path()
.join("providers/local-0/ssh.sock"))
Comment thread
twitchyliquid64 marked this conversation as resolved.
Resolved
}
#[cfg(target_os = "macos")]
minvmd::sock::resolve_uds_path()
Expand Down
1 change: 0 additions & 1 deletion crates/minimald/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ camino.workspace = true
clap.workspace = true
clap_complete.workspace = true
constcat.workspace = true
dirs.workspace = true
either.workspace = true
futures.workspace = true
hakoniwa.workspace = true
Expand Down
29 changes: 8 additions & 21 deletions crates/minimald/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! The minimal daemon, an SSH server which hosts sessions and
//! task/sandbox executions within them.

use camino::Utf8PathBuf;
use clap::{Args, CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use paths::{CwdRelative, Daemon, DaemonAbsPath, DaemonRelPath, sub_path};
Expand Down Expand Up @@ -35,33 +34,21 @@ impl Cli {
/// based on command-line arguments.
pub fn minimal_state_dir(&self) -> DaemonAbsPath {
match &self.global_args.minimal_state_dir {
Some(p) => p.resolve().unwrap(),
None => DaemonAbsPath::try_new(
Utf8PathBuf::from_path_buf(
dirs::state_dir()
.unwrap_or_else(|| dirs::home_dir().unwrap().join(".local/state"))
.join("minimal"),
)
.unwrap(),
)
.unwrap(),
Some(p) => p
.resolve()
.expect("could not resolve --minimal-state-dir against the current directory"),
None => paths::minimal_state_dir(),
}
}

/// Returns the path to base directory for caching
/// based on command-line arguments.
pub fn minimal_cache_dir(&self) -> DaemonAbsPath {
match &self.global_args.minimal_cache_dir {
Some(p) => p.resolve().unwrap(),
None => DaemonAbsPath::try_new(
Utf8PathBuf::from_path_buf(
dirs::cache_dir()
.unwrap_or_else(|| dirs::home_dir() 8 .unwrap( 10BC8 ).join(".local/cache"))
.join("minimal"),
)
.unwrap(),
)
.unwrap(),
Some(p) => p
.resolve()
.expect("could not resolve --minimal-cache-dir against the current directory"),
None => paths::minimal_cache_dir(),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/paths/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ publish.workspace = true

[dependencies]
camino.workspace = true
dirs.workspace = true
serde.workspace = true
thiserror.workspace = true

Expand Down
44 changes: 44 additions & 0 deletions crates/paths/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,50 @@ pub type DaemonPath = EitherPath<Daemon>;
/// [`RelPath::<ConfigRelative>::bind_to_host`].
pub type ConfigRelPath = RelPath<ConfigRelative>;

/// Returns minimal's default cache directory, `<cache>/minimal`.
///
/// The base is the platform cache directory (e.g. `$XDG_CACHE_HOME` or
/// `~/.cache` on Linux), falling back to `~/.cache` when it cannot be
/// determined.
///
/// # Panics
///
/// Panics if neither a cache directory nor a home directory can be resolved,
/// or if the resulting path is not valid UTF-8.
pub fn minimal_cache_dir() -> DaemonAbsPath {
default_dir(dirs::cache_dir, ".cache")
}

/// Returns minimal's default state directory, `<state>/minimal`.
///
/// The base is the platform state directory (e.g. `$XDG_STATE_HOME` or
/// `~/.local/state` on Linux), falling back to `~/.local/state` when it cannot
/// be determined.
///
/// # Panics
///
/// Panics if neither a state directory nor a home directory can be resolved,
/// or if the resulting path is not valid UTF-8.
pub fn minimal_state_dir() -> DaemonAbsPath {
default_dir(dirs::state_dir, ".local/state")
}

/// Computes `<base>/minimal`, where `base` comes from `base_dir` or, failing
/// that, `~/<home_fallback>`.
8161 fn default_dir(
base_dir: impl FnOnce() -> Option<std::path::PathBuf>,
home_fallback: &str,
) -> DaemonAbsPath {
let base = base_dir().unwrap_or_else(|| {
dirs::home_dir()
.expect("could not determine home directory")
.join(home_fallback)
});
let path = Utf8PathBuf::from_path_buf(base.join("minimal"))
.expect("default directory path is not valid UTF-8");
DaemonAbsPath::try_new(path).expect("default directory path is not absolute")
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/// Errors produced when constructing a path.
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
Expand Down
Loading
0