<commit_before>debug = require('debug')('s3utils-files-delete')
_ = require 'underscore'
colors = require 'colors'
program = require 'commander'
Promise = require 'bluebird'
Helpers = require '../helpers'
S3Client = require '../services/s3client'

program
.option '-c, --credentials <path>', 'set s3 credentials file path', Helpers.loadCredentials, Helpers.loadCredentials()
.option '-p, --prefix <name>', 'all files matching the prefix will be loaded'
.option '-r, --regex [name]', 'an optional RegExp used for filtering listed products (e.g.: /(.*)\.jpg/)', ''
.parse process.argv

debug 'parsing args: %s', process.argv

if program.credentials and program.prefix

  # TODO: nicer error message when credentials are missing
  s3client = new S3Client program.credentials

  debug 'using RegExp %s', program.regex
  s3client.list prefix: program.prefix
  .then (data) ->
    debug 'listing %s files', data.Contents.length
    # filter files from given regex
    regex = new RegExp program.regex, 'gi'
    files = _.filter data.Contents, (content) -> regex.test(content.Key)
    debug 'filtered %s files', files.length

    if _.size(files) > 0
      Promise.map files, (file) ->
        debug 'about to delete file %s', file.Key
        s3client.deleteFile file.Key
      , {concurrency: 5}
      .then ->
        console.log 'Files successfully deleted'.green
        process.exit 0
      .catch (error) ->
        console.log error.message.red
        process.exit 1
    else
      console.log 'No files to be deleted'.green
      process.exit 0
else
  console.log 'Missing required arguments'.red
  program.help()<commit_msg>Enable dry-run mode for files deletion. Closes #6<commit_after>debug = require('debug')('s3utils-files-delete')
_ = require 'underscore'
colors = require 'colors'
program = require 'commander'
Promise = require 'bluebird'
Helpers = require '../helpers'
S3Client = require '../services/s3client'

program
.option '-c, --credentials <path>', 'set s3 credentials file path', Helpers.loadCredentials, Helpers.loadCredentials()
.option '-p, --prefix <name>', 'all files matching the prefix will be loaded'
.option '-r, --regex [name]', 'an optional RegExp used for filtering listed products (e.g.: /(.*)\.jpg/)', ''
.option '--dry-run', 'list all files that will be deleted, but don\'t delete them', false
.parse process.argv

debug 'parsing args: %s', process.argv

if program.credentials and program.prefix

  # TODO: nicer error message when credentials are missing
  s3client = new S3Client program.credentials

  debug 'using RegExp %s', program.regex
  s3client.list prefix: program.prefix
  .then (data) ->
    debug 'listing %s files', data.Contents.length
    # filter files from given regex
    regex = new RegExp program.regex, 'gi'
    files = _.filter data.Contents, (content) -> regex.test(content.Key)
    debug 'filtered %s files', files.length

    if program.dryRun
      debug 'running is dry-run mode, no files will be deleted'
      console.log 'Following files will be deleted (if run without dry mode)'.blue
      console.log JSON.stringify files, null, 2
    else
      if _.size(files) > 0
        Promise.map files, (file) ->
          debug 'about to delete file %s', file.Key
          s3client.deleteFile file.Key
        , {concurrency: 5}
        .then ->
          console.log 'Files successfully deleted'.green
          process.exit 0
        .catch (error) ->
          console.log error.message.red
          process.exit 1
      else
        console.log 'No files to be deleted'.green
        process.exit 0
else
  console.log 'Missing required arguments'.red
  program.help()
{'fraction_non_alphanumeric': 0.1201870187018702, 'fraction_numerical': 0.007700770077007701, 'mean_word_length': 3.095720720720721, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '4540895', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1048, 'n_words': 369}