fortify / fcli

fcli is a command-line utility for interacting with various Fortify products
https://fortify.github.io/fcli/
Other
31 stars 18 forks source link

Core: Add support for powershell command and arugment auto-completion #450

Open MikeTheSnowman opened 11 months ago

MikeTheSnowman commented 11 months ago

We're able to generate auto-completion scripts for Linux and Mac for the Bash shell. While this may also work for Windows if something like Git Bash or Bash for Windows is available, it's probably not ideal for windows systems.

Ideally, I'd like to see support for powershell to be added as well.

As an example reference, here's a command auto-completion script for powreshell for just a few of fcli's commands:

# Useful links and resources:
#   https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_argument_completion?view=powershell-7.3
#   https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/tab-completion?view=powershell-7.3
#   https://doitpsway.com/how-to-add-parameter-tab-completion-to-your-powershell-functions

# The code that will perform the auto-completion
$scriptBlock = {
    # The parameters passed into the script block by the
    # Register-ArgumentCompleter command
    param($wordToComplete, $commandName, $cursorPosition)

    # The list of commands and subcommands that the typed text is compared to
    $subcommands = 'ssc',
    'ssc session',
    'ssc session login',
    'ssc session logout',
    'ssc session list',
    'ssc appversion', # commands from here down won't really work with tab-completion
    'ssc app',
    'sc-sast',
    'sc-dast', 
    'fod'

    # The line below has been helpful to see what Register-ArgumentCompleter has provided
    #Write-Host $values, $commandName, $wordToComplete, $cursorPosition

    foreach ($cmd in $subcommands) {
        # What has been typed matches the value from the list
        if ($cmd -like "$wordToComplete*") {
            # Returns the command that is most like what the user has currently provided
            $cmd
        }
    }
}

# Register our function and auto-completion code with the command
Register-ArgumentCompleter -CommandName fcli -ScriptBlock $scriptBlock