<commit_before>#!/usr/bin/env node

var fs = require('fs')
var minimist = require('minimist')
var mkdirp = require('mkdirp')
var path = require('path')
var transports = require('transport-stream')({command: 'hyperfs replicate -'})
var execspawn = require('execspawn')
var argv = minimist(process.argv.slice(2), {alias: {message: 'm', quiet: 'q', debug: 'D', store: 's', node: 'n'}})
var s = argv.store || 'hyperfs'

mkdirp.sync(s)

var cauf = require('./')(s)

var cmd = argv._[0]

if (cmd === 'list' || cmd === 'ls') {
  cauf.list().on('data', console.log)
  return
}

if (cmd === 'show') {
  cauf.show(argv._[1], function (err, val) {
    if (err) throw err
    cauf.readSnapshot(val.snapshot, function (err, rs) {
      if (err) throw err
      rs.on('data', function (data) {
        console.log(JSON.stringify(data))
      })
    })
  })
  return
}

if (cmd === 'version') {
  console.log(require('./package.json').version)
  return
}

if (cmd === 'nodes') {
  cauf.nodes().on('data', function (data) {
    console.log(data.key+': ' + (data.value.message || '(no message)'))
  })
  return
}

if (cmd === 'remove' || cmd === 'rm') {
  cauf.remove(argv._[1], function () {
    console.log(argv._[1] + ' removed')
  })
  return
}

if (cmd === 'info') {
  cauf.info(argv._[1], console.log)
  return
}

if (cmd === 'create') {
  if (!argv._[1]) throw new Error('volume required')
  cauf.create(argv._[1], argv, console.log)
  return
}

if (cmd === 'replicate') {
  var stream = transports(argv._[1])
  var rs = cauf.replicate(argv)

  stream.on('warn', console.error)

  if (argv._[1] !== '-' && !argv.quiet) {
    rs.on('receive-data', function (data) {
      console.error('receive-data: ' + data)
    })

    rs.on('receive-snapshot', function (hash) {
      console.error('receive-snapshot: ' + hash)
    })

    rs.on('send-data', function (data) {
      console.error('send-data: ' + data)
    })

    rs.on('send-snapshot', function (hash) {
      console.error('send-snapshot: ' + hash)
    })
  }

  stream.pipe(rs).pipe(stream)
  return
}

if (cmd === 'exec') {
  if (!argv._[1]) throw new Error('volume required')
  var folder = argv.mnt || path.join(s, 'mnt', argv._[1])

  mkdirp(folder, function (err) {
    if (err) throw err
    var mnt = cauf.mount(argv._[1], folder, argv)

    mnt.on('ready', function () {
      var proc = execspawn(argv._[2], {
        cwd: folder
      })

      proc.stdout.pipe(process.stdout)
      proc.stderr.pipe(process.stderr)

      proc.on('exit', function (code) {
        cauf.unmount(folder, function () {
          process.exit(code)
        })
      })
    })
<commit_msg>pipe in stdin to exec<commit_after>#!/usr/bin/env node

var fs = require('fs')
var minimist = require('minimist')
var mkdirp = require('mkdirp')
var path = require('path')
var transports = require('transport-stream')({command: 'hyperfs replicate -'})
var execspawn = require('execspawn')
var argv = minimist(process.argv.slice(2), {alias: {message: 'm', quiet: 'q', debug: 'D', store: 's', node: 'n'}})
var s = argv.store || 'hyperfs'

mkdirp.sync(s)

var cauf = require('./')(s)

var cmd = argv._[0]

if (cmd === 'list' || cmd === 'ls') {
  cauf.list().on('data', console.log)
  return
}

if (cmd === 'show') {
  cauf.show(argv._[1], function (err, val) {
    if (err) throw err
    cauf.readSnapshot(val.snapshot, function (err, rs) {
      if (err) throw err
      rs.on('data', function (data) {
        console.log(JSON.stringify(data))
      })
    })
  })
  return
}

if (cmd === 'version') {
  console.log(require('./package.json').version)
  return
}

if (cmd === 'nodes') {
  cauf.nodes().on('data', function (data) {
    console.log(data.key+': ' + (data.value.message || '(no message)'))
  })
  return
}

if (cmd === 'remove' || cmd === 'rm') {
  cauf.remove(argv._[1], function () {
    console.log(argv._[1] + ' removed')
  })
  return
}

if (cmd === 'info') {
  cauf.info(argv._[1], console.log)
  return
}

if (cmd === 'create') {
  if (!argv._[1]) throw new Error('volume required')
  cauf.create(argv._[1], argv, console.log)
  return
}

if (cmd === 'replicate') {
  var stream = transports(argv._[1])
  var rs = cauf.replicate(argv)

  stream.on('warn', console.error)

  if (argv._[1] !== '-' && !argv.quiet) {
    rs.on('receive-data', function (data) {
      console.error('receive-data: ' + data)
    })

    rs.on('receive-snapshot', function (hash) {
      console.error('receive-snapshot: ' + hash)
    })

    rs.on('send-data', function (data) {
      console.error('send-data: ' + data)
    })

    rs.on('send-snapshot', function (hash) {
      console.error('send-snapshot: ' + hash)
    })
  }

  stream.pipe(rs).pipe(stream)
  return
}

if (cmd === 'exec') {
  if (!argv._[1]) throw new Error('volume required')
  var folder = argv.mnt || path.join(s, 'mnt', argv._[1])

  mkdirp(folder, function (err) {
    if (err) throw err
    var mnt = cauf.mount(argv._[1], folder, argv)

    mnt.on('ready', function () {
      var proc = execspawn(argv._[2], {
        cwd: folder
      })

      proc.stdout.pipe(process.stdout)
      proc.stderr.pipe(process.stderr)
      process.stdin.pipe(proc.stdin)

      proc.on('exit', function (code) {
        cauf.unmount(folder, function () {
          process.exit(code)
        })
      })
    })

{'fraction_non_alphanumeric': 0.20634321313951293, 'fraction_numerical': 0.00528601094959411, 'mean_word_length': 3.0227790432801824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7066161', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1929, 'n_words': 409}