matt9ucci/DockerCompletion

Integration of a PowerShell Module with DockerCompletion

IsWladi opened this issue · 2 comments

Integration of a PowerShell Module with DockerCompletion

Hello DockerCompletion Maintainers,

I am in the process of developing a PowerShell module designed to colorize Docker command outputs. My goal is to integrate this feature with DockerCompletion to streamline user workflows.

Potential for Synergy

I believe there is potential for synergy between our tools, but I am uncertain if there is an existing method to extend DockerCompletion with an external module's argument completers.

Key Questions

  • Is there a recommended approach to achieve this?
  • Could DockerCompletion implement a function or hook to facilitate such an integration?

Current Attempt

I am currently trying to do this (but it is not ideal for me to modify your source code):
image

Seeking Advice

Your advice on this matter would be invaluable, and I am open to any suggestions you may have. The addition of such extensibility to DockerCompletion could offer significant benefits to users looking for a more customized command-line experience.

Acknowledgments

Thank you for your dedication to developing DockerCompletion, and I hope my contribution can further enrich this tool.

Best regards,
IsWladi

Currently, DockerCompletion does not provide a function or hook to complete an external module's argument. But there is a way to do that as the following script:

# Mock function
function DockerColorPosh {
	for ($i = 0; $i -lt $args.Count; $i++) {
		Write-Host ('$args[{0}] = {1}' -f $i, $args[$i])
	}
}

# Import and get the DockerCompletion's PSModuleInfo
Import-Module DockerCompletion
[System.Management.Automation.PSModuleInfo]$DockerCompletion = Get-Module DockerCompletion

# Get the `$argumentCompleter` from the DockerCompletion's PSModuleInfo.
# The `$argumentCompleter` is the scriptblock for `docker` command.
# It is not an exported variable, but we can access it with the `PSModuleInfo.Invoke()` method like this.
[scriptblock]$completer = $DockerCompletion.Invoke({ $argumentCompleter })

# Register the `$argumentCompleter` for any functions
Register-ArgumentCompleter -CommandName DockerColorPosh -ScriptBlock $completer

Now the completer works. For examples:

  • DockerColorPosh cont[tab] -> DockerColorPosh container
  • DockerColorPosh container ru[tab] -> DockerColorPosh container run
  • DockerColorPosh container run --rea[tab] -> DockerColorPosh container run --read-only

The output of the mock function is:

$ DockerColorPosh container run --read-only
$args[0] = container
$args[1] = run
$args[2] = --read-only

Thank you for the prompt resolution to my issue. Your guidance was clear and very helpful . I appreciate the support and look forward to contributing to this project in the future.