-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from nicholaschiasson/minor/add-cli-binary
Add CLI binary
- Loading branch information
Showing
5 changed files
with
238 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,37 @@ jobs: | |
name: 'Cargo.toml' | ||
path: 'Cargo.toml' | ||
|
||
build: | ||
if: github.event_name == 'push' || (github.base_ref == 'main' && github.event.pull_request.merged == true) | ||
strategy: | ||
matrix: | ||
platform: [macos-latest, windows-latest] | ||
runs-on: ${{ matrix.platform }} | ||
needs: [tag] | ||
steps: | ||
- name: Checkout | ||
uses: actions/[email protected] | ||
- name: Download Build Artifacts | ||
uses: actions/[email protected] | ||
with: | ||
name: 'Cargo.toml' | ||
- name: Build | ||
shell: bash | ||
run: | | ||
RAW_BINARY_NAME=fcidr | ||
BINARY_NAME=${RAW_BINARY_NAME} | ||
if [[ ${{ startsWith(matrix.platform, 'windows') }} == true ]] | ||
then | ||
BINARY_NAME=${BINARY_NAME}.exe | ||
fi | ||
cargo build --release --verbose | ||
cp target/release/${BINARY_NAME} ./ | ||
tar czf ${RAW_BINARY_NAME}-${{ runner.os }}-${{ runner.arch }}.tar.gz ${BINARY_NAME} | ||
- name: Upload Build Artifact | ||
uses: actions/[email protected] | ||
with: | ||
path: '*.tar.gz' | ||
|
||
publish: | ||
if: github.event_name == 'push' || (github.base_ref == 'main' && github.event.pull_request.merged == true) | ||
runs-on: ubuntu-latest | ||
|
@@ -108,11 +139,12 @@ jobs: | |
release: | ||
if: github.event_name == 'push' || (github.base_ref == 'main' && github.event.pull_request.merged == true) | ||
runs-on: ubuntu-latest | ||
needs: [tag] | ||
needs: [tag, build] | ||
steps: | ||
- name: Download Build Artifacts | ||
uses: actions/[email protected] | ||
- name: Release | ||
uses: softprops/[email protected] | ||
with: | ||
files: 'artifact/*.tar.gz' | ||
tag_name: ${{ needs.tag.outputs.version }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,28 @@ version = "0.0.0" | |
authors = ["Nicholas Omer Chiasson <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
description = """ | ||
Fragmented Classless Inter-Domain Routing (FCIDR) | ||
A library exposing a data structure to represent a set of CIDR ranges and | ||
easily manipulate its entries using set-like operations. | ||
""" | ||
description = """Fragmented Classless Inter-Domain Routing (FCIDR)""" | ||
readme = "README.md" | ||
homepage = "https://github.com/nicholaschiasson/fcidr" | ||
repository = "https://github.com/nicholaschiasson/fcidr" | ||
keywords = ["network", "ip", "ipv4", "cidr"] | ||
categories = ["data-structures", "network-programming"] | ||
keywords = ["network", "ip", "ipv4", "cidr", "cli"] | ||
categories = ["command-line-utilities", "data-structures", "network-programming"] | ||
rust-version = "1.70.0" | ||
|
||
[lib] | ||
name = "fcidr" | ||
path = "src/lib.rs" | ||
|
||
[[bin]] | ||
name = "fcidr" | ||
path = "src/main.rs" | ||
|
||
[badges] | ||
github = { repository = "nicholaschiasson/fcidr" } | ||
maintenance = { status = "passively-maintained" } | ||
|
||
[dependencies] | ||
clap = { version = "4.3", features = ["derive"] } | ||
serde = { version = "1.0", optional = true } | ||
|
||
[dev-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::{ | ||
error::Error, | ||
io::{stdin, IsTerminal}, | ||
}; | ||
|
||
use clap::{CommandFactory, Parser, Subcommand}; | ||
use fcidr::{Cidr, Fcidr}; | ||
|
||
#[derive(Debug, Parser)] | ||
#[command(about, author, version, long_about = None)] | ||
struct Cli { | ||
/// The input CIDR range and first operand to the computation. If omitted, | ||
/// input is taken from stdin. In this way, multiple computations can be | ||
/// chained together. | ||
cidr: Option<Cidr>, | ||
#[command(subcommand)] | ||
command: FcidrCommand, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum FcidrCommand { | ||
/// Compute the complement of the input CIDR(s) | ||
#[command(visible_alias = "!", visible_alias = "not")] | ||
Complement, | ||
/// Compute the set difference between the input CIDR(s) and another CIDR | ||
#[command( | ||
visible_alias = "-", | ||
visible_alias = "exclude", | ||
visible_alias = "minus" | ||
)] | ||
Difference { | ||
/// The second CIDR range operand for the difference function | ||
cidr: Cidr, | ||
}, | ||
#[command(visible_alias = "+", visible_alias = "include", visible_alias = "plus")] | ||
/// Compute the set union of the input CIDR(s) and another CIDR | ||
Union { | ||
/// The second CIDR range operand for the union function | ||
cidr: Cidr, | ||
}, | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let cli = Cli::parse(); | ||
|
||
let mut fcidr: Fcidr = if let Some(cidr) = cli.cidr { | ||
Fcidr::new(cidr) | ||
} else { | ||
if stdin().is_terminal() { | ||
Cli::command().print_help().unwrap(); | ||
::std::process::exit(2); | ||
} | ||
stdin().lines().fold( | ||
Ok(Fcidr::default()), | ||
|fcidr: Result<Fcidr, Box<dyn Error>>, l| { | ||
if let Ok(mut fcidr) = fcidr { | ||
fcidr.union(l?.parse()?); | ||
return Ok(fcidr); | ||
} | ||
fcidr | ||
}, | ||
)? | ||
}; | ||
|
||
match cli.command { | ||
FcidrCommand::Complement => fcidr.complement(), | ||
FcidrCommand::Difference { cidr } => fcidr.difference(cidr), | ||
FcidrCommand::Union { cidr } => fcidr.union(cidr), | ||
}; | ||
|
||
for cidr in fcidr { | ||
println!("{cidr}"); | ||
} | ||
|
||
Ok(()) | ||
} |