Jaa


Posh-AzureCli: Command completion for Azure Cross Platform Command Line in PowerShell

 

tl;dr This post discusses adding tab completion to Azure xplat CLI . If you just want to install it, see the notes below .

UPDATE 17th December 2015 – Install from PowerShell Gallery

Background

If you’re working with Azure then you can perform operations via the web portal, use the PowerShell cmdlets, or use the cross platform command line interface (xplat CLI).

If you’re running OS X or a Linux flavour then clearly the xplat CLI is a good choice Winking smile. But why use it if you’re running on Windows? Personal preference is one reason, but if you’re working across Windows and Linux/OS X then you might want to have a consistent tool across platforms.

Speaking of consistency, if you’re on OS X or Linux then you can enable auto-completion in the CLI to get a productivity boost. Unfortunately that doesn’t work on Windows, which led me to create posh-azurecli. Inspired by posh-git, posh-azurecli makes use of some hooks in PowerShell to enable tab-completion of commands for the CLI when running in PowerShell.

posh-azurecli

Installation

For full installation options, see https://github.com/stuartleeks/posh-azurecli. To install from PowerShell Gallery run

Install-Module -Name posh-azurecli
Install-AzureCliCompletion

How it works

The CLI is written in JavaScript using NodeJS and is open-source. The logic for auto-completion in the CLI is implemented in the autocomplete.js file, and the most relevant logic is in the handleAutoComplete function. This function works with metadata loaded in the initFromCmdMetadata function. To get an idea of what this metadata is, run “azure --gen” to generate the metadata files and open up one of the files. There are two files, one for asm mode and one for arm mode; each file is a json description of the relevant commands and options for that mode.

Essentially, the logic in the handleAutoComplete function breaks down the current command line at the point completion is requested, and then walks down the metadata matching commands/options as it goes.

I considered a couple of general approaches for posh-azurecli: extend the cli to allow a command line to be passed in and have the cli return the options (avoids duplication, but requires changes to the CLI), or replicate the logic in PowerShell (doesn’t require CLI changes, but risks drift in the logic). In the end, for simplicity (this was a side project for me after all!) I opted for re-writing in PowerShell. So, as a colleague put it, I temporarily became the human JavaScript-to-PowerShell transpiler!

As I was transpiling, I was quite surprised at how well the code ported. To illustrate, I opened the original JavaScript (left) and the PowerShell (right) in Visual Studio Code and put them side-by-side as shown in the screenshot below. The general shape stayed pretty similar!

image

Once the core logic was done, it just needed wrapping up as a chocolatey package for easy installation.

So there you have it; feel free to give it a go. If you hit issues then feel free to log them on github. (But please remember that this is an unsupported side project Smile).