Skip to content

Commit 17b4103

Browse files
authored
Check for new Fly.io regions (#10)
* Add `Region::all()` * Check for new Fly.io regions
1 parent 0ddd859 commit 17b4103

5 files changed

Lines changed: 76 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
name: Format
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
- uses: moonrepo/setup-rust@v1
1515
with:
1616
components: rustfmt
@@ -21,7 +21,7 @@ jobs:
2121
name: Lint
2222
runs-on: ubuntu-latest
2323
steps:
24-
- uses: actions/checkout@v3
24+
- uses: actions/checkout@v4
2525
- uses: moonrepo/setup-rust@v1
2626
with:
2727
components: clippy
@@ -36,7 +36,7 @@ jobs:
3636
name: Test
3737
runs-on: ubuntu-latest
3838
steps:
39-
- uses: actions/checkout@v3
39+
- uses: actions/checkout@v4
4040
- uses: moonrepo/setup-rust@v1
4141
- name: Run tests
4242
run: >-

.github/workflows/regions.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Check for new regions
2+
3+
on:
4+
pull_request:
5+
paths: src/region.rs
6+
workflow_dispatch:
7+
schedule:
8+
- cron: >-
9+
15 15 * * *
10+
11+
jobs:
12+
check:
13+
name: Check
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
issues: write
18+
steps:
19+
- name: Get current regions
20+
run: >
21+
curl 'https://app.fly.io/graphql' -H 'Accept: application/json' -H 'Content-Type: application/json' --data-binary '{"query":"{\n platform {\n regions {\n code\n name\n }\n }\n}"}'
22+
| jq '.data.platform.regions'
23+
> "${RUNNER_TEMP}/regions.current.json"
24+
- uses: actions/checkout@v4
25+
- uses: moonrepo/setup-rust@v1
26+
- name: Get defined regions
27+
run: >-
28+
cargo run --example regions | awk '{print $1}' | sort
29+
> "${RUNNER_TEMP}/regions.defined.txt"
30+
- name: Compare
31+
uses: silverlyra/script-action@v0.2
32+
with:
33+
script: |
34+
const read = (filename) => fs.readFile(path.join(env.RUNNER_TEMP, filename), 'utf-8');
35+
const current = JSON.parse(await read('regions.current.json'));
36+
const defined = new Set((await read('regions.defined.txt')).split('\n').filter(Boolean));
37+
const absent = new Set();
38+
39+
for (const { code, name } of current) {
40+
console.log(`${code}\t${name}`);
41+
42+
if (!defined.has(code)) {
43+
absent.add(code);
44+
console.log(` not defined in Flytrap`);
45+
}
46+
}
47+
48+
if (absent.size > 0) process.exit(1);

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "flytrap"
33
description = "Query the Fly.io runtime environment"
4-
version = "0.2.0"
4+
version = "0.2.1"
55
edition = "2021"
66
license = "MIT"
77
authors = ["Lyra Naeseth <lyra@lyra.codes>"]
@@ -67,6 +67,10 @@ tokio = { version = "1.34.0", features = ["full"] }
6767
name = "api"
6868
required-features = ["api", "environment"]
6969

70+
[[example]]
71+
name = "regions"
72+
required-features = ["regions"]
73+
7074
[package.metadata.docs.rs]
7175
all-features = true
7276
rustdoc-args = ["--cfg", "docsrs"]

examples/regions.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use flytrap::Region;
2+
3+
fn main() {
4+
for (code, details) in Region::all() {
5+
println!("{}\t{}", code, details.name);
6+
}
7+
}

src/region.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ impl Region {
240240
DETAILS[*self]
241241
}
242242

243+
/// Iterate over all known [regions][Region].
244+
pub fn all() -> impl Iterator<Item = (Region, RegionDetails<'static>)> {
245+
DETAILS.iter().map(|(r, d)| (r, *d))
246+
}
247+
243248
fn key(&self) -> RegionKey<'_> {
244249
(self.city.geo.x(), self.city.geo.y(), &self.code)
245250
}
@@ -330,7 +335,7 @@ impl FromStr for Region {
330335
/// assert_eq!(city.name, "Atlanta");
331336
/// assert_eq!(name, "Atlanta, Georgia (US)");
332337
/// ```
333-
#[derive(Copy, Clone, Debug)]
338+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
334339
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
335340
pub struct RegionDetails<'l> {
336341
pub code: &'l str,
@@ -359,7 +364,7 @@ impl RegionDetails<'static> {
359364
}
360365

361366
/// Describes a city where a Fly.io [region][Region] is hosted.
362-
#[derive(Copy, Clone, Debug)]
367+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
363368
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
364369
pub struct City<'l> {
365370
pub name: &'l str,
@@ -597,4 +602,10 @@ mod test {
597602
regions
598603
);
599604
}
605+
606+
#[test]
607+
fn all() {
608+
assert!(Region::all().count() >= 30);
609+
assert!(Region::all().all(|(r, d)| r.details() == d));
610+
}
600611
}

0 commit comments

Comments
 (0)