Skip to content

Commit

Permalink
Merge pull request #37 from yhakbar/feature/modeling_config_better
Browse files Browse the repository at this point in the history
Modeling config a bit better
  • Loading branch information
yhakbar committed Jul 19, 2023
2 parents f36f46a + 2655e1b commit 8c990aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
19 changes: 14 additions & 5 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use colored::Colorize;
use serde_yaml::Value;
use std::process::Command;

use crate::config::{parse_cmd, resolve_chdir, ParsedCommand, ParsedConfig};
use crate::config::{parse_cmd, resolve_chdir, FullCommand, ParsedConfig, CommandType};

pub fn run_command_from_config(
config: &Value,
Expand Down Expand Up @@ -41,11 +41,20 @@ fn run_command(
chdir,
} = command;

let ParsedCommand {
let full_command = match parsed_command {
CommandType::SimpleCommand(cmd) => FullCommand {
prog: "bash".to_string(),
args: vec!["-c".to_string()],
cmd: Some(cmd),
},
CommandType::FullCommand(cmd) => cmd,
};

let FullCommand {
ref prog,
ref args,
ref cmd,
} = parsed_command;
} = full_command;

let cmd = cmd.clone();

Expand All @@ -62,7 +71,7 @@ fn run_command(
}

if execution {
let mut parsed_command = format!("$ {}", parsed_command);
let mut parsed_command = format!("$ {}", full_command);
if !no_color {
parsed_command = parsed_command.blue().bold().to_string();
}
Expand Down Expand Up @@ -91,7 +100,7 @@ fn run_command(
let result = command_builder.spawn()?.wait()?;

if !result.success() {
let msg = format!("Command `{}` failed with status {}", parsed_command, result);
let msg = format!("Command `{}` failed with status {}", full_command, result);
return Err(anyhow::anyhow!("{}", msg));
}

Expand Down
29 changes: 16 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,26 @@ pub fn print_config_from_file(path: &Path) -> anyhow::Result<()> {
}

pub struct ParsedConfig {
pub parsed_command: ParsedCommand,
pub parsed_command: CommandType,
pub pre_msg: Option<String>,
pub post_msg: Option<String>,
pub pre_cmds: Option<Vec<String>>,
pub post_cmds: Option<Vec<String>>,
pub chdir: Option<String>,
}

pub struct ParsedCommand {
pub struct FullCommand {
pub prog: String,
pub args: Vec<String>,
pub cmd: Option<String>,
}

impl std::fmt::Display for ParsedCommand {
pub enum CommandType {
SimpleCommand(String),
FullCommand(FullCommand),
}

impl std::fmt::Display for FullCommand {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut display = self.prog.clone();
for arg in self.args.iter() {
Expand All @@ -125,11 +130,7 @@ pub fn parse_cmd(cmd: &Value) -> anyhow::Result<ParsedConfig> {

match cmd {
Value::String(s) => Ok(ParsedConfig {
parsed_command: ParsedCommand {
prog: DEFAULT_PROG_VALUE.to_string(),
args: DEFAULT_ARGS_VALUE.iter().map(|s| s.to_string()).collect(),
cmd: Some(s.to_string()),
},
parsed_command: CommandType::SimpleCommand(s.to_string()),
pre_msg: None,
post_msg: None,
pre_cmds: None,
Expand Down Expand Up @@ -242,11 +243,13 @@ pub fn parse_cmd(cmd: &Value) -> anyhow::Result<ParsedConfig> {
.transpose()?;

Ok(ParsedConfig {
parsed_command: ParsedCommand {
prog: prog.to_string(),
args,
cmd: cmd.map(|s| s.to_string()),
},
parsed_command: CommandType::FullCommand(
FullCommand {
prog: prog.to_string(),
args,
cmd: cmd.map(|s| s.to_string()),
}
),
pre_msg: pre_msg.map(|s| s.to_string()),
post_msg: post_msg.map(|s| s.to_string()),
pre_cmds,
Expand Down

0 comments on commit 8c990aa

Please sign in to comment.