Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: customisable task entrypoint #86

Merged
merged 6 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ tui = {version = "0.22.0", package = "ratatui"}
url = "2.4.0"
assert_cmd = "2.0.12"
semver = "1.0.18"
shlex = "1.1.0"
54 changes: 45 additions & 9 deletions src/actors/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::{
path::PathBuf,
};

use shlex;

use crate::config::{
pipe::{OutputRedirection, Pipe},
Config, Task,
Expand Down Expand Up @@ -109,6 +111,7 @@ pub struct CommandActor {
started_at: DateTime<Local>,
env: Vec<(String, String)>,
pipes: Vec<Pipe>,
entrypoint: Option<String>,
}

impl CommandActor {
Expand Down Expand Up @@ -164,6 +167,7 @@ impl CommandActor {
verbose,
env.into_iter().collect(),
task_pipes,
op.entrypoint.clone(),
)
.start();

Expand All @@ -187,6 +191,7 @@ impl CommandActor {
verbose: bool,
env: Vec<(String, String)>,
pipes: Vec<Pipe>,
entrypoint: Option<String>,
) -> Self {
Self {
op_name,
Expand All @@ -203,6 +208,7 @@ impl CommandActor {
started_at: Local::now(),
env,
pipes,
entrypoint,
}
}

Expand Down Expand Up @@ -250,17 +256,47 @@ impl CommandActor {
fn reload(&mut self) -> Result<()> {
let args = &self.operator.command;

self.log_debug(format!("EXEC: {} at {:?}", args, self.cwd));
self.console.do_send(PanelStatus {
panel_name: self.op_name.clone(),
status: None,
});
let default_entrypoint = {
#[cfg(not(target_os = "windows"))]
{"bash -c"}

#[cfg(target_os = "windows")]
{"cmd /c"}
};

let exec = {
let entrypoint_lex = match &self.entrypoint {
Some(e) => if !e.is_empty() { e.as_str() } else { default_entrypoint },
None => default_entrypoint,
};

let entrypoint_split = {
let mut s = shlex::split(entrypoint_lex).unwrap();

#[cfg(target_os = "windows")]
let exec = Exec::cmd("cmd").args(&["/c", args]);
match args {
Some(a) => {
s.push(a.to_owned());
s
},
None => s,
}
};

#[cfg(not(target_os = "windows"))]
let exec = Exec::cmd("bash").args(&["-c", args]);
let entrypoint = &entrypoint_split[0];
let nargs: Vec<String> = entrypoint_split[1..]
.to_owned()
.into_iter()
.filter(|s| !s.is_empty())
.collect();

self.log_debug(format!("EXEC: {} {:?} at {:?}", entrypoint_lex, nargs, self.cwd));
self.console.do_send(PanelStatus {
panel_name: self.op_name.clone(),
status: None,
});

Exec::cmd(entrypoint).args(&nargs)
};

let mut p = exec
.cwd(&self.cwd)
Expand Down
5 changes: 3 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl<T: std::clone::Clone> Lift<T> {
#[serde(deny_unknown_fields)]
pub struct Task {
pub workdir: Option<String>,
pub command: String,
pub command: Option<String>,
pub entrypoint: Option<String>,

#[serde(default)]
pub watch: Lift<String>,
Expand Down Expand Up @@ -397,7 +398,7 @@ mod tests {
);

let job_with_alias = config.ops.get("with_alias").unwrap();
assert_eq!(&job_with_alias.command, "echo with_alias");
assert_eq!(&job_with_alias.command.clone().unwrap(), "echo with_alias");
}
}

Expand Down
18 changes: 18 additions & 0 deletions whiz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ once:
A: world
command: "echo hello $A"

entrypoint_only:
entrypoint: "ls ./src ./src/config"
command: ''

once_b:
command: "sleep 2 && echo $B"
depends_on: once
Expand All @@ -35,6 +39,12 @@ sleep_count:
depends_on:
- once

no_command:
entrypoint: python3 -c "print(\"No command specified\")"

no_explicit_entrypoint:
command: echo "there's no entrypoint :("

dodo:
workdir: .
watch: target
Expand All @@ -45,6 +55,14 @@ dodo:
color:
command: python3 -c 'print(u"\u001b[31mHelloWorld\u001b[0m")'

color2:
command: 'print(u"\033[92mHello World\033[0m")'
entrypoint: 'python3 -c'

what_shell:
command: echo $(which $0)
entrypoint: 'sh -c'

watch_parent:
workdir: src
command: ls
Expand Down