Skip to content

Commit

Permalink
feat: ✨ added basic cli
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Aug 22, 2021
1 parent cd93fdc commit 5e7a867
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"packages/perseus",
"packages/perseus-actix-web",
"packages/perseus-cli",
"examples/showcase/app",
"examples/showcase/server-actix-web"
]
5 changes: 5 additions & 0 deletions bonnie.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ dev.subcommands.serve.cmd = [
"bonnie serve %server"
]
dev.subcommands.serve.args = [ "server" ]
# TODO fix this with an example
dev.subcommands.cli = [
"cd packages/perseus-cli",
"cargo watch -x \"run --bin cli\""
]
build = "cargo build"
test = "cargo watch -x \"test\""
check = "cargo check && cargo fmt -- --check && cargo clippy && cargo test" # This will be run on CI as well
Expand Down
17 changes: 17 additions & 0 deletions packages/perseus-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "perseus-cli"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
perseus = { path = "../perseus" }
perseus-actix-web = { path = "../perseus-actix-web" }

[lib]
name = "lib"

[[bin]]
name = "cli"
path = "src/bin/main.rs"
60 changes: 60 additions & 0 deletions packages/perseus-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::env;
use std::io::Write;
use lib::{PERSEUS_VERSION, help};

// All this does is run the program and terminate with the acquired exit code
fn main() {
let exit_code = real_main();
std::process::exit(exit_code)
}

// This manages error handling and returns a definite exit code to terminate with
fn real_main() -> i32 {
let res = core();
match res {
// If it worked, we pass the executed command's exit code through
Ok(exit_code) => exit_code,
// If something failed, we print the error to `stderr` and return a failure exit code
Err(err) => {
eprintln!("{}", err);
1
}
}
}

// This performs the actual logic, separated for deduplication of error handling and destructor control
// This returns the exit code of the executed command, which we should return from the process itself
// This prints warnings using the `writeln!` macro, which allows the parsing of `stdout` in production or a vector in testing
// If at any point a warning can't be printed, the program will panic
fn core() -> Result<i32, String> {
// Get `stdout` so we can write warnings appropriately
let stdout = &mut std::io::stdout();
// Get the arguments to this program, removing the first one (something like `perseus`)
let mut prog_args: Vec<String> = env::args().collect();
// This will panic if the first argument is not found (which is probably someone trying to fuzz us)
let _executable_name = prog_args.remove(0);
// Check for special arguments
if matches!(prog_args.get(0), Some(_)) {
if prog_args[0] == "-v" || prog_args[0] == "--version" {
writeln!(stdout, "You are currently running the Perseus CLI v{}! You can see the latest release at https://github.com/arctic-hen7/perseus/releases.", PERSEUS_VERSION).expect("Failed to write version.");
return Ok(0);
} else if prog_args[0] == "-h" || prog_args[0] == "--help" {
help(stdout);
return Ok(0);
}
// Now we're checking commands
else if prog_args[0] == "build" {
todo!("build command")
} else if prog_args[0] == "serve" {
todo!("serve command")
} else {
writeln!(stdout, "Unknown command '{}'. You can see the help page with -h/--help.", prog_args[0]);
return Ok(1);
}
} else {
writeln!(stdout, "Please provide a command to run, or use -h/--help to see the help page.");
return Ok(1);
}

Ok(0)
}
22 changes: 22 additions & 0 deletions packages/perseus-cli/src/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::PERSEUS_VERSION;

pub fn help(output: &mut impl std::io::Write) {
writeln!(
output,
"Perseus v{version} help page:
-------------------------
This is the CLI for Perseus, a super-fast WebAssembly frontend development framework! For the full reference, please see the documentation at https://arctic-hen7.github.io/perseus.
-h, --help prints this help page
-v, --version prints the current version of the CLI
build builds your app (-p/--prod for production, -w/--watch to watch files)
serve serves your app (accepts $PORT and $HOST env vars)
Further information can be found at https://arctic-hen7.github.io/perseus.
",
version = PERSEUS_VERSION
)
.expect("Failed to write help page.")
}
4 changes: 4 additions & 0 deletions packages/perseus-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod help;

pub const PERSEUS_VERSION: &str = "0.1.0";
pub use help::help;

0 comments on commit 5e7a867

Please sign in to comment.