Skip to content

Commit

Permalink
chore: uppercase app name in build script instead of using literal value
Browse files Browse the repository at this point in the history
This is the simplest way i could find to uppercase at compile-time
  • Loading branch information
sandorex committed Sep 13, 2024
1 parent d4c0556 commit 52ea247
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 18 deletions.
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ fn main() -> anyhow::Result<()> {
.sha(false)
.build()?;

// cause i cannot figure out how to uppercase a str literal at compile time
println!("cargo::rustc-env=CARGO_PKG_NAME_UPPERCASE={}", env!("CARGO_PKG_NAME").to_ascii_uppercase());

Emitter::default()
.add_instructions(&git2)?
.emit()

}
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const AFTER_HELP: &str = concat!(

/// Sandboxed development container manager, with focus on security by default
#[derive(Parser, Debug)]
#[command(name = crate::BIN_NAME, author, version = FULL_VERSION, about, after_help = AFTER_HELP)]
#[command(name = crate::APP_NAME, author, version = FULL_VERSION, about, after_help = AFTER_HELP)]
pub struct Cli {
/// Explicitly set container engine to use
#[arg(long, env = ENV_VAR_PREFIX!("ENGINE"))]
Expand Down Expand Up @@ -202,8 +202,8 @@ mod tests {
#[test]
fn verify_env_var_prefix() {
// just for my sanity check if i forgot to update them
assert_eq!(ENV_VAR_PREFIX!("A"), format!("{}_A", crate::BIN_NAME_UPPERCASE));
assert_eq!(crate::BIN_NAME.to_uppercase(), crate::BIN_NAME_UPPERCASE);
assert_eq!(ENV_VAR_PREFIX!("A"), format!("{}_A", crate::APP_NAME_UPPERCASE));
assert_eq!(crate::APP_NAME.to_uppercase(), crate::APP_NAME_UPPERCASE);
}
}

4 changes: 2 additions & 2 deletions src/commands/cmd_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn container_exec(engine: Engine, dry_run: bool, mut cli_args: cli::CmdExecA
// allow dry_run to work
None if dry_run => "/ws/dry_run".to_string(),
None => {
eprintln!("Container {:?} is not owned by {}", &cli_args.name, crate::BIN_NAME);
eprintln!("Container {:?} is not owned by {}", &cli_args.name, crate::APP_NAME);

return Err(1);
}
Expand All @@ -49,7 +49,7 @@ pub fn container_exec(engine: Engine, dry_run: bool, mut cli_args: cli::CmdExecA
]);

// run the command as one big concatenated string
cmd.arg(&cli_args.command.join(" "));
cmd.arg(cli_args.command.join(" "));
} else {
cmd.arg(&cli_args.name);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn kill_container(engine: Engine, dry_run: bool, mut cli_args: cli::CmdKillA

// check if container is owned
if util::get_container_ws(&engine, &cli_args.name).is_none() {
eprintln!("Container {:?} is not owned by {}", &cli_args.name, crate::BIN_NAME);
eprintln!("Container {:?} is not owned by {}", &cli_args.name, crate::APP_NAME);

return Err(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn print_containers(engine: Engine, dry_run: bool) -> ExitResult {
let mut cmd = Command::new(&engine.path);
cmd.args([
"container", "ls",
"--filter", format!("label={}", crate::BIN_NAME).as_str(),
"--filter", format!("label={}", crate::APP_NAME).as_str(),
"--format", "{{.Names}}|{{.Image}}|{{.Labels.host_dir}}|{{.Ports}}"
]);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn open_shell(engine: Engine, dry_run: bool, mut cli_args: cli::CmdShellArgs
// allow dry_run to work
None if dry_run => "/ws/dry_run".to_string(),
None => {
eprintln!("Container {:?} is not owned by {}", &cli_args.name, crate::BIN_NAME);
eprintln!("Container {:?} is not owned by {}", &cli_args.name, crate::APP_NAME);

return Err(1);
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/cmd_start.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::Config;
use crate::{ExitResult, VERSION, ENV_VAR_PREFIX, BIN_NAME};
use crate::{ExitResult, VERSION, ENV_VAR_PREFIX, APP_NAME};
use crate::util::{self, Engine, EngineKind};
use crate::util::command_extensions::*;
use crate::cli;
Expand Down Expand Up @@ -33,7 +33,7 @@ fn generate_name() -> String {

// allow custom container suffix but default to bin name
let suffix = std::env::var(ENV_VAR_PREFIX!("CONTAINER_SUFFIX"))
.unwrap_or_else(|_| BIN_NAME.to_string());
.unwrap_or_else(|_| APP_NAME.to_string());

format!("{}-{}", adjective, suffix)
}
Expand Down Expand Up @@ -220,9 +220,9 @@ pub fn start_container(engine: Engine, dry_run: bool, mut cli_args: cli::CmdStar
cmd.args([
// TODO add display for engine so that its prints lowercase
format!("--label=manager={:?}", engine.kind),
format!("--label={}={}", BIN_NAME, main_project_dir),
format!("--label={}={}", APP_NAME, main_project_dir),
format!("--label=host_dir={}", cwd.to_string_lossy()),
format!("--env={0}={0}", BIN_NAME),
format!("--env={0}={0}", APP_NAME),
format!("--name={}", container_name),
format!("--env={}={}", ENV_VAR_PREFIX!("VERSION"), VERSION),
format!("--env=manager={:?}", engine.kind),
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ pub const ENGINE_ERR_MSG: &str = "Failed to execute engine";
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const FULL_VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "-", env!("VERGEN_GIT_DESCRIBE"));

pub const BIN_NAME: &str = env!("CARGO_BIN_NAME");

// TODO find a way to uppercase bin name at compile time
pub const BIN_NAME_UPPERCASE: &str = "ARCAM";
pub const APP_NAME: &str = env!("CARGO_PKG_NAME");
pub const APP_NAME_UPPERCASE: &str = env!("CARGO_PKG_NAME_UPPERCASE");

/// Prefix env var name with proper prefix
#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn app_dir() -> PathBuf {
};

// use bin name for dir name
PathBuf::from(xdg_config_home).join(crate::BIN_NAME)
PathBuf::from(xdg_config_home).join(crate::APP_NAME)
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn container_exists(engine: &Engine, container: &str) -> bool {
}

pub fn get_container_ws(engine: &Engine, container: &str) -> Option<String> {
let key = format!(".Config.Labels.{}", crate::BIN_NAME);
let key = format!(".Config.Labels.{}", crate::APP_NAME);

// this looks like a mess as i need to escape curly braces
//
Expand Down

0 comments on commit 52ea247

Please sign in to comment.