Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove bashify experiment
Browse files Browse the repository at this point in the history
The prior implementation was overkill and the interface not really that helpful (except for forcing people to think about quoting and escaping).
schneems committed Jul 6, 2023
1 parent 852548c commit d018ea6
Showing 3 changed files with 22 additions and 85 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion buildpacks/ruby/Cargo.toml
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ libherokubuildpack = "0.13"
rand = "0.8"
regex = "1"
serde = "1"
shell-words = "1.1.0"
tar = "0.4"
tempfile = "3"
thiserror = "1"
99 changes: 22 additions & 77 deletions buildpacks/ruby/src/steps/get_default_process.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ use libcnb::data::launch::Process;
use libcnb::data::launch::ProcessBuilder;
use libcnb::data::process_type;
use libherokubuildpack::log as user;
use std::fmt::Display;
use std::path::Path;

use crate::RubyBuildpack;
@@ -64,86 +63,32 @@ fn detect_web(gem_list: &GemList, app_path: &Path) -> WebProcess {
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
enum ShellString {
Escape(String),
QuoteEnvVar(String),
}

impl ShellString {
fn escape(arg: impl Into<String>) -> ShellString {
ShellString::Escape(arg.into())
}

fn quote_env_var(arg: impl Into<String>) -> ShellString {
ShellString::QuoteEnvVar(arg.into())
}
}

impl Display for ShellString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ShellString::Escape(string) => f.write_str(&shell_words::quote(string)), // single quote only if needed
ShellString::QuoteEnvVar(string) => write!(f, "\"{string}\""),
}
}
}

fn bashify(program: &ShellString, args: impl IntoIterator<Item = ShellString>) -> Process {
let args = args
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join(" ");
let command = [String::from("exec"), program.to_string(), args].join(" ");

fn default_rack() -> Process {
ProcessBuilder::new(process_type!("web"), ["bash"])
.args(["-c", &command])
.args([
"-c",
&[
"bundle exec rackup",
"--port \"$PORT\"",
"--host \"0.0.0.0\"",
]
.join(" "),
])
.default(true)
.build()
}

fn default_rack() -> Process {
bashify(
&ShellString::escape("bundle"),
[
ShellString::escape("exec"),
ShellString::escape("rackup"),
ShellString::escape("--port"),
ShellString::quote_env_var("$PORT"),
ShellString::escape("--host"),
ShellString::escape("0.0.0.0"),
],
)
}

fn default_rails() -> Process {
bashify(
&ShellString::escape("bin/rails"),
[
ShellString::escape("server"),
ShellString::escape("--port"),
ShellString::quote_env_var("$PORT"),
ShellString::escape("--environment"),
ShellString::quote_env_var("$RAILS_ENV"),
],
)
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn shell_quoting() {
assert_eq!(
String::from("\"$PORT\""),
ShellString::quote_env_var("$PORT").to_string()
);

assert_eq!(
String::from("'hello there'"),
ShellString::escape("hello there").to_string()
);
}
ProcessBuilder::new(process_type!("web"), ["bash"])
.args([
"-c",
&[
"bin/rails server",
"--port \"$PORT\"",
"--environment \"$RAILS_ENV\"",
]
.join(" "),
])
.default(true)
.build()
}

0 comments on commit d018ea6

Please sign in to comment.