Keeper-Security / Commander

Keeper Commander is a python-based CLI and SDK interface to the Keeper Security platform. Provides administrative controls, reporting, import/export and vault management.
https://www.keepersecurity.com/commander.html
MIT License
182 stars 74 forks source link

Unable to pipe output of commander on Windows #1193

Closed KevinGlassDDL closed 1 month ago

KevinGlassDDL commented 4 months ago

Hello!

I'm in the process of trying to setup a python automation that call keeper-commander.exe and attempts to save the output to a file. However, I'm running into and annoying unicode handling limitation with Windows. This is on the most recent Keeper Commander, tested on Windows 11 and Server 2016.

As an example, if I call keeper tree i get this in the console, which is expected:

My Vault
 ├── Folder 1 [SHARED]
 ├── Folder 2 [SHARED]
 ......

If I call keeper tree >text.txt we get this error message. An unexpected error occurred: 'charmap' codec can't encode characters in position 11-13: character maps to <undefined>.

If we copy and paste the output to a text file and open it in a hex editor we can see it's perfectly valid UTF-8 box drawing characters but not correct Ansi. image

I believe this commit that added color support may be the cause but it could have been an earlier change. https://github.com/Keeper-Security/Commander/blame/7c60fc32b0578dde83c36cd4419de240330b0b99/keepercommander/commands/folder.py#L24

In older versions, release 16.8.10 is what I'm using, the tree command output like this:

My Vault
 +-- Folder 1 [Shared]
 +-- Folder 2 [Shared]

Ideally I would like some kind of setting or flag to tell Commander to use the old behavior, or detect when output is being piped, or called from another script. I had hoped --batch-mode would hide non-ascii characters it doesn't change the tree behavior. It's also possible a change to charmap might be able to resolve the issue but based on my quick look of the code base and limited understanding of Python, it doesn't seem easy.

Thank you!

sk-keeper commented 4 months ago

Setting this environment variable works short term.

set PYTHONIOENCODING=UTF-8

The next commander release will fix this issue.

sk-keeper commented 4 months ago

Commander release 16.10.6 fixed this stdout output redirect issue. The output uses utf-8 characters now.

My Vault
 ├── Folder 1 [Shared]
 └── Folder 2