summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2021-11-05 08:55:55 +1100
committerNick Brassel <nick@tzarc.org>2021-11-05 08:55:55 +1100
commit92e9bbd9b9c83553f5c9060cbcdb5c44e9eddb79 (patch)
tree2ccfc6ff2317d94d02e2a3f25db0ec383de32032 /lib/python/qmk/cli
parent84ea77ead663624a6d8ae451ca3d4318008d9d41 (diff)
parentc8da63382c7d2cc0de13559c10b5fde0f436f9e3 (diff)
downloadqmk_firmware-92e9bbd9b9c83553f5c9060cbcdb5c44e9eddb79.tar.gz
qmk_firmware-92e9bbd9b9c83553f5c9060cbcdb5c44e9eddb79.zip
Merge remote-tracking branch 'upstream/master' into develop
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rwxr-xr-xlib/python/qmk/cli/cd.py46
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 094ea80b8d..edf351d628 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.")