cmdlib: Extract instance query related functionality