Skip to content

Instantly share code, notes, and snippets.

@notslang
Created March 27, 2021 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notslang/708b728769e0056fa70b3ea77e50e19c to your computer and use it in GitHub Desktop.
Save notslang/708b728769e0056fa70b3ea77e50e19c to your computer and use it in GitHub Desktop.
Extract files from an ignition config
const { ArgumentParser } = require('argparse')
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const packageInfo = require('./package')
const argparser = new ArgumentParser({
addHelp: true,
description: 'Extract files from an ignition config',
version: packageInfo.version
})
argparser.addArgument(['directory'], {
help: 'The DIRECTORY to unpack into.',
type: 'string',
metavar: 'DIRECTORY'
})
const argv = argparser.parseArgs()
process.stdin.setEncoding('utf8')
process.stdin.on('readable', function () {
var buffer, chunk
buffer = ''
while ((chunk = process.stdin.read()) !== null) {
buffer += chunk
}
if (buffer !== '') {
unpackConfig(JSON.parse(buffer))
}
})
const unpackConfig = function (config) {
var file, filePath, i, len, ref, results
ref = config.storage.files
results = []
for (i = 0, len = ref.length; i < len; i++) {
file = ref[i]
filePath = file.path
if (file.filesystem !== 'root') {
filePath = path.join(file.filesystem, filePath)
}
filePath = path.join(argv.directory, filePath)
mkdirp.sync(path.dirname(filePath))
fs.writeFileSync(filePath, decodeURIComponent(file.contents.source.slice(6)), {
encoding: 'utf8',
mode: file.mode
})
results.push(fs.chownSync(filePath, file.user.id, file.group.id))
}
return results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment