doitlive icon indicating copy to clipboard operation
doitlive copied to clipboard

Handle multi-line commands

Open nrvale0 opened this issue 6 years ago • 4 comments

Nice package. Having a problem using it when there are single or double-quotes in either command or the comments (using commentecho: true). For instance, the following line:

vault write -format=yaml pki_root/root/generate/internal \                                                                                                                                                                           
      common_name='somefakeorg.example Root CA' \                                                                                                                                                                                                                                                                                                                                                                                      
      ttl=8760h                                                                                                                                                                                                                                                                                                                                                                               

cause the following backtrace:

Traceback (most recent call last):                                                                                                                                                                                                                                                                                                                                                                                                       
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/bin/doitlive", line 11, in <module>                                                                                                                                                                                                                                                                                                                                             
    sys.exit(cli())                                                                                                                                                                                                                                                                                                                                                                                                                      
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/site-packages/click/core.py", line 722, in __call__                                                                                                                                                                                                                                                                                                               
    return self.main(*args, **kwargs)                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/site-packages/click/core.py", line 697, in main                                                                                                                                                                                                                                                                                                                   
    rv = self.invoke(ctx)                                                                                                                                                                                                                                                                                                                                                                                                                
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/site-packages/click/core.py", line 1066, in invoke                                                                                                                                                                                                                                                                                                                
    return _process_result(sub_ctx.command.invoke(sub_ctx))                                                                                                                                                                                                                                                                                                                                                                              
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/site-packages/click/core.py", line 895, in invoke                                                                                                                                                                                                                                                                                                                 
    return ctx.invoke(self.callback, **ctx.params)                                                                                                                                                                                                                                                                                                                                                                                       
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/site-packages/click/core.py", line 535, in invoke                                                                                                                                                                                                                                                                                                                 
    return callback(*args, **kwargs)                                                                                                                                                                                                                                                                                                                                                                                                     
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/site-packages/doitlive/cli.py", line 322, in play                                                                                                                                                                                                                                                                                                                 
    commentecho=commentecho)                                                                                                                                                                                                                                                                                                                                                                                                             
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/site-packages/doitlive/cli.py", line 129, in run                                                                                                                                                                                                                                                                                                                  
    command_as_list = shlex.split(ensure_utf8(command))                                                                                                                                                                                                                                                                                                                                                                                  
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/shlex.py", line 305, in split                                                                                                                                                                                                                                                                                                                                     
    return list(lex)                                                                                                                                                                                                                                                                                                                                                                                                                     
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/shlex.py", line 295, in __next__                                                                                                                                                                                                                                                                                                                                  
    token = self.get_token()                                                                                                                                                                                                                                                                                                                                                                                                             
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/shlex.py", line 105, in get_token                                                                                                                                                                                                                                                                                                                                 
    raw = self.read_token()                                                                                                                                                                                                                                                                                                                                                                                                              
  File "/home/redacted/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/shlex.py", line 206, in read_token                                                                                                                                                                                                                                                                                                                                
    raise ValueError("No escaped character")                                                                                                                                                                                                                                                                                                                                                                                             
ValueError: No escaped character                                                                                                                                                                                                                                                                                                                                                                                                         

At least in the case of comments you can avoid the backtrace by escaping the single or double quotes but then the escaping also shows up in the commentecho output.

Ideas?

nrvale0 avatar May 22 '18 19:05 nrvale0

Thanks for reporting this. I don't have time to look into this right now, but I would certainly review and merge a PR for this.

sloria avatar May 23 '18 02:05 sloria

This is not because of the nested quoting but because of the multiline escaped commands passed to Python shlex.

nrvale0 avatar Jun 05 '18 04:06 nrvale0

Ah, good catch @nrvale0 . I've renamed the issue.

sloria avatar Jun 05 '18 12:06 sloria

The root issue is that the cli.py/run is reading the session one line at a time and then feeds each line to shlex.split(). In the situation where the command is multiline shlex is rightfully complaining that there's nothing following the line escape character. Some adjustments are necessary to the parsing logic in run()...seems like it should not be particularly difficult to add support for multiline.

nrvale0 avatar Jun 05 '18 14:06 nrvale0