Skip to content

Commit

Permalink
dump fails when destination dir is not empty, added force flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapaezbas committed Oct 18, 2024
1 parent faada21 commit e9dfc95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions cmd/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ const output = outputter('stage', {
if (err.info && err.info.encrypted && info.ask && isTTY) {
return permit(ipc, err.info, 'dump')
}
if (err.code === 'ERR_DIR_NONEMPTY') {
return 'Dir is not empty. To overwrite: --force'
}
return `Dumping Error (code: ${err.code || 'none'}) ${err.stack}`
}
})

module.exports = (ipc) => async function dump (cmd) {
const { checkout, json, encryptionKey, ask } = cmd.flags
const { checkout, json, encryptionKey, ask, force } = cmd.flags
const { link } = cmd.args
let { dir } = cmd.args
if (!link) throw ERR_INVALID_INPUT('<link> must be specified.')
if (!dir) throw ERR_INVALID_INPUT('<dir> must be specified.')
dir = dir === '-' ? '-' : (isAbsolute(dir) ? dir : resolve('.', dir))
await output(json, ipc.dump({ id: Bare.pid, link, dir, checkout, encryptionKey }), { ask }, ipc)
await output(json, ipc.dump({ id: Bare.pid, link, dir, checkout, encryptionKey, force }), { ask }, ipc)
}
1 change: 1 addition & 0 deletions cmd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ module.exports = async (ipc, argv = Bare.argv.slice(1)) => {
arg('<dir>', 'Directory path to dump to, may be - for stdout'),
flag('--checkout <n>', 'Dump from specified checkout, n is version length'),
flag('--json', 'Newline delimited JSON output'),
flag('--force|-f', 'Force overwrite existing files'),
flag('--no-ask', 'Suppress permissions dialogs'),
hiddenFlag('--encryption-key <name>', 'Application encryption key'),
runners.dump(ipc)
Expand Down
9 changes: 7 additions & 2 deletions subsystems/sidecar/ops/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ const Store = require('../lib/store')
const Opstream = require('../lib/opstream')
const parseLink = require('../../../lib/parse-link')
const Hyperdrive = require('hyperdrive')
const { ERR_PERMISSION_REQUIRED } = require('../../../errors')
const { ERR_PERMISSION_REQUIRED, ERR_DIR_NONEMPTY } = require('../../../errors')
const hypercoreid = require('hypercore-id-encoding')

module.exports = class Dump extends Opstream {
constructor (...args) { super((...args) => this.#op(...args), ...args) }

async #op ({ link, dir, checkout, encryptionKey }) {
async #op ({ link, dir, checkout, encryptionKey, force }) {
const { session, sidecar } = this
await sidecar.ready()

const files = await fsp.readdir(dir)
const empty = files.length === 0
if (empty === false && !force) throw new ERR_DIR_NONEMPTY('Dir is not empty. To overwrite: --force')

const parsed = parseLink(link)
const isFileLink = parsed.protocol === 'file:'
const localFile = isFileLink && (await fsp.stat(parsed.pathname)).isDirectory() === false
Expand Down

0 comments on commit e9dfc95

Please sign in to comment.