Created
March 27, 2021 22:50
-
-
Save notslang/708b728769e0056fa70b3ea77e50e19c to your computer and use it in GitHub Desktop.
Extract files from an ignition config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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