Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
simplify dryRun/listOnly logic, move into receive
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-james-watson committed Dec 8, 2018
1 parent c4cc8c4 commit 567e6b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dat-cp",
"version": "0.7.2",
"version": "0.7.3",
"description": "Dat Copy - remote file copy, powered by the dat protocol",
"engines": {
"node": ">=6.0.0"
Expand Down
54 changes: 21 additions & 33 deletions src/lib/dat-cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import prompt from './prompt'

export default class DatCp {

constructor(dat, program) {
constructor(dat, options) {
this.dat = dat
this.program = program
this.options = options
this.files = 0
this.totalSize = 0
}

async upload(paths) {
await this.ensurePathsValid(paths)

if (this.program.dryRun) {
if (this.options.dryRun) {
this.printTotal()
process.exit(0)
}
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class DatCp {
}

if (stats.isFile()) {
this.countPath(path, stats, this.program.dryRun)
this.countPath(path, stats)
return
}

Expand All @@ -67,13 +67,13 @@ export default class DatCp {
}

async ensureDirValid(path, stats) {
if (!this.program.recursive) {
if (!this.options.recursive) {
logger.warn(`${path}: Is a directory (not copied).`)
return
}

if (path[path.length - 1] !== '/') {
this.countPath(path, stats, this.program.dryRun)
this.countPath(path, stats)
}

const dirPaths = await fs.readdir(path)
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class DatCp {
}

async uploadDir(path, datPath) {
if (!this.program.recursive) {
if (!this.options.recursive) {
return
}

Expand Down Expand Up @@ -135,7 +135,7 @@ export default class DatCp {
})
}

download(listOnly=false) {
download() {
return new Promise((resolve) => {
const abort = setTimeout(() => {
logger.error('Failed to download metadata from peer.')
Expand All @@ -149,16 +149,9 @@ export default class DatCp {
clearTimeout(abort)

for (const path of paths) {
await this.downloadPath(path, listOnly)
await this.downloadPath(path)
}

if (
this.program.dryRun ||
(!listOnly && this.files > 30) ||
this.files === 0
) {
this.printTotal()
}
resolve()
} else {
setTimeout(readRoot, 300)
Expand All @@ -169,17 +162,17 @@ export default class DatCp {
})
}

async downloadPath(path, listOnly) {
async downloadPath(path) {
const stats = await this.stat(path)

if (stats.isDirectory()) {
await this.downloadDir(path, stats, listOnly)
await this.downloadDir(path, stats)
} else {
await this.downloadFile(path, stats, listOnly)
await this.downloadFile(path, stats)
}
}

async downloadFile(path, stats, listOnly) {
async downloadFile(path, stats) {
// If the file exists and is the same size, assume that it hasn't changed
// and skip it.
try {
Expand All @@ -192,9 +185,9 @@ export default class DatCp {
// File doesn't exist, do nothing.
}

this.countPath(path, stats, listOnly)
this.countPath(path, stats)

if (listOnly) {
if (this.options.dryRun) {
return
}

Expand All @@ -205,8 +198,8 @@ export default class DatCp {
await pipeStreams(readStream, writeStream, filesize, path)
}

async downloadDir(path, stats, listOnly) {
if (!listOnly) {
async downloadDir(path, stats) {
if (!this.options.dryRun) {
// lstat will throw an error if a path does not exist, so rely on that to
// know that the dir does not already exist. If the path exists and is not
// a directory, error.
Expand All @@ -221,24 +214,24 @@ export default class DatCp {
}
}

this.countPath(path, stats, listOnly)
this.countPath(path, stats)

const dirPaths = await this.readdir(path)

for (const dirPath of dirPaths) {
await this.downloadPath(nodePath.join(path, dirPath), listOnly)
await this.downloadPath(nodePath.join(path, dirPath))
}
}

countPath(path, stats, listPath) {
countPath(path, stats) {
if (path === '.') {
return
}

this.files += 1
this.totalSize += stats.size

if (!listPath) {
if (!this.options.dryRun) {
return
}

Expand All @@ -253,11 +246,6 @@ export default class DatCp {
logger.info(`\nTotal: ${this.files} files (${formatSize(this.totalSize)})`)
}

resetCounts() {
this.files = 0
this.totalSize = 0
}

async downloadPrompt() {
const answer = await prompt(
`\nDownload ${this.files} files (${formatSize(this.totalSize)})? [Y/n] `
Expand Down
28 changes: 16 additions & 12 deletions src/receive.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import Dat from './lib/dat'
import DatCp from './lib/dat-cp'

export default async function receive(key, program) {
export default async function receive(key, options) {
const dat = await Dat({key, sparse: true})

const datCp = new DatCp(dat, program)
if (!options.skipPrompt) {
const datCpDryRun = new DatCp(dat, {...options, dryRun: true})

if (!program.skipPrompt) {
await datCp.download(true)
}
await datCpDryRun.download()

if (program.dryRun || (!program.skipPrompt && datCp.files === 0)) {
process.exit()
}
if (options.dryRun || datCpDryRun.files === 0) {
datCpDryRun.printTotal()
process.exit()
}

if (!program.skipPrompt) {
const proceed = await datCp.downloadPrompt()
const proceed = await datCpDryRun.downloadPrompt()

if (!proceed) {
process.exit()
}
}

datCp.resetCounts()
await datCp.download()
const datCpDownload = new DatCp(dat, {...options})

await datCpDownload.download()

if (options.skipPrompt || datCpDownload.files > 30) {
datCpDownload.printTotal()
}

process.exit()
}
4 changes: 2 additions & 2 deletions src/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import DatCp from './lib/dat-cp'
import logger from './lib/logger'
import monitorUpload from './lib/monitor-upload'

export default async function send(paths, program) {
export default async function send(paths, options) {
const dat = await Dat()

const datCp = new DatCp(dat, program)
const datCp = new DatCp(dat, options)
await datCp.upload(paths)

logger.info('\nPaste files on another host with:\n')
Expand Down

0 comments on commit 567e6b2

Please sign in to comment.