annotations: add an option to enable config autocompletion in bash
authorAndrea Righi <andrea.righi@canonical.com>
Mon, 5 Dec 2022 16:48:39 +0000 (17:48 +0100)
committerAndrea Righi <andrea.righi@canonical.com>
Mon, 30 Jan 2023 08:12:28 +0000 (09:12 +0100)
Type `source <(annotations --autocomplete)` to enable auto-completion of
config options (without the CONFIG_ prefix) in bash when running
annotations script commands.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
annotations

index 6b1a0a3ba79c065ba61bc1ba1e019713469e2d76..c74dbe745043d6a4a71731a7ed6ae6fa9b8ad0c1 100755 (executable)
@@ -37,6 +37,8 @@ def make_parser():
                         help='Query annotations')
     parser.add_argument('--note', '-n', action='store',
                         help='Write a specific note to a config option in annotations')
+    parser.add_argument('--autocomplete', action='store_true',
+                        help='Enable config bash autocomplete: `source <(annotations --autocomplete)`')
 
     ga = parser.add_argument_group(title='Action').add_mutually_exclusive_group(required=False)
     ga.add_argument('--write', '-w', action='store',
@@ -77,6 +79,11 @@ def do_query(args):
             break
     print_result(config, res)
 
+def do_autocomplete(args):
+    a = Annotation(args.file)
+    res = (c.removeprefix('CONFIG_') for c in a.search_config())
+    print('complete -W "{}" annotations'.format(' '.join(res)))
+
 def do_note(args):
     if args.config is None:
         arg_fail('error: --note requires --config')
@@ -226,6 +233,8 @@ def main():
         do_update(args)
     elif args.check_file:
         do_check(args)
+    elif args.autocomplete:
+        do_autocomplete(args)
     else:
         do_query(args)