1use super::{Command, Config};
2use crate::shell::{self, Shell};
3use crate::symlink;
4use crate::version;
5use crate::version::system;
6use crate::version::Alias;
7use crate::version::Local;
8use clap;
9use std::path::PathBuf;
10use thiserror::Error;
11
12#[derive(clap::Parser, Debug)]
13pub struct Init {
14 #[clap(long, possible_values(shell::available_shells()))]
16 shell: Option<Shell>,
17
18 #[clap(long, visible_alias = "auto")]
20 auto_switch: bool,
21
22 #[clap(flatten)]
23 version_file: version::File,
24}
25
26#[derive(Error, Debug)]
27pub enum Error {
28 #[error("Can't detect using shell: {0}; You may be using unsupported shell")]
29 UndetectedShell(#[from] shell::ShellDetectError),
30}
31
32impl Command for Init {
33 type Error = Error;
34
35 fn run(&self, config: &Config) -> Result<(), Error> {
36 let shell = self.shell.map_or_else(Shell::detect_shell, Ok)?;
37 let symlink = create_symlink();
38 if let Some(default_path) = default_path(config) {
39 symlink::link(&default_path, &symlink).expect("Can't create symlink!");
40 }
41
42 let mut eval_stmts = vec![
43 shell.set_env("PHPUP_MULTISHELL_PATH", symlink.display()),
44 shell.set_path(symlink),
45 ];
46 if self.auto_switch {
47 eval_stmts.push(shell.auto_switch_hook(&self.version_file))
48 }
49 if let Some(rehash) = shell.rehash() {
50 eval_stmts.push(rehash)
51 }
52 println!("{}", eval_stmts.join("\n"));
53 Ok(())
54 }
55}
56
57fn create_symlink() -> std::path::PathBuf {
58 let temp_dir = std::env::temp_dir().join("phpup");
59 std::fs::create_dir_all(&temp_dir).expect("Can't create tempdir!");
60
61 loop {
62 let symlink_path = temp_dir.join(generate_symlink_path());
63 if !symlink_path.exists() {
64 break symlink_path;
65 }
66 }
67}
68
69fn generate_symlink_path() -> PathBuf {
70 PathBuf::from(format!(
71 "{}_{}",
72 std::process::id(),
73 chrono::Utc::now().timestamp_millis(),
74 ))
75}
76
77fn default_path(config: &Config) -> Option<PathBuf> {
78 if let Ok(Local::Installed(version)) = Alias::default().resolve(config.aliases_dir()) {
79 if let Some(installed) = version::latest_installed_by(&version, config) {
80 Some(
81 config
82 .versions_dir()
83 .join(installed.to_string())
84 .join("bin"),
85 )
86 } else {
87 println!(
88 "echo \"warning: Version '{}' which is specified as default does not exist\"",
89 version
90 );
91 None
92 }
93 } else {
94 system::path()
95 }
96}
97
98#[cfg(test)]
99mod tests {}