-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd93fdc
commit 5e7a867
Showing
6 changed files
with
109 additions
and
0 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
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,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" |
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,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) | ||
} |
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,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.") | ||
} |
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,4 @@ | ||
mod help; | ||
|
||
pub const PERSEUS_VERSION: &str = "0.1.0"; | ||
pub use help::help; |