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

Allow setting more command-line options with environment variables #2161

Merged
merged 18 commits into from
Jun 14, 2024
253 changes: 133 additions & 120 deletions src/config.rs

Large diffs are not rendered by default.

141 changes: 68 additions & 73 deletions src/function.rs

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ pub use crate::run::run;
#[doc(hidden)]
pub use unindent::unindent;

pub(crate) type CompileResult<'a, T = ()> = Result<T, CompileError<'a>>;
pub(crate) type ConfigResult<T> = Result<T, ConfigError>;
pub(crate) type RunResult<'a, T = ()> = Result<T, Error<'a>>;
pub(crate) type SearchResult<T> = Result<T, SearchError>;
type CompileResult<'a, T = ()> = Result<T, CompileError<'a>>;
type ConfigResult<T> = Result<T, ConfigError>;
type FunctionResult = Result<String, String>;
type RunResult<'a, T = ()> = Result<T, Error<'a>>;
type SearchResult<T> = Result<T, SearchError>;

#[cfg(test)]
#[macro_use]
Expand Down
8 changes: 4 additions & 4 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl PlatformInterface for Platform {
Ok(cmd)
}

fn set_execute_permission(path: &Path) -> Result<(), io::Error> {
fn set_execute_permission(path: &Path) -> io::Result<()> {
use std::os::unix::fs::PermissionsExt;

// get current permissions
Expand All @@ -38,7 +38,7 @@ impl PlatformInterface for Platform {
exit_status.signal()
}

fn convert_native_path(_working_directory: &Path, path: &Path) -> Result<String, String> {
fn convert_native_path(_working_directory: &Path, path: &Path) -> FunctionResult {
path
.to_str()
.map(str::to_string)
Expand Down Expand Up @@ -85,7 +85,7 @@ impl PlatformInterface for Platform {
Ok(cmd)
}

fn set_execute_permission(_path: &Path) -> Result<(), io::Error> {
fn set_execute_permission(_path: &Path) -> io::Result<()> {
// it is not necessary to set an execute permission on a script on windows, so
// this is a nop
Ok(())
Expand All @@ -97,7 +97,7 @@ impl PlatformInterface for Platform {
None
}

fn convert_native_path(working_directory: &Path, path: &Path) -> Result<String, String> {
fn convert_native_path(working_directory: &Path, path: &Path) -> FunctionResult {
// Translate path from windows style to unix style
let mut cygpath = Command::new("cygpath");
cygpath.current_dir(working_directory);
Expand Down
4 changes: 2 additions & 2 deletions src/platform_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub(crate) trait PlatformInterface {
) -> Result<Command, OutputError>;

/// Set the execute permission on the file pointed to by `path`
fn set_execute_permission(path: &Path) -> Result<(), io::Error>;
fn set_execute_permission(path: &Path) -> io::Result<()>;

/// Extract the signal from a process exit status, if it was terminated by a
/// signal
fn signal_from_exit_status(exit_status: ExitStatus) -> Option<i32>;

/// Translate a path from a "native" path to a path the interpreter expects
fn convert_native_path(working_directory: &Path, path: &Path) -> Result<String, String>;
fn convert_native_path(working_directory: &Path, path: &Path) -> FunctionResult;
}
26 changes: 11 additions & 15 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ pub(crate) enum Subcommand {
}

impl Subcommand {
pub(crate) fn execute<'src>(
&self,
config: &Config,
loader: &'src Loader,
) -> Result<(), Error<'src>> {
pub(crate) fn execute<'src>(&self, config: &Config, loader: &'src Loader) -> RunResult<'src> {
use Subcommand::*;

match self {
Expand Down Expand Up @@ -107,7 +103,7 @@ impl Subcommand {
loader: &'src Loader,
arguments: &[String],
overrides: &BTreeMap<String, String>,
) -> Result<(), Error<'src>> {
) -> RunResult<'src> {
if matches!(
config.search_config,
SearchConfig::FromInvocationDirectory | SearchConfig::FromSearchDirectory { .. }
Expand Down Expand Up @@ -192,7 +188,7 @@ impl Subcommand {
config: &Config,
loader: &'src Loader,
search: &Search,
) -> Result<Compilation<'src>, Error<'src>> {
) -> RunResult<'src, Compilation<'src>> {
let compilation = Compiler::compile(config.unstable, loader, &search.justfile)?;

if config.verbosity.loud() {
Expand All @@ -214,7 +210,7 @@ impl Subcommand {
search: &Search,
overrides: &BTreeMap<String, String>,
chooser: Option<&str>,
) -> Result<(), Error<'src>> {
) -> RunResult<'src> {
let mut recipes = Vec::<&Recipe>::new();
let mut stack = vec![justfile];
while let Some(module) = stack.pop() {
Expand Down Expand Up @@ -304,7 +300,7 @@ impl Subcommand {
Ok(())
}

fn dump(config: &Config, ast: &Ast, justfile: &Justfile) -> Result<(), Error<'static>> {
fn dump(config: &Config, ast: &Ast, justfile: &Justfile) -> RunResult<'static> {
match config.dump_format {
DumpFormat::Json => {
serde_json::to_writer(io::stdout(), justfile)
Expand All @@ -316,7 +312,7 @@ impl Subcommand {
Ok(())
}

fn edit(search: &Search) -> Result<(), Error<'static>> {
fn edit(search: &Search) -> RunResult<'static> {
let editor = env::var_os("VISUAL")
.or_else(|| env::var_os("EDITOR"))
.unwrap_or_else(|| "vim".into());
Expand All @@ -338,7 +334,7 @@ impl Subcommand {
Ok(())
}

fn format(config: &Config, search: &Search, src: &str, ast: &Ast) -> Result<(), Error<'static>> {
fn format(config: &Config, search: &Search, src: &str, ast: &Ast) -> RunResult<'static> {
config.require_unstable("The `--fmt` command is currently unstable.")?;

let formatted = ast.to_string();
Expand Down Expand Up @@ -383,7 +379,7 @@ impl Subcommand {
Ok(())
}

fn init(config: &Config) -> Result<(), Error<'static>> {
fn init(config: &Config) -> RunResult<'static> {
let search = Search::init(&config.search_config, &config.invocation_directory)?;

if search.justfile.is_file() {
Expand All @@ -403,7 +399,7 @@ impl Subcommand {
}
}

fn man() -> Result<(), Error<'static>> {
fn man() -> RunResult<'static> {
let mut buffer = Vec::<u8>::new();

Man::new(Config::app())
Expand All @@ -423,7 +419,7 @@ impl Subcommand {
Ok(())
}

fn list(config: &Config, mut module: &Justfile, path: &ModulePath) -> Result<(), Error<'static>> {
fn list(config: &Config, mut module: &Justfile, path: &ModulePath) -> RunResult<'static> {
for name in &path.path {
module = module
.modules
Expand Down Expand Up @@ -585,7 +581,7 @@ impl Subcommand {
config: &Config,
mut module: &Justfile<'src>,
path: &ModulePath,
) -> Result<(), Error<'src>> {
) -> RunResult<'src> {
for name in &path.path[0..path.path.len() - 1] {
module = module
.modules
Expand Down
2 changes: 1 addition & 1 deletion src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod full {
};
}

pub fn summary(path: &Path) -> Result<Result<Summary, String>, io::Error> {
pub fn summary(path: &Path) -> io::Result<Result<Summary, String>> {
let loader = Loader::new();

match Compiler::compile(false, &loader, path) {
Expand Down
14 changes: 7 additions & 7 deletions src/thunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,42 @@ pub(crate) enum Thunk<'src> {
Nullary {
name: Name<'src>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
function: fn(function::Context) -> Result<String, String>,
function: fn(function::Context) -> FunctionResult,
},
Unary {
name: Name<'src>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
function: fn(function::Context, &str) -> Result<String, String>,
function: fn(function::Context, &str) -> FunctionResult,
arg: Box<Expression<'src>>,
},
UnaryOpt {
name: Name<'src>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
function: fn(function::Context, &str, Option<&str>) -> Result<String, String>,
function: fn(function::Context, &str, Option<&str>) -> FunctionResult,
args: (Box<Expression<'src>>, Box<Option<Expression<'src>>>),
},
UnaryPlus {
name: Name<'src>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
function: fn(function::Context, &str, &[String]) -> Result<String, String>,
function: fn(function::Context, &str, &[String]) -> FunctionResult,
args: (Box<Expression<'src>>, Vec<Expression<'src>>),
},
Binary {
name: Name<'src>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
function: fn(function::Context, &str, &str) -> Result<String, String>,
function: fn(function::Context, &str, &str) -> FunctionResult,
args: [Box<Expression<'src>>; 2],
},
BinaryPlus {
name: Name<'src>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
function: fn(function::Context, &str, &str, &[String]) -> Result<String, String>,
function: fn(function::Context, &str, &str, &[String]) -> FunctionResult,
args: ([Box<Expression<'src>>; 2], Vec<Expression<'src>>),
},
Ternary {
name: Name<'src>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
function: fn(function::Context, &str, &str, &str) -> Result<String, String>,
function: fn(function::Context, &str, &str, &str) -> FunctionResult,
args: [Box<Expression<'src>>; 3],
},
}
Expand Down