aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk
diff options
context:
space:
mode:
authorErovia <Erovia@users.noreply.github.com>2021-11-04 21:21:09 +0000
committerGitHub <noreply@github.com>2021-11-05 08:21:09 +1100
commitc8b09d0d4ab73c88c6ae84718f0c890640dfbece (patch)
treebf7d61aec282c9cedbfd97d4ffc67092f2396966 /lib/python/qmk
parentfefd7fdc5145b677da7b0e1d75bab11eac9db605 (diff)
downloadqmk_firmware-c8b09d0d4ab73c88c6ae84718f0c890640dfbece.tar.gz
qmk_firmware-c8b09d0d4ab73c88c6ae84718f0c890640dfbece.zip
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>
Diffstat (limited to 'lib/python/qmk')
-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 539d03e2f..ea961315b 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -31,6 +31,7 @@ safe_commands = [
31subcommands = [ 31subcommands = [
32 'qmk.cli.bux', 32 'qmk.cli.bux',
33 'qmk.cli.c2json', 33 'qmk.cli.c2json',
34 'qmk.cli.cd',
34 'qmk.cli.cformat', 35 'qmk.cli.cformat',
35 'qmk.cli.chibios.confmigrate', 36 'qmk.cli.chibios.confmigrate',
36 'qmk.cli.clean', 37 'qmk.cli.clean',
diff --git a/lib/python/qmk/cli/cd.py b/lib/python/qmk/cli/cd.py
new file mode 100755
index 000000000..c62c3f56c
--- /dev/null
+++ b/lib/python/qmk/cli/cd.py
@@ -0,0 +1,46 @@
1"""Open a shell in the QMK Home directory
2"""
3import sys
4import os
5
6from milc import cli
7
8from qmk.path import under_qmk_firmware
9
10
11@cli.subcommand('Go to QMK Home')
12def cd(cli):
13 """Go to QMK Home
14 """
15 if not sys.stdout.isatty():
16 cli.log.error("This command is for interactive usage only. For non-interactive usage, 'cd $(qmk env QMK_HOME)' is more robust.")
17 sys.exit(1)
18
19 if not under_qmk_firmware():
20 # Only do anything if the user is not under qmk_firmware already
21 # in order to reduce the possibility of starting multiple shells
22 cli.log.info("Spawning a subshell in your QMK_HOME directory.")
23 cli.log.info("Type 'exit' to get back to the parent shell.")
24 if not cli.platform.lower().startswith('windows'):
25 # For Linux/Mac/etc
26 # Check the user's login shell from 'passwd'
27 # alternatively fall back to $SHELL env var
28 # and finally to '/bin/bash'.
29 import getpass
30 import pwd
31 shell = pwd.getpwnam(getpass.getuser()).pw_shell
32 if not shell:
33 shell = os.environ.get('SHELL', '/bin/bash')
34 # Start the new subshell
35 os.execl(shell, shell)
36 else:
37 # For Windows
38 # Check the $SHELL env var
39 # and fall back to '/usr/bin/bash'.
40 qmk_env = os.environ.copy()
41 # Set the prompt for the new shell
42 qmk_env['MSYS2_PS1'] = qmk_env['PS1']
43 # Start the new subshell
44 cli.run([os.environ.get('SHELL', '/usr/bin/bash')], env=qmk_env)
45 else:
46 cli.log.info("Already within qmk_firmware directory.")