|
| 1 | +use logger_core::{log_info, log_warn}; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | + |
| 4 | +pub const GLIDE_BASE_FOLDER_NAME: &str = "GLIDE"; |
| 5 | +pub const SOCKET_FILE_NAME: &str = "glide-socket.sock"; |
| 6 | + |
| 7 | +/// For ergonomics: Converts a PathBuf into a string. |
| 8 | +fn pathbuf_to_string(pathbuf: &Path) -> String { |
| 9 | + pathbuf.as_os_str().to_string_lossy().to_string() |
| 10 | +} |
| 11 | + |
| 12 | +/// For ergonomics: Converts a string into a PathBuf. |
| 13 | +fn string_to_pathbuf(string: &str) -> PathBuf { |
| 14 | + PathBuf::from(string) |
| 15 | +} |
| 16 | + |
| 17 | +/// GlidePaths struct. |
| 18 | +/// Contains the path to the file, primarily in order to manage the socket file. |
| 19 | +/// The socket file is used to communicate with the GLIDE server. |
| 20 | +pub struct GlidePaths { |
| 21 | + pub glide_file: PathBuf, |
| 22 | +} |
| 23 | +/// GlidePaths struct implementation. |
| 24 | +impl GlidePaths { |
| 25 | + /// Creates a new GlidePaths struct with the given file path. |
| 26 | + pub fn new(glide_file: PathBuf) -> Self { |
| 27 | + GlidePaths { glide_file } |
| 28 | + } |
| 29 | + |
| 30 | + /// Create a new GlidePaths struct from string paths. |
| 31 | + pub fn from_file_path(glide_file: &str) -> Self { |
| 32 | + GlidePaths { |
| 33 | + glide_file: string_to_pathbuf(glide_file), |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Create a new GlidePaths struct from string file name. |
| 38 | + pub fn from_file_name(file_name: &str) -> Self { |
| 39 | + GlidePaths { |
| 40 | + glide_file: Self::glide_sock_dir_path().join(file_name), |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Returns the path to the temporary directory where the socket file is stored. |
| 45 | + fn glide_sock_dir_path() -> PathBuf { |
| 46 | + let path = std::env::temp_dir() |
| 47 | + .join(GLIDE_BASE_FOLDER_NAME) |
| 48 | + .join(std::process::id().to_string()); |
| 49 | + if !path.exists() { |
| 50 | + std::fs::create_dir_all(&path) |
| 51 | + .map_err(|e| { |
| 52 | + log_warn( |
| 53 | + "Socket directory creation", |
| 54 | + format!("Error creating socket directory: {e}"), |
| 55 | + ) |
| 56 | + }) |
| 57 | + .ok(); |
| 58 | + } |
| 59 | + path |
| 60 | + } |
| 61 | + |
| 62 | + /// Returns the path to the socket file as PathBuf. |
| 63 | + pub fn glide_sock_file_path() -> PathBuf { |
| 64 | + Self::glide_sock_dir_path().join(SOCKET_FILE_NAME) |
| 65 | + } |
| 66 | + |
| 67 | + /// Removes the socket file. |
| 68 | + pub fn remove_glide_file(&self) -> Result<(), std::io::Error> { |
| 69 | + let results = std::fs::remove_file(self.glide_file.clone()); |
| 70 | + if let Err(e) = results { |
| 71 | + log_warn( |
| 72 | + "Socket file removal", |
| 73 | + format!("Error removing socket file: {e}"), |
| 74 | + ); |
| 75 | + return Err(e); |
| 76 | + } |
| 77 | + log_info("Socket file removal", "Socket file removed successfully."); |
| 78 | + Ok(()) |
| 79 | + } |
| 80 | + |
| 81 | + /// Get the glide_file as a string. |
| 82 | + pub fn glide_file(&self) -> String { |
| 83 | + pathbuf_to_string(&self.glide_file) |
| 84 | + } |
| 85 | + |
| 86 | + /// Replace the glide_file with a new path. |
| 87 | + pub fn set_glide_file_string(&mut self, glide_file: &str) { |
| 88 | + self.glide_file = string_to_pathbuf(&glide_file); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl Default for GlidePaths { |
| 93 | + fn default() -> Self { |
| 94 | + GlidePaths { |
| 95 | + glide_file: Self::glide_sock_file_path(), |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments