1use clap;
2use std::fs;
3use std::path::{Path, PathBuf};
4use thiserror::Error;
5
6#[derive(clap::Parser, Debug, Default)]
7pub struct Config {
8 #[clap(long = "phpup-dir", env = "PHPUP_DIR")]
10 base_dir: Option<PathBuf>,
11
12 #[clap(long, env = "PHPUP_MULTISHELL_PATH", hide = true)]
14 multishell_path: Option<PathBuf>,
15}
16
17#[derive(Error, Debug)]
18pub enum Error {
19 #[error("Not yet initialized; Need to run `eval \"$(phpup init)\"`")]
20 NoMultiShellPath,
21}
22
23impl Config {
24 pub fn multishell_path(&self) -> Result<&Path, Error> {
25 self.multishell_path
26 .as_deref()
27 .ok_or(Error::NoMultiShellPath)
28 }
29 pub fn base_dir(&self) -> PathBuf {
30 if let Some(base_dir) = self.base_dir.as_ref() {
31 base_dir.clone()
32 } else {
33 dirs::home_dir()
34 .expect("Can't get home directory")
35 .join(".phpup")
36 }
37 }
38 pub fn versions_dir(&self) -> PathBuf {
39 let versions_dir = self.base_dir().join("versions").join("php");
40 fs::create_dir_all(&versions_dir)
41 .unwrap_or_else(|_| panic!("Can't create version dirctory: {:?}", versions_dir));
42 versions_dir
43 }
44 pub fn aliases_dir(&self) -> PathBuf {
45 let aliases_dir = self.base_dir().join("aliases");
46 fs::create_dir_all(&aliases_dir)
47 .unwrap_or_else(|_| panic!("Can't create alias dirctory: {:?}", aliases_dir));
48 aliases_dir
49 }
50
51 #[cfg(test)]
52 pub fn with_base_dir(mut self, base_dir: impl AsRef<std::path::Path>) -> Self {
53 self.base_dir = Some(PathBuf::from(base_dir.as_ref()));
54 self
55 }
56}