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

set_project_dirs to switch the directory where containers are stored #103

Merged
merged 1 commit into from
Apr 11, 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
4 changes: 2 additions & 2 deletions ocipkg/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub enum Error {
//
// System error
//
#[error("No valid home directory path could be retrieved from the operating system.")]
NoValidHomeDirecotry,
#[error("Project directory is tried to set twice")]
ProjectDirectoryAlreadySet,
#[error("No valid runtime directory where authentication info will be stored.")]
NoValidRuntimeDirectory,
#[error(transparent)]
Expand Down
18 changes: 15 additions & 3 deletions ocipkg/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ use crate::{
ImageName,
};
use directories::ProjectDirs;
use std::path::*;
use std::{path::*, sync::OnceLock};

pub const PROJECT_NAME: &str = "ocipkg";
pub const DEFAULT_PROJECT_NAME: &str = "ocipkg";

static PROJECT_DIRS: OnceLock<ProjectDirs> = OnceLock::new();

pub fn set_project_dirs(dirs: ProjectDirs) -> Result<()> {
PROJECT_DIRS
.set(dirs)
.map_err(|_| Error::ProjectDirectoryAlreadySet)
}

/// Project root data directory
pub fn data_dir() -> Result<PathBuf> {
let p = ProjectDirs::from("", PROJECT_NAME, PROJECT_NAME).ok_or(Error::NoValidHomeDirecotry)?;
// FIXME: use `get_or_try_init` after it is stabilized
let p = PROJECT_DIRS.get_or_init(|| {
ProjectDirs::from("", DEFAULT_PROJECT_NAME, DEFAULT_PROJECT_NAME)
.expect("No valid home directory")
});
let dir = p.data_dir();
Ok(dir.to_owned())
}
Expand Down
Loading