-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
5 changed files
with
227 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,3 +6,9 @@ members = ["crates/*"] | |
license = "MIT" | ||
edition = "2021" | ||
authors = ["Miguel Piedrafita <[email protected]>"] | ||
|
||
[profile.release] | ||
lto = true | ||
strip = true | ||
opt-level = 3 | ||
panic = "abort" |
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 |
---|---|---|
@@ -1,14 +1,21 @@ | ||
[package] | ||
version = "0.1.0" | ||
name = "orbit-cli" | ||
categories = ["command-line-utilities"] | ||
|
||
edition.workspace = true | ||
license.workspace = true | ||
authors.workspace = true | ||
|
||
[dependencies] | ||
url = "2.5.2" | ||
log = "0.4.22" | ||
anyhow = "1.0.86" | ||
console = "0.15.8" | ||
pin-utils = "0.1.0" | ||
futures-util = "0.3.30" | ||
tokio = { version = "1.38.1", features = ["full"] } | ||
tokio = { version = "=1.29", features = ["full"] } | ||
fern = { version = "0.6.2", features = ["colored"] } | ||
orbit-types = { version = "0.1.0", path = "../types" } | ||
orbit-client = { version = "0.1.0", path = "../client" } | ||
clap = { version = "4.5.9", features = ["derive", "env"] } |
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,81 @@ | ||
use fern::colors::{Color, ColoredLevelConfig}; | ||
use log::{Level, LevelFilter}; | ||
|
||
pub fn set_hook() { | ||
// setup a panic hook to easily exit the program on panic | ||
std::panic::set_hook(Box::new(|panic_info| { | ||
// print the panic message | ||
let message = panic_info.payload().downcast_ref::<String>().map_or_else( | ||
|| { | ||
panic_info.payload().downcast_ref::<&str>().map_or_else( | ||
|| format!("{panic_info:?}"), | ||
|message| (*message).to_string(), | ||
) | ||
}, | ||
Clone::clone, | ||
); | ||
|
||
// add some color | ||
log::error!("{message}"); | ||
|
||
#[cfg(debug_assertions)] | ||
log::debug!("{panic_info}"); | ||
|
||
std::process::exit(1); | ||
})); | ||
} | ||
|
||
pub fn logs(verbose: bool) { | ||
let colors = ColoredLevelConfig::new() | ||
.info(Color::BrightCyan) | ||
.error(Color::BrightRed) | ||
.warn(Color::BrightYellow) | ||
.debug(Color::BrightWhite); | ||
|
||
fern::Dispatch::new() | ||
.format(move |out, message, record| { | ||
let level = record.level(); | ||
|
||
match level { | ||
Level::Debug => out.finish(format_args!( | ||
"{} [{}]: {}", | ||
colors.color(Level::Debug).to_string().to_lowercase(), | ||
record.target(), | ||
message | ||
)), | ||
|
||
level => out.finish(format_args!( | ||
"{}: {}", | ||
colors.color(level).to_string().to_lowercase(), | ||
message | ||
)), | ||
} | ||
}) | ||
.level(if verbose { | ||
LevelFilter::Debug | ||
} else { | ||
LevelFilter::Info | ||
}) | ||
.chain( | ||
fern::Dispatch::new() | ||
.filter(|metadata| !matches!(metadata.level(), Level::Error | Level::Warn)) | ||
.chain(std::io::stdout()), | ||
) | ||
.chain( | ||
fern::Dispatch::new() | ||
.level(log::LevelFilter::Error) | ||
.level(log::LevelFilter::Warn) | ||
.chain(std::io::stderr()), | ||
) | ||
.apply() | ||
.ok(); | ||
} | ||
|
||
pub fn clean_term() { | ||
let term = console::Term::stdout(); | ||
|
||
// if the terminal is a tty, clear the screen and reset the cursor | ||
if term.is_term() { | ||
term.show_cursor().ok(); | ||
} | ||
} |