Skip to content

Commit

Permalink
Unlink by port instead of project name
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Feb 11, 2024
1 parent 0ed9398 commit 5f9cd72
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ Deletes all projects.

Lists each project in alphabetical order with its ports, directory, and linked port.

### `portman link $port [project-name]`
### `portman link <port> [project-name]`

Links a project to the specified port. `project-name` defaults to the active project.

### `portman unlink [project-name]`
### `portman unlink <port>`

Removes the linked port from a project. `project-name` defaults to the active project.
Unlinks the port from the project it was linked to.

### `portman caddyfile`

Expand Down
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub enum Cli {
project_name: Option<String>,
},

/// Unlink the port from a project
/// Unlink a port from a project
Unlink {
/// The name of the project to unlink (defaults to the active project)
project_name: Option<String>,
/// The port to unlink
port: u16,
},

/// Print the generated Caddyfile
Expand Down
14 changes: 5 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,16 @@ fn run(
};
registry.link(deps, &project_name, port)?;
registry.save(deps)?;
println!("Linked project {project_name} to port {port}");
println!("Linked port {port} to project {project_name}");
}

Cli::Unlink { project_name } => {
Cli::Unlink { port } => {
let mut registry = Registry::new(deps, port_allocator)?;
let project_name = match project_name {
Some(name) => name,
None => active_project(deps, &registry)?.0.clone(),
};
let unlinked_port = registry.unlink(&project_name)?;
let unlinked_port = registry.unlink(port);
registry.save(deps)?;
match unlinked_port {
Some(port) => println!("Unlinked project {project_name} from port {port}"),
None => println!("Project {project_name} was not linked to a port"),
Some(project_name) => println!("Unlinked port {port} from project {project_name}"),
None => println!("Port {port} was not linked to a project"),
};
}

Expand Down
32 changes: 12 additions & 20 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,16 @@ impl Registry {
Ok(())
}

// Unlink the port linked to a project and return the previous linked port
pub fn unlink(&mut self, project_name: &str) -> Result<Option<u16>> {
let Some(project) = self.projects.get_mut(project_name) else {
bail!("Project {project_name} does not exist");
};

let previous_linked_port = project.linked_port.take();
if previous_linked_port.is_some() {
self.dirty = true;
// Unlink the port linked to a project and return the name of the project it was linked to
pub fn unlink(&mut self, port: u16) -> Option<String> {
for (name, project) in &mut self.projects {
if project.linked_port == Some(port) {
project.linked_port = None;
self.dirty = true;
return Some(name.clone());
}
}
Ok(previous_linked_port)
None
}

// Find and return the project that matches the current working directory, if any
Expand Down Expand Up @@ -762,21 +761,14 @@ linked_port = 3000",
#[test]
fn test_unlink() {
let mut registry = get_mocked_registry().unwrap();
assert_eq!(registry.unlink("app2").unwrap().unwrap(), 3000);
assert_eq!(registry.unlink(3000).unwrap(), String::from("app2"));
assert!(registry.dirty);
}

#[test]
fn test_unlink_no_previous() {
let mut registry = get_mocked_registry().unwrap();
assert!(registry.unlink("app1").unwrap().is_none());
assert!(!registry.dirty);
}

#[test]
fn test_unlink_nonexistent() {
fn test_unlink_not_linked() {
let mut registry = get_mocked_registry().unwrap();
assert!(registry.unlink("app4").is_err());
assert!(registry.unlink(3001).is_none());
assert!(!registry.dirty);
}

Expand Down

0 comments on commit 5f9cd72

Please sign in to comment.