Skip to content

Commit

Permalink
feat(rust): add binary to compile branded command binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Jan 14, 2025
1 parent 39ccf63 commit bcd1b25
Show file tree
Hide file tree
Showing 44 changed files with 878 additions and 362 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::cli_state::CliStateError;
use crate::logs::ExportingEnabled;
use crate::terminal::notification::Notification;

pub const OCKAM_HOME: &str = "OCKAM_HOME";

/// Maximum number of notifications present in the channel
const NOTIFICATIONS_CHANNEL_CAPACITY: usize = 16;

Expand Down Expand Up @@ -331,7 +333,7 @@ impl CliState {
/// That directory is determined by `OCKAM_HOME` environment variable and is
/// $OCKAM_HOME/.ockam.
///
/// If $OCKAM_HOME is not defined then $HOME is used instead
/// If $OCKAM_HOME is not defined, then $HOME is used instead
pub(super) fn default_dir() -> Result<PathBuf> {
Ok(get_env_with_default::<PathBuf>(
"OCKAM_HOME",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub const DEFAULT_CONTROLLER_ADDRESS: &str = "/dnsaddr/orchestrator.ockam.io/tcp
/// from ./static/controller.id.
/// How to use: when running a command that spawns a background node or use an embedded node
/// add the env variable. `OCKAM_CONTROLLER_IDENTITY_ID={identity.id-contents} ockam ...`
pub(crate) const OCKAM_CONTROLLER_IDENTITY_ID: &str = "OCKAM_CONTROLLER_IDENTITY_ID";
pub const OCKAM_CONTROLLER_IDENTITY_ID: &str = "OCKAM_CONTROLLER_IDENTITY_ID";

/// Total time to wait for Orchestrator long-running operations to complete
pub const ORCHESTRATOR_AWAIT_TIMEOUT: Duration = Duration::from_secs(60 * 10);
Expand Down
33 changes: 27 additions & 6 deletions implementations/rust/ockam/ockam_api/src/ui/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,36 @@ impl<T: TerminalWriter + Debug, W> Terminal<T, W> {
pub struct TerminalStream<T: Write + Debug + Clone> {
pub writer: T,
pub no_color: bool,
bin_name: String,
brand_name: String,
}

impl<T: Write + Debug + Clone> TerminalStream<T> {
pub fn prepare_msg(&self, msg: impl AsRef<str>) -> Result<String> {
let msg = msg.as_ref().to_string();
let mut msg = if self.brand_name != "Ockam" {
msg.replace("Ockam", &self.brand_name)
} else {
msg
};
msg = if self.bin_name != "ockam" {
msg.replace("ockam", &self.bin_name)
} else {
msg
};

if self.no_color {
Ok(strip_ansi_escapes::strip_str(msg.as_ref()))
Ok(strip_ansi_escapes::strip_str(&msg))
} else {
Ok(msg.as_ref().to_string())
Ok(msg)
}
}
}

/// Trait defining the main methods to write messages to a terminal stream.
pub trait TerminalWriter: Clone {
fn stdout(no_color: bool) -> Self;
fn stderr(no_color: bool) -> Self;
fn stdout(no_color: bool, bin_name: impl Into<String>, brand_name: impl Into<String>) -> Self;
fn stderr(no_color: bool, bin_name: impl Into<String>, brand_name: impl Into<String>) -> Self;
fn is_tty(&self) -> bool;
fn color(&self) -> bool;

Expand All @@ -95,18 +109,23 @@ pub trait TerminalWriter: Clone {

// Core functions
impl<W: TerminalWriter + Debug> Terminal<W> {
#[allow(clippy::too_many_arguments)]
pub fn new(
logging_enabled: bool,
logging_goes_to_file: bool,
quiet: bool,
no_color: bool,
no_input: bool,
output_format: OutputFormat,
bin_name: impl Into<String>,
brand_name: impl Into<String>,
) -> Self {
let bin_name = bin_name.into();
let brand_name = brand_name.into();
let no_color = Self::should_disable_color(no_color);
let no_input = Self::should_disable_user_input(no_input);
let stdout = W::stdout(no_color);
let stderr = W::stderr(no_color);
let stdout = W::stdout(no_color, &bin_name, &brand_name);
let stderr = W::stderr(no_color, bin_name, brand_name);
let max_width_col_count = get_size().map(|it| it.col_count).unwrap_or(ch!(80)).into();
Self {
stdout,
Expand All @@ -130,6 +149,8 @@ impl<W: TerminalWriter + Debug> Terminal<W> {
false,
false,
OutputFormat::Plain,
"ockam",
"Ockam",
)
}

Expand Down
30 changes: 24 additions & 6 deletions implementations/rust/ockam/ockam_api/src/ui/terminal/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ use crate::terminal::{TerminalStream, TerminalWriter};
use crate::Result;

impl TerminalWriter for TerminalStream<Term> {
fn stdout(no_color: bool) -> Self {
fn stdout(no_color: bool, bin_name: impl Into<String>, brand_name: impl Into<String>) -> Self {
let writer = Term::stdout();
let no_color = no_color || !writer.features().colors_supported();
Self { writer, no_color }
Self {
writer,
no_color,
bin_name: bin_name.into(),
brand_name: brand_name.into(),
}
}

fn stderr(no_color: bool) -> Self {
fn stderr(no_color: bool, bin_name: impl Into<String>, brand_name: impl Into<String>) -> Self {
let writer = Term::stderr();
let no_color = no_color || !writer.features().colors_supported();
Self { writer, no_color }
Self {
writer,
no_color,
bin_name: bin_name.into(),
brand_name: brand_name.into(),
}
}

fn is_tty(&self) -> bool {
Expand Down Expand Up @@ -57,8 +67,16 @@ mod tests {

#[test]
fn test_write() {
let sut: Terminal<TerminalStream<Term>> =
Terminal::new(false, false, false, false, false, OutputFormat::Plain);
let sut: Terminal<TerminalStream<Term>> = Terminal::new(
false,
false,
false,
false,
false,
OutputFormat::Plain,
"",
"",
);
sut.write("1").unwrap();
sut.rewrite("1-r\n").unwrap();
sut.write_line("2".red().to_string()).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions implementations/rust/ockam/ockam_command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cfg_aliases = "0.2.1"
[dependencies]
arboard = "3.4.1"
async-trait = "0.1"
cfg-if = "1.0.0"
clap = { version = "4.5", features = ["derive", "cargo", "wrap_help"] }
clap_complete = "4.5.28"
clap_mangen = "0.2.23"
Expand Down Expand Up @@ -103,14 +104,13 @@ assert_cmd = "2"
mockito = "1.5.0"
ockam_api = { path = "../ockam_api", version = "0.89.0", default-features = false, features = ["test-utils"] }
ockam_macros = { path = "../ockam_macros", version = "^0.36.0" }
proptest = "1.5.0"
serial_test = "3.0.0"
tempfile = "3.10.1"
time = { version = "0.3", default-features = false, features = ["std", "local-offset"] }

[features]
default = ["orchestrator", "rust-crypto", "privileged_portals"]
default = ["rust-crypto", "privileged_portals", "admin_commands", "advanced_commands"]
privileged_portals = ["ockam_api/privileged_portals"]
orchestrator = []
aws-lc = ["ockam_vault/aws-lc", "ockam_api/aws-lc", "rustls/aws-lc-rs"]
rust-crypto = ["ockam_vault/rust-crypto", "ockam_api/rust-crypto", "rustls/ring"]
admin_commands = []
advanced_commands = []
17 changes: 17 additions & 0 deletions implementations/rust/ockam/ockam_command/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cfg_aliases::cfg_aliases;
use std::env;
use std::process::Command;

fn hash() {
Expand All @@ -10,8 +11,24 @@ fn hash() {
println!("cargo:rustc-env=GIT_HASH={git_hash}");
}

fn binary_name() {
let bin_name = env::var("OCKAM_COMMAND_BIN_NAME").unwrap_or("ockam".to_string());
println!("cargo:rustc-env=OCKAM_COMMAND_BIN_NAME={bin_name}");
println!("cargo:rerun-if-env-changed=OCKAM_COMMAND_BIN_NAME");
let brand_name = env::var("OCKAM_COMMAND_BRAND_NAME").unwrap_or("Ockam".to_string());
println!("cargo:rustc-env=OCKAM_COMMAND_BRAND_NAME={brand_name}");
let home_dir = env::var("OCKAM_HOME").unwrap_or("".to_string());
println!("cargo:rustc-env=OCKAM_HOME={home_dir}");
let orchestrator_identifier =
env::var("OCKAM_CONTROLLER_IDENTITY_ID").unwrap_or("".to_string());
println!("cargo:rustc-env=OCKAM_CONTROLLER_IDENTITY_ID={orchestrator_identifier}");
let orchestrator_address = env::var("OCKAM_CONTROLLER_ADDR").unwrap_or("".to_string());
println!("cargo:rustc-env=OCKAM_CONTROLLER_ADDR={orchestrator_address}");
}

fn main() {
hash();
binary_name();
cfg_aliases! {
privileged_portals_support: { all(target_os = "linux", feature = "privileged_portals") }
}
Expand Down
Loading

0 comments on commit bcd1b25

Please sign in to comment.