diff options
Diffstat (limited to 'crates/turtle/src/atuin_common')
| -rw-r--r-- | crates/turtle/src/atuin_common/api.rs | 27 | ||||
| -rw-r--r-- | crates/turtle/src/atuin_common/calendar.rs | 16 | ||||
| -rw-r--r-- | crates/turtle/src/atuin_common/shell.rs | 137 | ||||
| -rw-r--r-- | crates/turtle/src/atuin_common/utils.rs | 22 |
4 files changed, 3 insertions, 199 deletions
diff --git a/crates/turtle/src/atuin_common/api.rs b/crates/turtle/src/atuin_common/api.rs index c18db04f..0868943d 100644 --- a/crates/turtle/src/atuin_common/api.rs +++ b/crates/turtle/src/atuin_common/api.rs @@ -11,28 +11,6 @@ pub(crate) static ATUIN_VERSION: LazyLock<Version> = LazyLock::new(|| Version::parse(ATUIN_CARGO_VERSION).expect("failed to parse self semver")); #[derive(Debug, Serialize, Deserialize)] -pub(crate) struct RegisterResponse { - pub(crate) session: String, -} - -#[derive(Debug, Serialize, Deserialize)] -pub(crate) struct ChangePasswordRequest { - pub(crate) current_password: String, - pub(crate) new_password: String, -} - -#[derive(Debug, Serialize, Deserialize)] -pub(crate) struct LoginRequest { - pub(crate) username: String, - pub(crate) password: String, -} - -#[derive(Debug, Serialize, Deserialize)] -pub(crate) struct LoginResponse { - pub(crate) session: String, -} - -#[derive(Debug, Serialize, Deserialize)] pub(crate) struct ErrorResponse<'a> { pub(crate) reason: Cow<'a, str>, } @@ -42,8 +20,3 @@ pub(crate) struct IndexResponse { pub(crate) homage: String, pub(crate) version: String, } - -#[derive(Debug, Serialize, Deserialize)] -pub(crate) struct MeResponse { - pub(crate) username: String, -} diff --git a/crates/turtle/src/atuin_common/calendar.rs b/crates/turtle/src/atuin_common/calendar.rs deleted file mode 100644 index befe8c2e..00000000 --- a/crates/turtle/src/atuin_common/calendar.rs +++ /dev/null @@ -1,16 +0,0 @@ -// Calendar data -use serde::{Serialize, Deserialize}; - -pub(crate) enum TimePeriod { - YEAR, - MONTH, - DAY, -} - -#[derive(Debug, Serialize, Deserialize)] -pub(crate) struct TimePeriodInfo { - pub(crate) count: u64, - - // TODO: Use this for merkle tree magic - pub(crate) hash: String, -} diff --git a/crates/turtle/src/atuin_common/shell.rs b/crates/turtle/src/atuin_common/shell.rs index dbd9b982..d259b99e 100644 --- a/crates/turtle/src/atuin_common/shell.rs +++ b/crates/turtle/src/atuin_common/shell.rs @@ -1,9 +1,3 @@ -use std::{ffi::OsStr, path::Path, process::Command}; - -use serde::Serialize; -use sysinfo::{Process, System, get_current_pid}; -use thiserror::Error; - #[derive(PartialEq)] pub(crate) enum Shell { Sh, @@ -35,91 +29,15 @@ impl std::fmt::Display for Shell { } } -#[derive(Debug, Error, Serialize)] -pub(crate) enum ShellError { - #[error("shell not supported")] - NotSupported, - - #[error("failed to execute shell command: {0}")] - ExecError(String), -} - impl Shell { - pub(crate) fn current() -> Shell { - let sys = System::new_all(); - - let process = sys - .process(get_current_pid().expect("Failed to get current PID")) - .expect("Process with current pid does not exist"); - - let parent = sys - .process(process.parent().expect("Atuin running with no parent!")) - .expect("Process with parent pid does not exist"); - - let shell = parent.name().trim().to_lowercase(); - let shell = shell.strip_prefix('-').unwrap_or(&shell); - - Shell::from_string(shell.to_string()) - } - pub(crate) fn from_env() -> Shell { std::env::var("ATUIN_SHELL").map_or(Shell::Unknown, |shell| { - Shell::from_string(shell.trim().to_lowercase()) + Shell::from_string(shell.trim().to_lowercase().as_str()) }) } - pub(crate) fn config_file(&self) -> Option<std::path::PathBuf> { - let mut path = if let Some(base) = directories::BaseDirs::new() { - base.home_dir().to_owned() - } else { - return None; - }; - - // TODO: handle all shells - match self { - Shell::Bash => path.push(".bashrc"), - Shell::Zsh => path.push(".zshrc"), - Shell::Fish => path.push(".config/fish/config.fish"), - - _ => return None, - }; - - Some(path) - } - - /// Best-effort attempt to determine the default shell - /// This implementation will be different across different platforms - /// Caller should ensure to handle Shell::Unknown correctly - pub(crate) fn default_shell() -> Result<Shell, ShellError> { - let sys = System::name().unwrap_or("".to_string()).to_lowercase(); - - // TODO: Support Linux - // I'm pretty sure we can use /etc/passwd there, though there will probably be some issues - let path = if sys.contains("darwin") { - // This works in my testing so far - Shell::Sh.run_interactive([ - "dscl localhost -read \"/Local/Default/Users/$USER\" shell | awk '{print $2}'", - ])? - } else if cfg!(windows) { - return Ok(Shell::Powershell); - } else { - Shell::Sh.run_interactive(["getent passwd $LOGNAME | cut -d: -f7"])? - }; - - let path = Path::new(path.trim()); - let shell = path.file_name(); - - if shell.is_none() { - return Err(ShellError::NotSupported); - } - - Ok(Shell::from_string( - shell.unwrap().to_string_lossy().to_string(), - )) - } - - pub(crate) fn from_string(name: String) -> Shell { - match name.as_str() { + pub(crate) fn from_string(name: &str) -> Shell { + match name { "bash" => Shell::Bash, "fish" => Shell::Fish, "zsh" => Shell::Zsh, @@ -131,53 +49,4 @@ impl Shell { _ => Shell::Unknown, } } - - /// Returns true if the shell is posix-like - /// Note that while fish is not posix compliant, it behaves well enough for our current - /// featureset that this does not matter. - pub(crate) fn is_posixish(&self) -> bool { - matches!(self, Shell::Bash | Shell::Fish | Shell::Zsh) - } - - pub(crate) fn run_interactive<I, S>(&self, args: I) -> Result<String, ShellError> - where - I: IntoIterator<Item = S>, - S: AsRef<OsStr>, - { - let shell = self.to_string(); - let output = if self == &Self::Powershell { - Command::new(shell) - .args(args) - .output() - .map_err(|e| ShellError::ExecError(e.to_string()))? - } else { - Command::new(shell) - .arg("-ic") - .args(args) - .output() - .map_err(|e| ShellError::ExecError(e.to_string()))? - }; - - Ok(String::from_utf8(output.stdout).unwrap()) - } -} - -pub(crate) fn shell_name(parent: Option<&Process>) -> String { - let sys = System::new_all(); - - let parent = if let Some(parent) = parent { - parent - } else { - let process = sys - .process(get_current_pid().expect("Failed to get current PID")) - .expect("Process with current pid does not exist"); - - sys.process(process.parent().expect("Atuin running with no parent!")) - .expect("Process with parent pid does not exist") - }; - - let shell = parent.name().trim().to_lowercase(); - let shell = shell.strip_prefix('-').unwrap_or(&shell); - - shell.to_string() } diff --git a/crates/turtle/src/atuin_common/utils.rs b/crates/turtle/src/atuin_common/utils.rs index 09718241..ba0c8eb7 100644 --- a/crates/turtle/src/atuin_common/utils.rs +++ b/crates/turtle/src/atuin_common/utils.rs @@ -2,30 +2,8 @@ use std::borrow::Cow; use std::env; use std::path::{Path, PathBuf}; -use base64::prelude::{BASE64_URL_SAFE_NO_PAD, Engine}; -use getrandom::getrandom; use uuid::Uuid; -/// Generate N random bytes, using a cryptographically secure source -pub(crate) fn crypto_random_bytes<const N: usize>() -> [u8; N] { - // rand say they are in principle safe for crypto purposes, but that it is perhaps a better - // idea to use getrandom for things such as passwords. - let mut ret = [0u8; N]; - - getrandom(&mut ret).expect("Failed to generate random bytes!"); - - ret -} - -/// Generate N random bytes using a cryptographically secure source, return encoded as a string -pub(crate) fn crypto_random_string<const N: usize>() -> String { - let bytes = crypto_random_bytes::<N>(); - - // We only use this to create a random string, and won't be reversing it to find the original - // data - no padding is OK there. It may be in URLs. - BASE64_URL_SAFE_NO_PAD.encode(bytes) -} - pub(crate) fn uuid_v7() -> Uuid { Uuid::now_v7() } |
