Skip to content

Commit

Permalink
Add link support
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Jan 30, 2024
1 parent aef523f commit f88ecce
Show file tree
Hide file tree
Showing 14 changed files with 1,123 additions and 842 deletions.
34 changes: 0 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ anyhow = "1.0.66"
clap = { version = "4.2.4", features = ["derive"] }
directories = "5.0.0"
entrait = { version = "0.4.6", features = ["unimock"] }
lazy_static = "1.4.0"
rand = "0.8.5"
regex = { version = "1.5.5", default-features = false, features = ["perf", "std"] }
serde = { version = "1.0.136", features = ["derive"] }
toml = "0.7.3"
unimock = "0.3.14"
Expand Down
232 changes: 131 additions & 101 deletions README.md

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions default_config.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# `ranges` tells portman which ranges of ports to allocate ports in. It is an
# array of two-item arrays. The first item is the start of the port range and
# the second item is the end of the port range.
# `ranges` tells portman which ranges of ports can be assigned to projects. It
# is an array of two-item arrays. The first item is the start of the port range
# and the second item is the end of the port range.
#
# Example (allocates ports from 3000-3099 and 3200-3299):
# Example (assigns ports from 3000-3099 and 3200-3299):
# ranges = [[3000, 3099], [3200, 3299]]
ranges = [[3000, 3999]]

# `ranges` overrides `ranges` and tells portman which ports to never allocate.
# `reserved` overrides `ranges` and tells portman which ports to never assign.
# It is an array of integers.
#
# Example (allocates ports in `ranges` except for 3210 and 3121):
# Example (assigns ports in `ranges` except for 3210 and 3121):
# reserved = [3210, 3121]
reserved = []
2 changes: 1 addition & 1 deletion e2e/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ echo -e "# Empty" > $HOMEBREW_PREFIX/etc/Caddyfile
sudo $(which caddy) start --config $HOMEBREW_PREFIX/etc/Caddyfile

echo "127.0.0.1 portman.localhost" | sudo tee -a /etc/hosts
portman allocate
portman create
40 changes: 28 additions & 12 deletions src/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::dependencies::ChoosePort;
use anyhow::{bail, Result};
use std::collections::HashSet;

pub struct PortAllocator {
Expand All @@ -13,8 +14,13 @@ impl PortAllocator {
}
}

// Remove a port from the pool of available ports
pub fn discard(&mut self, port: u16) {
self.available_ports.remove(&port);
}

// Allocate a new port, using the desired port if it is provided and is valid
pub fn allocate(&mut self, deps: &impl ChoosePort, desired_port: Option<u16>) -> Option<u16> {
pub fn allocate(&mut self, deps: &impl ChoosePort, desired_port: Option<u16>) -> Result<u16> {
let allocated_port = desired_port
.and_then(|port| {
if self.available_ports.contains(&port) {
Expand All @@ -24,10 +30,11 @@ impl PortAllocator {
}
})
.or_else(|| deps.choose_port(&self.available_ports));
if let Some(port) = allocated_port {
self.available_ports.remove(&port);
}
allocated_port
let Some(port) = allocated_port else {
bail!("All available ports have been allocated already")
};
self.available_ports.remove(&port);
Ok(port)
}
}

Expand All @@ -52,22 +59,31 @@ mod tests {
assert!(range.contains(&allocator.allocate(&deps, None).unwrap()));
}

#[test]
fn test_discard() {
let mut allocator = PortAllocator::new(3000..=3001);
let deps = get_deps();
allocator.discard(3000);
assert_eq!(allocator.allocate(&deps, None).unwrap(), 3001);
assert!(allocator.allocate(&deps, None).is_err());
}

#[test]
fn test_allocate() {
let mut allocator = PortAllocator::new(3000..=3001);
let deps = get_deps();
assert!(allocator.allocate(&deps, None).is_some());
assert!(allocator.allocate(&deps, None).is_some());
assert_eq!(allocator.allocate(&deps, None), None);
assert!(allocator.allocate(&deps, None).is_ok());
assert!(allocator.allocate(&deps, None).is_ok());
assert!(allocator.allocate(&deps, None).is_err());
}

#[test]
fn test_desired_port() {
let mut allocator = PortAllocator::new(3000..=3002);
let deps = get_deps();
assert_eq!(allocator.allocate(&deps, Some(3001)), Some(3001));
assert_eq!(allocator.allocate(&deps, Some(4000)), Some(3000));
assert_eq!(allocator.allocate(&deps, None), Some(3002));
assert_eq!(allocator.allocate(&deps, None), None);
assert_eq!(allocator.allocate(&deps, Some(3001)).unwrap(), 3001);
assert_eq!(allocator.allocate(&deps, Some(4000)).unwrap(), 3000);
assert_eq!(allocator.allocate(&deps, None).unwrap(), 3002);
assert!(allocator.allocate(&deps, None).is_err());
}
}
Loading

0 comments on commit f88ecce

Please sign in to comment.