Skip to content

Commit

Permalink
feat(vhd-cli info): list method with multiple VHDs
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-f committed Jun 27, 2022
1 parent c275d5d commit ba8c5d7
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/vhd-cli/commands/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

const { Constants, VhdFile } = require('vhd-lib')
const { getHandler } = require('@xen-orchestra/fs')
const { openVhd } = require('vhd-lib/openVhd')
const { resolve } = require('path')
const UUID = require('uuid')
const Disposable = require('promise-toolbox/Disposable')
const humanFormat = require('human-format')
const invert = require('lodash/invert.js')
const UUID = require('uuid')

const { PLATFORMS } = Constants

Expand Down Expand Up @@ -34,8 +36,8 @@ function mapProperties(object, mapping) {
return result
}

module.exports = async function info(args) {
const vhd = new VhdFile(getHandler({ url: 'file:///' }), resolve(args[0]))
async function showDetails(handler, path) {
const vhd = new VhdFile(handler, resolve(path))

try {
await vhd.readHeaderAndFooter()
Expand Down Expand Up @@ -69,3 +71,29 @@ module.exports = async function info(args) {
})
)
}

async function showList(handler, paths) {
let previousUuid
for (const path of paths) {
await Disposable.use(openVhd(handler, resolve(path)), async vhd => {
const uuid = MAPPERS.uuid(vhd.footer.uuid)
const fields = [path, MAPPERS.bytes(vhd.footer.currentSize), uuid, MAPPERS.diskType(vhd.footer.diskType)]
if (vhd.footer.diskType === Constants.DISK_TYPES.DIFFERENCING) {
const parentUuid = MAPPERS.uuid(vhd.header.parentUuid)
fields.push(parentUuid === previousUuid ? '<above VHD>' : parentUuid)
}
previousUuid = uuid
console.log(fields.join(' | '))
})
}
}

module.exports = async function info(args) {
const handler = getHandler({ url: 'file:///' })

if (args.length === 1) {
return showDetails(handler, args[0])
}

return showList(handler, args)
}

0 comments on commit ba8c5d7

Please sign in to comment.