Improve handling of variables. Can now define them in parent of script, script directory or the directory the simdem command was run from

This commit is contained in:
Ross Gardler 2017-06-23 06:47:19 +00:00
Родитель 8299a26baa
Коммит 54e7f32d26
11 изменённых файлов: 184 добавлений и 65 удалений

3
.gitignore поставляемый
Просмотреть файл

@ -1,3 +1,6 @@
*~
\#*
.#*
env.local.json
!demo_scripts/test/env.local.json
!/env.local.json

Просмотреть файл

@ -1,3 +1,3 @@
{
"TEST": "Hello from the project"
"PARENT_TEST": "Hello from the parent"
}

Просмотреть файл

@ -1,3 +0,0 @@
{
"LOCAL_TEST": "Hello from the local project config"
}

Просмотреть файл

@ -0,0 +1,3 @@
{
"NAME": "Hello from the variables subdirectory"
}

Просмотреть файл

@ -4,25 +4,14 @@ In order to use environment variables, you can define one or more
files. These variables are available in every command that is
executed.
Tutorials can carry `env.json` files in the project directory and/or
in tutorial sub- directories. Files in tutorial sub-directories will
overwrite settings pulled from the project directory.
Tutorials can carry `env.json` files in the directory the simdem
command was run and/or in tutorial sub-directories. Files in tutorial
sub-directories will overwrite settings pulled from the project
directory.
This tutorial defines an 'env.json' in the project directory:
```
cat $SIMDEM_CWD/../env.json
```
Results:
```
{
"TEST": "Hello from the project"
}
```
It also defines an 'env.json' file in the tutorial folder:
For example, this tutorial defines an 'env.json' in the `simdem`
parent folder and in the `variables` subdirectory that contains this
script. Here is the content from the test subdirectory.
```
cat $SIMDEM_CWD/env.json
@ -32,22 +21,41 @@ Results:
```
{
"TEST": "hello-world"
"NAME": "Hello from the variables subdirectory"
}
```
The file that "wins" is the most local one, that is the one in the tutorial:
It also defines an 'env.json' file in the `SimDem` root
folder. Assuming you executed the `simdem` command from within that
folder the followin command will display it's content.
```
echo $TEST
cat env.json
```
Results:
```
hello-world
{
"TEST": "Hello from the SimDem project"
}
```
Finally, a project may define an `env.local.json` file in the
directory from which the `simdem` command is run. This file is the
last to be loaded and overrides all other values.
Values are loaded in the following order, the last file to define a
vlaue is the one that "wins".
- PARENT_OF_SCRIPT_DIR/env.json
- SCRIPT_DIR/env.json
- PARENT_OF_SCRIPT_DIR/env.local.json
- SCRIPT_DIR/env.local.json
- CWD/env.json
- CWD/env.local.json
## Interactive Variables
If you include an environment variable that isn't set, SimDem will prompt

Просмотреть файл

@ -1,3 +1,3 @@
{
"TEST": "hello-world"
"TEST": "Hello from the test script"
}

Просмотреть файл

@ -0,0 +1,3 @@
{
"TEST": "A local hello from the current working directory (where the simdem command was executed)"
}

Просмотреть файл

@ -21,10 +21,114 @@ echo $SIMDEM_CWD
Results:
``` Expected similarity: 0.9
``` Expected_Similarity=0.9
demo_scripts/test
```
# Configuraiton Check
We should be able to retrieve environment variables from the directory
in which the command was given:
```
cat env.json
```
Results:
```
{
"TEST": "Hello from the current working directory (where the simdem command was executed)"
}
```
We should also be able to retrieve locallay defined environment
variables from the directory in which the command was given:
```
cat env.local.json
```
Results:
```
{
"TEST": "A local hello from the current working directory (where the simdem command was executed)"
}
```
There should also be environment variables in the the directory in
which the current script resides.
```
cat $SIMDEM_CWD/env.json
```
Results:
```
{
"TEST": "Hello from the test script"
}
```
Local variables can also be found in the the directory in which the
current script resides.
```
cat $SIMDEM_CWD/env.local.json
```
Results:
```
{
"TEST": "A local hello from the current working directory (where the simdem command was executed)"
}
```
For the `TEST` variable we should have the `env.local.json` value from
the directory in which the application was executed.
```
echo $TEST
```
Results:
```
A local hello from the current working directory (where the simdem command was executed)
```
And finally there should be variable definitions in the parent of the
current script directory:
```
cat $SIMDEM_CWD/../env.json
```
Results:
```
{
"PARENT_TEST": "Hello from the parent"
}
```
Since the value of `PARENT_TEST` is only defined in this file we
should have the value from there:
```
echo $PARENT_TEST
```
Results:
```
Hello from the parent
```
# Simple Echo
```
@ -37,20 +141,6 @@ Results:
Hello world
```
# Cat a file
```
cat $SIMDEM_CWD/env.json
```
Results:
```
{
"TEST": "hello-world"
}
```
# Code comments
```
@ -79,23 +169,6 @@ Results:
Tue Jun 6 15:23:53 UTC 2017
```
# Sudo
NOTE: there is no sudo in Docker containers, so we need to strip
`sudo` from commands if running in a Docker container. If we are not
in a container we need to run SimDem as `sudo`. This test should pass
in both scenarios.
```
sudo echo "Sudo Works"
```
Results:
```
Sudo Works
```
# For Loop
```

3
env.json Normal file
Просмотреть файл

@ -0,0 +1,3 @@
{
"TEST": "A local hello from the current working directory (where the simdem command was executed)"
}

4
env.local.json Normal file
Просмотреть файл

@ -0,0 +1,4 @@
{
"TEST": "A local hello from the current working directory (where the simdem command was executed)"
}

Просмотреть файл

@ -33,11 +33,23 @@ class Environment(object):
self.set("SIMDEM_CWD", directory)
def read_simdem_environment(self, directory):
"""
Populates each shell environment with a set of environment vars
loaded via env.json and/or env.local.json files. Variables are loaded
in order first from the parent of script_dir, then script_dir itself.
env.local.json > env.json.
"""Populates each shell environment with a set of environment vars
loaded via env.json and/or env.local.json files. Variables are
loaded in order first from the parent of the current script
directory, then the current scriptdir itself and finally from
the directory in which the `simdem` command was executed (the
CWD).
Values are loaded in the following order, the last file to
define a vlaue is the one that "wins".
- PARENT_OF_SCRIPT_DIR/env.json
- SCRIPT_DIR/env.json
- PARENT_OF_SCRIPT_DIR/env.local.json
- SCRIPT_DIR/env.local.json
- CWD/env.json
- CWD/env.local.json
"""
env = {}
@ -68,6 +80,19 @@ class Environment(object):
local_env = json.load(env_file)
env.update(local_env)
filename = "/env.json"
if os.path.isfile(filename):
with open(filename) as env_file:
local_env = json.load(env_file)
env.update(local_env)
filename = "env.local.json"
if os.path.isfile(filename):
with open(filename) as env_file:
local_env = json.load(env_file)
env.update(local_env)
self.env.update(env)
def set(self, var, value):