From d569f0877155efc752994f8a21f5cf001f9d6ae6 Mon Sep 17 00:00:00 2001
From: skullydazed <skullydazed@users.noreply.github.com>
Date: Sun, 22 Sep 2019 13:25:33 -0700
Subject: Configuration system for CLI (#6708)

* Rework how bin/qmk handles subcommands

* qmk config wip

* Code to show all configs

* Fully working `qmk config` command

* Mark some CLI arguments so they don't pollute the config file

* Fleshed out config support, nicer subcommand support

* sync with installable cli

* pyformat

* Add a test for subcommand_modules

* Documentation for the `qmk config` command

* split config_token on space so qmk config is more predictable

* Rework how subcommands are imported

* Document `arg_only`

* Document deleting from CLI

* Document how multiple operations work

* Add cli config to the doc index

* Add tests for the cli commands

* Make running the tests more reliable

* Be more selective about building all default keymaps

* Update new-keymap to fit the new subcommand style

* Add documentation about writing CLI scripts

* Document new-keyboard

* Update docs/cli_configuration.md

Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com>

* Update docs/cli_development.md

Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com>

* Update docs/cli_development.md

Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com>

* Update docs/cli_development.md

Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com>

* Address yan's comments.

* Apply suggestions from code review

suggestions from @noahfrederick

Co-Authored-By: Noah Frederick <code@noahfrederick.com>

* Apply suggestions from code review

Co-Authored-By: Noah Frederick <code@noahfrederick.com>

* Remove pip3 from the test runner
---
 bin/qmk              | 92 ++++++++++++++++++++--------------------------------
 bin/qmk-compile-json |  1 -
 bin/qmk-doctor       |  1 -
 bin/qmk-hello        |  1 -
 bin/qmk-json-keymap  |  1 -
 5 files changed, 36 insertions(+), 60 deletions(-)
 delete mode 120000 bin/qmk-compile-json
 delete mode 120000 bin/qmk-doctor
 delete mode 120000 bin/qmk-hello
 delete mode 120000 bin/qmk-json-keymap

(limited to 'bin')

diff --git a/bin/qmk b/bin/qmk
index 1aa16e17dc..876473fa43 100755
--- a/bin/qmk
+++ b/bin/qmk
@@ -4,10 +4,8 @@
 import os
 import subprocess
 import sys
-from glob import glob
-from time import strftime
-from importlib import import_module
 from importlib.util import find_spec
+from time import strftime
 
 # Add the QMK python libs to our path
 script_dir = os.path.dirname(os.path.realpath(__file__))
@@ -15,12 +13,8 @@ qmk_dir = os.path.abspath(os.path.join(script_dir, '..'))
 python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python'))
 sys.path.append(python_lib_dir)
 
-# Change to the root of our checkout
-os.environ['ORIG_CWD'] = os.getcwd()
-os.chdir(qmk_dir)
-
 # Make sure our modules have been setup
-with open('requirements.txt', 'r') as fd:
+with open(os.path.join(qmk_dir, 'requirements.txt'), 'r') as fd:
     for line in fd.readlines():
         line = line.strip().replace('<', '=').replace('>', '=')
 
@@ -32,72 +26,58 @@ with open('requirements.txt', 'r') as fd:
 
         module = line.split('=')[0] if '=' in line else line
         if not find_spec(module):
-            print('Your QMK build environment is not fully setup!\n')
-            print('Please run `./util/qmk_install.sh` to setup QMK.')
+            print('Could not find module %s!', module)
+            print('Please run `pip3 install -r requirements.txt` to install the python dependencies.')
             exit(255)
 
 # Figure out our version
+# TODO(skullydazed/anyone): Find a method that doesn't involve git. This is slow in docker and on windows.
 command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
-result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 
 if result.returncode == 0:
-    os.environ['QMK_VERSION'] = 'QMK ' + result.stdout.strip()
+    os.environ['QMK_VERSION'] = result.stdout.strip()
 else:
-    os.environ['QMK_VERSION'] = 'QMK ' + strftime('%Y-%m-%d-%H:%M:%S')
+    os.environ['QMK_VERSION'] = 'nogit-' + strftime('%Y-%m-%d-%H:%M:%S') + '-dirty'
 
 # Setup the CLI
 import milc
-milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}'
 
-# If we were invoked as `qmk <cmd>` massage sys.argv into `qmk-<cmd>`.
-# This means we can't accept arguments to the qmk script itself.
-script_name = os.path.basename(sys.argv[0])
-if script_name == 'qmk':
-    if len(sys.argv) == 1:
-        milc.cli.log.error('No subcommand specified!\n')
-
-    if len(sys.argv) == 1 or sys.argv[1] in ['-h', '--help']:
-        milc.cli.echo('usage: qmk <subcommand> [...]')
-        milc.cli.echo('\nsubcommands:')
-        subcommands = glob(os.path.join(qmk_dir, 'bin', 'qmk-*'))
-        for subcommand in sorted(subcommands):
-            subcommand = os.path.basename(subcommand).split('-', 1)[1]
-            milc.cli.echo('\t%s', subcommand)
-        milc.cli.echo('\nqmk <subcommand> --help for more information')
-        exit(1)
+milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}'
 
-    if sys.argv[1] in ['-V', '--version']:
-        milc.cli.echo(os.environ['QMK_VERSION'])
-        exit(0)
 
-    sys.argv[0] = script_name = '-'.join((script_name, sys.argv[1]))
-    del sys.argv[1]
+@milc.cli.entrypoint('QMK Helper Script')
+def qmk_main(cli):
+    """The function that gets run when no subcommand is provided.
+    """
+    cli.print_help()
 
-# Look for which module to import
-if script_name == 'qmk':
-    milc.cli.print_help()
-    exit(0)
-elif not script_name.startswith('qmk-'):
-    milc.cli.log.error('Invalid symlink, must start with "qmk-": %s', script_name)
-else:
-    subcommand = script_name.replace('-', '.').replace('_', '.').split('.')
-    subcommand.insert(1, 'cli')
-    subcommand = '.'.join(subcommand)
 
-    try:
-        import_module(subcommand)
-    except ModuleNotFoundError as e:
-        if e.__class__.__name__ != subcommand:
-            raise
+def main():
+    """Setup our environment and then call the CLI entrypoint.
+    """
+    # Change to the root of our checkout
+    os.environ['ORIG_CWD'] = os.getcwd()
+    os.chdir(qmk_dir)
 
-        milc.cli.log.error('Invalid subcommand! Could not import %s.', subcommand)
-        exit(1)
+    # Import the subcommands
+    import qmk.cli
 
-if __name__ == '__main__':
+    # Execute
     return_code = milc.cli()
+
     if return_code is False:
         exit(1)
-    elif return_code is not True and isinstance(return_code, int) and return_code < 256:
+
+    elif return_code is not True and isinstance(return_code, int):
+        if return_code < 0 or return_code > 255:
+            milc.cli.log.error('Invalid return_code: %d', return_code)
+            exit(255)
+
         exit(return_code)
-    else:
-        exit(0)
+
+    exit(0)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/bin/qmk-compile-json b/bin/qmk-compile-json
deleted file mode 120000
index c92dce8a10..0000000000
--- a/bin/qmk-compile-json
+++ /dev/null
@@ -1 +0,0 @@
-qmk
\ No newline at end of file
diff --git a/bin/qmk-doctor b/bin/qmk-doctor
deleted file mode 120000
index c92dce8a10..0000000000
--- a/bin/qmk-doctor
+++ /dev/null
@@ -1 +0,0 @@
-qmk
\ No newline at end of file
diff --git a/bin/qmk-hello b/bin/qmk-hello
deleted file mode 120000
index c92dce8a10..0000000000
--- a/bin/qmk-hello
+++ /dev/null
@@ -1 +0,0 @@
-qmk
\ No newline at end of file
diff --git a/bin/qmk-json-keymap b/bin/qmk-json-keymap
deleted file mode 120000
index c92dce8a10..0000000000
--- a/bin/qmk-json-keymap
+++ /dev/null
@@ -1 +0,0 @@
-qmk
\ No newline at end of file
-- 
cgit v1.2.3