Skip to content

Commit

Permalink
Clean code after feedback from u/Compux72
Browse files Browse the repository at this point in the history
  • Loading branch information
JensGM committed May 27, 2024
1 parent 2c721f3 commit 60022bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pushenv"
version = "1.1.1"
version = "1.1.2"
edition = "2021"
description = """
A CLI utility that reads a .env file before starting a process.
Expand Down
26 changes: 8 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::OsString;
use clap::Parser as _;

#[derive(clap::Parser, Debug)]
Expand All @@ -6,7 +7,7 @@ use clap::Parser as _;
struct Cli {
envfile: Option<String>,
#[clap(last = true)]
cmd: Vec<String>,
cmd: Vec<OsString>,
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -20,14 +21,14 @@ enum PushEnvError {
}

#[cfg(unix)]
fn exec(cmd: &str, args: &[String]) -> Result<(), PushEnvError> {
fn exec(cmd: &OsString, args: &[OsString]) -> Result<(), PushEnvError> {
use std::os::unix::process::CommandExt as _;
let err = std::process::Command::new(cmd).args(args).exec();
Err(err.into())
}

#[cfg(windows)]
fn exec(cmd: &str, args: &[String]) -> Result<(), PushEnvError> {
fn exec(cmd: &OsString, args: &[OsString]) -> Result<(), PushEnvError> {
let status = std::process::Command::new(cmd)
.args(args)
.stdin(std::process::Stdio::inherit())
Expand All @@ -44,23 +45,12 @@ fn exec(cmd: &str, args: &[String]) -> Result<(), PushEnvError> {
}
}

fn main2() -> Result<(), PushEnvError> {
fn main() -> Result<(), PushEnvError> {
let args = Cli::parse();
dotenv::from_filename(args.envfile.unwrap_or(".env".to_string()))?;

let cmd = match args.cmd.first() {
Some(cmd) => cmd,
None => return Err(PushEnvError::MissingCommand),
};

let args = &args.cmd[1..];
exec(cmd, args)
}

fn main() -> () {
if let Err(e) = main2() {
eprintln!("{}", e);
std::process::exit(1);
match args.cmd.as_slice() {
[cmd, args@..] => exec(cmd, args),
_ => Err(PushEnvError::MissingCommand),
}
}

Expand Down

0 comments on commit 60022bb

Please sign in to comment.