Skip to content

Commit

Permalink
Support setting envvars with 'export' commands
Browse files Browse the repository at this point in the history
closes #32
  • Loading branch information
sloria committed May 12, 2018
1 parent a387b72 commit 2de0499
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Features:

* Add shell completion for bash, zsh, and fish (:issue:`3`).
* Add "Did you mean" suggestions.
* Support setting environment variables with ``export`` commands
(:issue:`32`). Thanks :user:`asmacdo` for the suggestion.

Bug fixes:

Expand Down
12 changes: 11 additions & 1 deletion doitlive/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import functools
import os
import re
import shlex
import sys
import textwrap
from codecs import open
Expand All @@ -11,14 +12,14 @@
from click import secho, style
from click_didyoumean import DYMGroup

from doitlive.__version__ import __version__
from doitlive.compat import ensure_utf8
from doitlive.exceptions import SessionError
from doitlive.keyboard import (RETURNS, magicrun, magictype, run_command,
wait_for)
from doitlive.python_consoles import PythonRecorderConsole, start_python_player
from doitlive.styling import THEMES, echo, echo_prompt, format_prompt
from doitlive.termutils import get_default_shell
from doitlive.__version__ import __version__

env = os.environ
click_completion.init()
Expand Down Expand Up @@ -120,6 +121,7 @@ def run(commands, shell=None, prompt_template='default', speed=1,
i = 0
while i < len(commands):
command = commands[i].strip()
command_as_list = shlex.split(command)
i += 1
if not command:
continue
Expand All @@ -135,6 +137,14 @@ def run(commands, shell=None, prompt_template='default', speed=1,
comment = command.lstrip("#")
secho(comment, fg='yellow', bold=True)
continue
# Handle 'export' commands by adding envvars to SessionState
elif command_as_list and command_as_list[0] == 'export':
magictype(command,
prompt_template=state['prompt_template'],
speed=state['speed'])
for envvar in command_as_list[1:]:
state.add_envvar(envvar)
# Handle ```python and ```ipython by running "player" shells
elif shell_match:
shell_name = shell_match.groups()[0].strip()
py_commands = []
Expand Down
2 changes: 2 additions & 0 deletions tests/sessions/export.session
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export NAME=Steve
echo 'Hello' $NAME
9 changes: 9 additions & 0 deletions tests/test_doitlive.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ def test_unset_envvar(self, runner):
result = run_session(runner, 'unset.session', user_input)
assert 'fortytwo' not in result.output

def test_export(self, runner):
user_input = ''.join([
random_string(len('export NAME=Steve')),
'\n',
random_string(len("echo 'Hello' $NAME"))
])
result = run_session(runner, 'export.session', user_input)
assert 'Hello Steve' in result.output


def test_themes_list(runner):
result1 = runner.invoke(cli, ['themes'])
Expand Down

0 comments on commit 2de0499

Please sign in to comment.