-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add --completions argument to generate Nushell completion script #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0df0e7d
fix: last minute rename of the show-product to list-product command was
lfrancke 488e470
feat: Add --completions argument to generate Nushell completion script
lfrancke 6d4d07c
Move completion to module
lfrancke aa04986
Update src/image_tools/completions.py
lfrancke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from .args import build_bake_argparser | ||
|
||
|
||
def nushell_completion(completions): | ||
# List of options to exclude | ||
excluded_options = {"--product"} | ||
|
||
# Collect completion lines | ||
# One line for each argument we support in bake | ||
completion_lines = [] | ||
for completion in completions: | ||
if completion["long"] not in excluded_options: | ||
# Build the baseline | ||
if completion["short"]: | ||
line = f" {completion['long']} ({completion['short']})" | ||
else: | ||
line = f" {completion['long']}" | ||
|
||
# Add ": string" if it takes a parameter | ||
if completion["takes_param"]: | ||
line += ": string" | ||
|
||
# Add help text as a comment if available | ||
if completion["help"]: | ||
line += f" # {completion['help']}" | ||
|
||
# Append the line to the list | ||
completion_lines.append(line) | ||
completion_lines = "\n".join(completion_lines) | ||
|
||
script_template = f""" | ||
module bake-completions {{ | ||
def list_products_completions [] {{ | ||
let output = (bake --list-products | from json) | ||
let products = $output | columns | ||
return $products | ||
}} | ||
|
||
def list_versions_completions [product] {{ | ||
let output = (bake --list-products | from json) | ||
let versions = $output | get $product | each {{|version| $"($product)=($version)"}} | ||
return $versions | ||
}} | ||
|
||
export extern "bake" [ | ||
{completion_lines} | ||
--product (-p): string@"nu-complete product" # Product to build images for. For example 'druid' or 'druid=28.0.1' to build a specific version. | ||
] | ||
|
||
def "nu-complete product" [context: string] {{ | ||
# This function will not currently work properly should one full product be a prefix of another product name | ||
|
||
# context will be something like "bake --product <something>" | ||
# So, after splitting it up we'll have | ||
# - Row 0: "bake" | ||
# - Row 1: "--product" | ||
# - Row 2: "<something>" (e.g. empty or "hb" or "hbase" or "hbase=2.6.0" or similar | ||
let parts = ($context | split row ' ') | ||
let product_specification = $parts | get 2 | ||
let product_parts = $product_specification | split row '=' | ||
let product = $product_parts | get 0 | ||
|
||
let all_products = list_products_completions | ||
|
||
# Check if the product that was specified is already "complete" (can be found in the list of all products) | ||
if ($all_products | any {{ |item| $item == $product }}) {{ | ||
list_versions_completions $product | ||
}} else {{ | ||
return $all_products | ||
}} | ||
}} | ||
}} | ||
|
||
export use bake-completions * | ||
|
||
""" | ||
print(script_template) | ||
|
||
|
||
def print_completion(shell: str): | ||
completions = [] | ||
parser = build_bake_argparser() | ||
for action in parser._actions: | ||
# Separate long and short options | ||
long_option = None | ||
short_option = None | ||
|
||
for option in action.option_strings: | ||
if option.startswith("--"): | ||
long_option = option | ||
elif option.startswith("-"): | ||
short_option = option | ||
|
||
# Determine if the argument takes a parameter | ||
if action.nargs == 0: | ||
takes_param = False | ||
else: | ||
takes_param = action.nargs is not None or action.type is not None or action.default is not None | ||
|
||
help = None | ||
if action.help: | ||
help = action.help.replace("\n", " ").replace("\r", "").strip() | ||
|
||
completions.append({"long": long_option, "short": short_option, "takes_param": takes_param, "help": help}) | ||
|
||
if shell == "nushell": | ||
nushell_completion(completions) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.