at master 1.7 kB view raw
1"""Open a shell in the QMK Home directory 2""" 3import sys 4import os 5import subprocess 6 7from milc import cli 8 9from qmk.path import under_qmk_firmware 10 11 12@cli.subcommand('Go to QMK Home') 13def cd(cli): 14 """Go to QMK Home 15 """ 16 if not sys.stdout.isatty(): 17 cli.log.error("This command is for interactive usage only. For non-interactive usage, 'cd $(qmk env QMK_HOME)' is more robust.") 18 sys.exit(1) 19 20 if not under_qmk_firmware(): 21 # Only do anything if the user is not under qmk_firmware already 22 # in order to reduce the possibility of starting multiple shells 23 cli.log.info("Spawning a subshell in your QMK_HOME directory.") 24 cli.log.info("Type 'exit' to get back to the parent shell.") 25 if not cli.platform.lower().startswith('windows'): 26 # For Linux/Mac/etc 27 # Check the user's login shell from 'passwd' 28 # alternatively fall back to $SHELL env var 29 # and finally to '/bin/bash'. 30 import getpass 31 import pwd 32 shell = pwd.getpwnam(getpass.getuser()).pw_shell 33 if not shell: 34 shell = os.environ.get('SHELL', '/bin/bash') 35 # Start the new subshell 36 os.execl(shell, shell) 37 else: 38 # For Windows 39 # Check the $SHELL env var 40 # and fall back to '/usr/bin/bash'. 41 qmk_env = os.environ.copy() 42 # Set the prompt for the new shell 43 qmk_env['MSYS2_PS1'] = qmk_env['PS1'] 44 # Start the new subshell 45 subprocess.run([os.environ.get('SHELL', '/usr/bin/bash')], env=qmk_env) 46 else: 47 cli.log.info("Already within qmk_firmware directory.")