cd.py 1.6 KB

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