refactor(state): remove unused methods and parameters
This commit is contained in:
@@ -51,8 +51,6 @@ pub enum ControlCommands {
|
|||||||
pub enum Commands {
|
pub enum Commands {
|
||||||
/// Create a new project, or initialize instance for existing one
|
/// Create a new project, or initialize instance for existing one
|
||||||
Init,
|
Init,
|
||||||
/// Create a new project, or initialize instance for existing one
|
|
||||||
Sync,
|
|
||||||
|
|
||||||
/// Start the PostgreSQL container for the current project
|
/// Start the PostgreSQL container for the current project
|
||||||
Instance {
|
Instance {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const VERIFY_DURATION_SECS: u64 = 10;
|
|||||||
pub struct Controller {
|
pub struct Controller {
|
||||||
docker: DockerController,
|
docker: DockerController,
|
||||||
project: Option<Project>,
|
project: Option<Project>,
|
||||||
|
#[allow(unused)]
|
||||||
state: StateManager,
|
state: StateManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +115,7 @@ impl Controller {
|
|||||||
for attempt in 1..=MAX_RETRIES {
|
for attempt in 1..=MAX_RETRIES {
|
||||||
spinner.set_message(format!("Starting container (attempt {}/{})", attempt, MAX_RETRIES));
|
spinner.set_message(format!("Starting container (attempt {}/{})", attempt, MAX_RETRIES));
|
||||||
|
|
||||||
let result = self.try_starting_container(&container_id, attempt, &spinner).await;
|
let result = self.try_starting_container(&container_id, &spinner).await;
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
@@ -138,7 +139,6 @@ impl Controller {
|
|||||||
async fn try_starting_container(
|
async fn try_starting_container(
|
||||||
&self,
|
&self,
|
||||||
container_id: &String,
|
container_id: &String,
|
||||||
attempt: u32,
|
|
||||||
spinner: &indicatif::ProgressBar,
|
spinner: &indicatif::ProgressBar,
|
||||||
) -> Result<(), miette::Error> {
|
) -> Result<(), miette::Error> {
|
||||||
match self.docker.start_container_by_id(container_id).await {
|
match self.docker.start_container_by_id(container_id).await {
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ async fn main() -> Result<()> {
|
|||||||
match cli.command {
|
match cli.command {
|
||||||
cli::Commands::Init => controller.init_project().await?,
|
cli::Commands::Init => controller.init_project().await?,
|
||||||
cli::Commands::Instance { name, cmd } => todo!(),
|
cli::Commands::Instance { name, cmd } => todo!(),
|
||||||
cli::Commands::Sync => todo!(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
65
src/state.rs
65
src/state.rs
@@ -39,14 +39,6 @@ fn state_file_path() -> Result<PathBuf> {
|
|||||||
Ok(PathBuf::from(home).join(".pgd").join("state.json"))
|
Ok(PathBuf::from(home).join(".pgd").join("state.json"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the path to the .pgd directory
|
|
||||||
pub fn pgd_dir() -> Result<PathBuf> {
|
|
||||||
let home = std::env::var("HOME")
|
|
||||||
.into_diagnostic()
|
|
||||||
.wrap_err("Failed to get HOME environment variable")?;
|
|
||||||
|
|
||||||
Ok(PathBuf::from(home).join(".pgd"))
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StateManager {
|
impl StateManager {
|
||||||
/// Load the state manager from disk, or create a new one if it doesn't exist
|
/// Load the state manager from disk, or create a new one if it doesn't exist
|
||||||
@@ -100,11 +92,6 @@ impl StateManager {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the state for a specific project
|
|
||||||
pub fn get(&self, project_name: &str) -> Option<&InstanceState> {
|
|
||||||
self.instances.get(project_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get mutable state for a specific project
|
/// Get mutable state for a specific project
|
||||||
pub fn get_mut(&mut self, project_name: &str) -> Option<&mut InstanceState> {
|
pub fn get_mut(&mut self, project_name: &str) -> Option<&mut InstanceState> {
|
||||||
self.instances.get_mut(project_name)
|
self.instances.get_mut(project_name)
|
||||||
@@ -115,28 +102,10 @@ impl StateManager {
|
|||||||
self.instances.insert(project_name, state);
|
self.instances.insert(project_name, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the state for a specific project, creating it if it doesn't exist
|
|
||||||
pub fn update<F>(&mut self, project_name: &str, updater: F) -> Result<()>
|
|
||||||
where
|
|
||||||
F: FnOnce(&mut InstanceState),
|
|
||||||
{
|
|
||||||
if let Some(state) = self.instances.get_mut(project_name) {
|
|
||||||
updater(state);
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
miette::bail!("No state found for project: {}", project_name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove the state for a specific project
|
/// Remove the state for a specific project
|
||||||
pub fn remove(&mut self, project_name: &str) -> Option<InstanceState> {
|
pub fn remove(&mut self, project_name: &str) -> Option<InstanceState> {
|
||||||
self.instances.remove(project_name)
|
self.instances.remove(project_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all instances
|
|
||||||
pub fn all_instances(&self) -> &HashMap<String, InstanceState> {
|
|
||||||
&self.instances
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InstanceState {
|
impl InstanceState {
|
||||||
@@ -153,36 +122,4 @@ impl InstanceState {
|
|||||||
created_at: now,
|
created_at: now,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_state_manager_operations() {
|
|
||||||
let mut manager = StateManager {
|
|
||||||
instances: HashMap::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let state = InstanceState::new(
|
|
||||||
"container123".to_string(),
|
|
||||||
PostgresVersion {
|
|
||||||
major: 18,
|
|
||||||
minor: 1,
|
|
||||||
},
|
|
||||||
5432,
|
|
||||||
);
|
|
||||||
|
|
||||||
manager.set("my-project".to_string(), state);
|
|
||||||
|
|
||||||
assert!(manager.get("my-project").is_some());
|
|
||||||
assert_eq!(
|
|
||||||
manager.get("my-project").unwrap().container_id,
|
|
||||||
"container123"
|
|
||||||
);
|
|
||||||
|
|
||||||
manager.remove("my-project");
|
|
||||||
assert!(manager.get("my-project").is_none());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user