Skip to content

Commit

Permalink
support prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmmjackson committed Sep 15, 2023
1 parent bd2052a commit 3eb4145
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 10 deletions.
114 changes: 114 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
name: Release
on:
push:
tags:
- "*"
jobs:
build-mac-m1:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
target: aarch64-apple-darwin
- uses: actions-rs/cargo@v1
with:
command: build
args: --release
- uses: actions/cache@v3
with:
path: target/aarch64-apple-darwin
build-mac-x86:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
target: x86_64-apple-darwin
- uses: actions-rs/cargo@v1
with:
command: build
args: --release
- uses: actions/cache@v3
with:
path: target/x86_64-apple-darwin
build-linux-aarch64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
target: aarch64-unknown-linux-gnu
- uses: actions-rs/cargo@v1
with:
command: build
args: --release
- uses: actions/cache@v3
with:
path: aarch64-unknown-linux-gnu
build-linux-x86_64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
target: x86_64-unknown-linux-gnu
- uses: actions-rs/cargo@v1
with:
command: build
args: --release
- uses: actions/cache@v3
with:
path: x86_64-unknown-linux-gnu
build-windows-x86_64:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
target: x86_64-pc-windows-msvc
- uses: actions-rs/cargo@v1
with:
command: build
args: --release
- uses: actions/cache@v3
with:
path: x86_64-pc-windows-msvc
build-windows-aarch64:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
target: aarch64-pc-windows-msvc
- uses: actions-rs/cargo@v1
with:
command: build
args: --release
- uses: actions/cache@v3
with:
path: aarch64-pc-windows-msvc
archive:
runs-on: ubuntu-latest
needs:
- build-mac-m1
- build-mac-x86
- build-linux-aarch64
- build-linux-x86_64
- build-windows-x86_64
- build-windows-aarch64
steps:
- uses: extractions/setup-just@v1
- run: just archive-all do-release-package
11 changes: 10 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version:=`toml get Cargo.toml package.version --raw`
archive_name:="upnotify-" + version + "-" + target
msg:="Unknown error"
binary_name:=if os_family == "windows" { "upnotify.exe" } else { "upnotify "}
notes:=""

default: build

Expand Down Expand Up @@ -58,6 +59,14 @@ archive-windows:
just target=aarch64-pc-windows-msvc archive
just target=x86_64-pc-windows-msvc archive

archive-all:
just target=aarch64-apple-darwin archive-tarball
just target=x86_64-apple-darwin archive-tarball
just target=aarch64-unknown-linux-gnu archive-tarball
just target=x86_64-unknown-linux-gnu archive-tarball
just target=aarch64-pc-windows-msvc archive-zip
just target=x86_64-pc-windows-msvc archive-zip

package:
#!/usr/bin/env bash
mkdir -p dist/{{target}}
Expand Down Expand Up @@ -86,7 +95,7 @@ linux-packages:
just target=aarch64-unknown-linux-gnu package_type=deb package-termux

create-release:
gh release create {{version}}
gh release create {{version}} --notes "{{notes}}"

upload-to-release:
gh release view {{version}} || just msg="Release does not exist" die
Expand Down
20 changes: 11 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,38 @@ fn main() {
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(
arg!(--url <VALUE>).required(true).action(ArgAction::Set)
)
.arg(arg!(--url <VALUE>).required(true).action(ArgAction::Set))
.arg(arg!(--prefix <VALUE>).required(false).action(ArgAction::Set))
.get_matches();

let url = cli.get_one::<String>("url").expect("required");
let default_prefix = "".to_string();
let prefix = cli.get_one::<String>("prefix").unwrap_or(&default_prefix);
let client = Client::builder()
.danger_accept_invalid_certs(true)
.build().unwrap_or_else(|_result| panic!("Unable to create client!"));
let mut previous_status_option: Result = Result::None;

loop {
let response = client.get(url).send();
previous_status_option = request_loop(response, previous_status_option);
previous_status_option = request_loop(response, previous_status_option, prefix);
sleep(Duration::from_secs(5));
}
}

fn request_loop(response: reqwest::Result<reqwest::blocking::Response>,
previous_status_option: Result) -> Result {
previous_status_option: Result, prefix: &String) -> Result {
return match response {
Ok(result) => {
let status_code = result.status();
match previous_status_option {
Result::StatusCode(previous_status)
if status_code.as_u16() != previous_status.as_u16() => {
println!("Status changed to {:?}", status_code)
println!("{:?} Status changed to {:?}", prefix, status_code)
}
Result::None | Result::TimeoutError(_) | Result::ConnectError(_) |
Result::RequestError(_) | Result::OtherError(_) => {
println!("Status changed to {:?}", status_code)
println!("{:?} Status changed to {:?}", prefix, status_code)
}
_ => {}
}
Expand Down Expand Up @@ -95,11 +96,12 @@ mod tests {
#[test]
fn test_request_loop() {
// Unfortunately, reqwest doesn't really let us mock results :(
let result200 = reqwest::Result::Ok(Response::from(
let result200 = Ok(Response::from(
HttpResponse::builder().status(StatusCode::OK).body("").unwrap()
));
let prefix = "".to_string();
let result = request_loop(result200,
Result::StatusCode(StatusCode::NOT_FOUND));
Result::StatusCode(StatusCode::NOT_FOUND), &prefix);
match result {
Result::StatusCode(StatusCode::OK) => { /* ok */ },
_ => { panic!("Unexpected status code!", )}
Expand Down

0 comments on commit 3eb4145

Please sign in to comment.