Asadmin Shell Completion

I’ve been juggling glassfish servers a lot lately, and found the asadmin CLI admin tool pretty nice. There was one thing lacking though: ZSH tab completion. So, being a hacker, I thought to myself: “How hard can it be?”

Pretty hard, as it turns out. The ZSH completion system is ridiculously complex and poorly documented. There is a pretty good ebook, but it is from 2003 and I’m sure a lot of things have changed since then. Other than that, then only tutorial on writing your own completion functions was an old Linux Mag article.

After a lot of trial and error, borrowing code from various oh-my-zsh plugins, and nearly giving up in frustration, I’ve now produced a plugin of my own. It’s pretty large (1147 lines), but has completions for all the 265 subcommands that I know of. I quickly realized that maintaining the argument completions for all these subcommands would be quite impractical, so I did what any hacker would do: I wrote quick-and-dirty program that goes through all the glassfish jar files on the classpath, looking for classes that implement the asadmin commands. It then looks at annotations and uses a bit of introspection to determine what arguments are available for each command, and what completions (if any) should be used to complete these. The end result is a ZSH script fragment which I then insert manually into my completion script. Overall, this has worked out pretty well. And it sure beats doing it by hand.

Anyway, if you work with asadmin and would like some hot completion action going, check out my oh-my-zsh branch on github.