Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
empty vars inject nothing and not specified vars inject all
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Jul 29, 2020
1 parent 25c8003 commit 1b64d8a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 32 deletions.
13 changes: 8 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It is like to run `eval $(.env_file) ./script.sh` with more options.
- Allow multiple [**setups**](#setup) in the same [**project**](#project-shortyaml).
- Allow to store example of **no critical** environment file in your source code with a [**public env directory**](#public-directory).
- Allow to store prod/stage/etc.. and **critical** environment file in your source code with a [**private env directory 🔒**](#private-directory-).
- You can apply a mapping in order to [**select**, **group** and add **custom formats / cases**](#arrayvar) on the fly on the [environment variables](#variables).
- You can apply a mapping in order to [**select**, **group** and add **custom formats / cases**](#arrayvars) on the fly on the [environment variables](#variables).
- The result of mapping will be **inject** as [environment variables](#variables) in the output .sh script that will be executed.

![short global workflow](./docs/img/short_global_workflow.png)
Expand Down Expand Up @@ -385,7 +385,7 @@ setups:
case: CamelCase # ArrayVar.case : Enum<Case> - Optional
format: "[{key}]='{value}'" # ArrayVar.format : String - Optional
delimiter: " " # ArrayVar.delimiter : String - Optional
vars: []
vars: [] # Vars
```

Expand All @@ -397,7 +397,7 @@ Setup name

Path towards the project subdirectory.

#### ArrayVar
#### ArrayVars

This configuration allow to group and apply custom format and mapping in one environment variables.

Expand Down Expand Up @@ -459,7 +459,10 @@ Variables as set here, are selected for injection.

👉 Must **match** with environment variables.

👉 If it's no specified or empty, **all variables are selected**.
👉 If it's no specified, **all variables are selected**.

👉 If it's empty like `vars: []`, **any variables are selected**.


# Help
```
Expand Down Expand Up @@ -576,7 +579,7 @@ So value can be changed but, **variables,variables order,spaces, comments** will
### Variables

Variables can be come from the [environment file](#environment-file-environment_name)
or specified in the [configuration file](#configuration-file-shortyaml) : [array_vars](#arrayvar) and [vars](#varsvar).
or specified in the [configuration file](#configuration-file-shortyaml) : [array_vars](#arrayvars) and [vars](#varsvar).

👉 Injected to script as an **environment variables**, variables name will be converted to **UPPER_CASE**. (See also [`vars`](#vars-displaycompare-mapping-environment-variables) command).

Expand Down
12 changes: 2 additions & 10 deletions src/cfg/local/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use crate::cfg::local::setup_vars::Vars;
use crate::cfg::local::ArrayVars;
use crate::cfg::setup::SetupCfg;
use crate::cfg::{CfgError};
use crate::cfg::CfgError;

pub type SetupName = String;

Expand Down Expand Up @@ -65,7 +65,6 @@ impl LocalSetupCfg {
};

local_setup.new_array_vars();
local_setup.new_vars();

local_setup
}
Expand All @@ -78,12 +77,6 @@ impl LocalSetupCfg {
self.file = file;
}

pub fn new_vars(&mut self) -> Rc<RefCell<Vars>> {
let vars = Rc::new(RefCell::new(Vars::new()));
self.vars = Some(Rc::clone(&vars));
vars
}

pub fn new_array_vars(&mut self) -> Rc<RefCell<ArrayVars>> {
let array_vars = ArrayVars::new();
let array_vars = Rc::new(RefCell::new(array_vars));
Expand Down Expand Up @@ -143,8 +136,7 @@ file: run.sh
array_vars:
all: ".*"
var2: "*_SUFFIX"
var1: PREFIX_*
vars: []"#;
var1: PREFIX_*"#;

let array_vars = setup_cfg.array_vars().unwrap();
let mut array_vars = array_vars.borrow_mut();
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ pub fn run(app: &ArgMatches) -> Result<()> {
let local_setup = setup.local_setup().unwrap();
let local_setup = local_setup.borrow();
let array_vars = local_setup.array_vars().unwrap_or_default();
let vars = local_setup.vars().unwrap_or_default();
let vars = local_setup.vars();
drop(local_setup);

let mut env_vars = generate_env_vars(&env, array_vars.borrow(), vars.borrow())?;
let mut env_vars = generate_env_vars(&env, array_vars.borrow(), vars)?;

env_vars.push(
EnvVar::from_env(&env).context(format!("fail to generate var from env `{:?}`", env))?,
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub fn vars(app: &ArgMatches) -> Result<()> {
let local_setup = setup.local_setup().unwrap();
let local_setup = local_setup.borrow();
let array_vars = local_setup.array_vars().unwrap_or_default();
let vars = local_setup.vars().unwrap_or_default();
let vars = local_setup.vars();
drop(local_setup);
let mut env_vars = generate_env_vars(&env_ref, array_vars.borrow(), vars.borrow())?;
let mut env_vars = generate_env_vars(&env_ref, array_vars.borrow(), vars)?;
env_vars.push(
EnvVar::from_setup(&setup)
.context(format!("fail to generate var from setup `{:?}`", setup))?,
Expand Down
23 changes: 14 additions & 9 deletions src/run_file/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::cfg::{ArrayVar, ArrayVars, Setup, VarCase, VarName, Vars};
use crate::env_file;
use crate::env_file::Env;
use heck::*;
use std::cell::RefCell;
use std::rc::Rc;

const DEFAULT_DELIMITER: &'static str = ",";
const DEFAULT_FORMAT: &'static str = "{key}:{value}";
Expand Down Expand Up @@ -127,10 +129,13 @@ pub fn generate_env_var(env: &Env, var: &VarName) -> EnvVar {
)
}

pub fn generate_env_vars<AV, V>(env: &Env, array_vars: AV, vars: V) -> Result<Vec<EnvVar>>
pub fn generate_env_vars<AV>(
env: &Env,
array_vars: AV,
vars: Option<Rc<RefCell<Vars>>>,
) -> Result<Vec<EnvVar>>
where
AV: Deref<Target = ArrayVars>,
V: Deref<Target = Vars>,
{
let mut env_vars: Vec<EnvVar> = vec![];

Expand All @@ -139,19 +144,19 @@ where
env_vars.append(&mut vec![env_var]);
}

let vars = vars.as_ref();
if vars.is_empty() {
if let Some(vars) = vars {
let vars = vars.borrow();
for var in vars.as_ref().iter() {
let env_var = generate_env_var(env, var);
env_vars.append(&mut vec![env_var]);
}
} else {
for env_var in env.iter() {
env_vars.push(EnvVar::from((
VarName::from(env_var.name().to_owned()),
EnvValue::Var(env_var.clone()),
)));
}
} else {
for var in vars.iter() {
let env_var = generate_env_var(env, var);
env_vars.append(&mut vec![env_var]);
}
}
Ok(env_vars)
}
Expand Down
6 changes: 2 additions & 4 deletions tests/generate_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ setups:
pattern: ".*"
case: CamelCase
format: "[{key}]='{value}'"
delimiter: " "
vars: []"#,
delimiter: " ""#,
&r
);
}
Expand Down Expand Up @@ -341,8 +340,7 @@ setups:
setup_1:
file: run.sh
array_vars:
all: ".*"
vars: []"#,
all: ".*""#,
&r
);
}
45 changes: 45 additions & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,48 @@ echo "SETUP VAR $SHORT_SETUP"
assert!(contains("ARG2 = TEST_ARG2").count(1).eval(&r));
assert!(contains("SETUP VAR setup_1").count(1).eval(&r));
}

#[test]
fn cmd_run_with_empty_vars() {
let mut e = init("cmd_run");

e.add_file(PROJECT_ENV_EXAMPLE_1_FILE, r#"VAR1=VALUE1"#);
e.add_file(
PROJECT_CFG_FILE,
r#"
setups:
setup_1:
file: run.sh
array_vars:
ALL:
pattern: ".*"
format: "[{key}]='{value}'"
delimiter: " "
vars: []"#,
);
e.add_file(
PROJECT_RUN_FILE,
r#"#!/bin/bash
echo "TEST VAR1=$VAR1"
declare -p ALL
echo "ARG1 = $1"
echo "ARG2 = $2"
echo "ENVIRONMENT VAR $SHORT_ENV"
echo "SETUP VAR $SHORT_SETUP"
"#,
);
e.setup();
e.set_exec_permission(PROJECT_RUN_FILE).unwrap();

let mut command = e.command(BIN_NAME).unwrap();
let command = command
.env("RUST_LOG", "debug")
.arg("run")
.args(&["TEST_ARG1", "TEST_ARG2"])
.args(&vec!["-s", "setup_1"])
.args(&vec!["-e", "example1"]);
let r = command.assert().success().to_string();
assert!(!contains("TEST VAR1=VALUE1").count(1).eval(&r));
assert!(contains("SETUP VAR setup_1").count(1).eval(&r));
assert!(contains("ENVIRONMENT VAR example1").count(1).eval(&r));
}

0 comments on commit 1b64d8a

Please sign in to comment.