Checkpoint
This commit is contained in:
@@ -0,0 +1 @@
|
||||
pub mod setup;
|
||||
@@ -0,0 +1,84 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::Context;
|
||||
use nye_wire_protocol::messages::MessageResponse;
|
||||
use nye_wire_protocol::{Connection, Stream};
|
||||
use tokio::fs;
|
||||
|
||||
use crate::utils::LinuxUser;
|
||||
|
||||
pub async fn run(connection: &mut Connection, stream: &mut Stream) -> anyhow::Result<()> {
|
||||
let user =
|
||||
LinuxUser::from_connection(connection).context("Could not get the user to set up.")?;
|
||||
|
||||
setup(SetupKind::User(user.clone()))
|
||||
.await
|
||||
.context("Could not set up the user.")?;
|
||||
|
||||
stream
|
||||
.send(MessageResponse::Setup)
|
||||
.await
|
||||
.context("Could not send setup completion notification to the client.")?;
|
||||
|
||||
tracing::trace!(
|
||||
user.name,
|
||||
user.uid,
|
||||
user.gid,
|
||||
"Sent setup completion notification to the client"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Represents the type of setup to perform.
|
||||
pub enum SetupKind {
|
||||
/// Sets up the Nye package manager for a specific user.
|
||||
User(LinuxUser),
|
||||
|
||||
/// Sets up the Nye package manager for the entire system.
|
||||
System,
|
||||
}
|
||||
|
||||
impl SetupKind {
|
||||
/// Returns the root directory for the setup kind.
|
||||
///
|
||||
/// Returns:
|
||||
/// [`PathBuf`] - The root directory for the setup kind.
|
||||
fn root(&self) -> PathBuf {
|
||||
match self {
|
||||
SetupKind::User(user) => format!("/usr/{}/nye", user.name).into(),
|
||||
SetupKind::System => "/nye".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets up the environment for the given setup kind.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * `kind` - The kind of setup to perform.
|
||||
///
|
||||
/// Returns:
|
||||
/// * `Ok(())` - if the setup was successful.
|
||||
/// * `Err(anyhow::Error)` - if there was an error during setup.
|
||||
pub async fn setup(kind: SetupKind) -> anyhow::Result<()> {
|
||||
let dirs = [
|
||||
kind.root(),
|
||||
kind.root().join("packages"),
|
||||
kind.root().join("volumes"),
|
||||
];
|
||||
|
||||
for dir in dirs {
|
||||
fs::create_dir_all(&dir)
|
||||
.await
|
||||
.with_context(|| format!("Could not create directories at {}", dir.display()))?;
|
||||
|
||||
tracing::debug!("Created directories at {}", dir.display());
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
"The Nye package manager was successfully set up at {}",
|
||||
kind.root().display()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user