diff options
author | Erovia <Erovia@users.noreply.github.com> | 2021-11-04 21:21:09 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-05 08:21:09 +1100 |
commit | c8b09d0d4ab73c88c6ae84718f0c890640dfbece (patch) | |
tree | bf7d61aec282c9cedbfd97d4ffc67092f2396966 | |
parent | fefd7fdc5145b677da7b0e1d75bab11eac9db605 (diff) |
CLI: Add 'cd' subcommand (#12584)
* CLI: Add 'cd' subcommand
Go to your qmk_firmware dir with ease.
* Fix for Windows and do not run if already under QMK Home
* Make flake8 happy
* Fix prompt for Windows
* Make flake8 happy once again
* I'll get it right eventually
* Apply suggestions from code review
Co-authored-by: Ryan <fauxpark@gmail.com>
* Add subcommand to __init__.py and fixup after rebase
* Update Windows code to use milc's run
* Unify the subshell starting with os.execl
* Exit with error msg when output is redirected to non-TTY.
* Revert Windows-specific code
Co-authored-by: Ryan <fauxpark@gmail.com>
-rw-r--r-- | docs/cli_commands.md | 14 | ||||
-rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
-rwxr-xr-x | lib/python/qmk/cli/cd.py | 46 |
3 files changed, 61 insertions, 0 deletions
diff --git a/docs/cli_commands.md b/docs/cli_commands.md index e498f3b22e..8f5117633f 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -118,6 +118,20 @@ This command lets you configure the behavior of QMK. For the full `qmk config` d qmk config [-ro] [config_token1] [config_token2] [...] [config_tokenN] ``` +## `qmk cd` + +This command opens a new shell in your `qmk_firmware` directory. + +Note that if you are already somewhere within `QMK_HOME` (for example, the `keyboards/` folder), nothing will happen. + +To exit out into the parent shell, simply type `exit`. + +**Usage**: + +``` +qmk cd +``` + ## `qmk console` This command lets you connect to keyboard consoles to get debugging messages. It only works if your keyboard firmware has been compiled with `CONSOLE_ENABLE=yes`. diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 539d03e2fc..ea961315bd 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -31,6 +31,7 @@ safe_commands = [ subcommands = [ 'qmk.cli.bux', 'qmk.cli.c2json', + 'qmk.cli.cd', 'qmk.cli.cformat', 'qmk.cli.chibios.confmigrate', 'qmk.cli.clean', diff --git a/lib/python/qmk/cli/cd.py b/lib/python/qmk/cli/cd.py new file mode 100755 index 0000000000..c62c3f56c6 --- /dev/null +++ b/lib/python/qmk/cli/cd.py @@ -0,0 +1,46 @@ +"""Open a shell in the QMK Home directory +""" +import sys +import os + +from milc import cli + +from qmk.path import under_qmk_firmware + + +@cli.subcommand('Go to QMK Home') +def cd(cli): + """Go to QMK Home + """ + if not sys.stdout.isatty(): + cli.log.error("This command is for interactive usage only. For non-interactive usage, 'cd $(qmk env QMK_HOME)' is more robust.") + sys.exit(1) + + if not under_qmk_firmware(): + # Only do anything if the user is not under qmk_firmware already + # in order to reduce the possibility of starting multiple shells + cli.log.info("Spawning a subshell in your QMK_HOME directory.") + cli.log.info("Type 'exit' to get back to the parent shell.") + if not cli.platform.lower().startswith('windows'): + # For Linux/Mac/etc + # Check the user's login shell from 'passwd' + # alternatively fall back to $SHELL env var + # and finally to '/bin/bash'. + import getpass + import pwd + shell = pwd.getpwnam(getpass.getuser()).pw_shell + if not shell: + shell = os.environ.get('SHELL', '/bin/bash') + # Start the new subshell + os.execl(shell, shell) + else: + # For Windows + # Check the $SHELL env var + # and fall back to '/usr/bin/bash'. + qmk_env = os.environ.copy() + # Set the prompt for the new shell + qmk_env['MSYS2_PS1'] = qmk_env['PS1'] + # Start the new subshell + cli.run([os.environ.get('SHELL', '/usr/bin/bash')], env=qmk_env) + else: + cli.log.info("Already within qmk_firmware directory.") |