Class: SpreenClean::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/spreen_clean/cli.rb,
sig/generated/spreen_clean/cli.rbs

Overview

Command line interface behind the file-clean executable: file-clean [PATTERN] [--dirname DIR] [--mode d|e] [--yes].

Constant Summary collapse

MODES =

: Array

Returns:

  • (Array[String])
%w[d e].freeze
BULK_DELETE_THRESHOLD =

The execution mode asks for confirmation above this many matches.

Returns:

  • (Integer)
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • argv (Array[String])


24
25
26
27
28
29
30
# File 'lib/spreen_clean/cli.rb', line 24

def initialize(argv)
  @argv              = argv.dup
  @dirname           = '.'
  @mode              = 'd'
  @skip_confirmation = false
  @action            = :clean_files
end

Instance Attribute Details

#actionSymbol (readonly)

: Symbol

Returns:

  • (Symbol)


47
48
49
# File 'lib/spreen_clean/cli.rb', line 47

def action
  @action
end

#argvArray[String] (readonly)

: Array

Returns:

  • (Array[String])


43
44
45
# File 'lib/spreen_clean/cli.rb', line 43

def argv
  @argv
end

#dirnameString (readonly)

: String

Returns:

  • (String)


44
45
46
# File 'lib/spreen_clean/cli.rb', line 44

def dirname
  @dirname
end

#modeString (readonly)

: String

Returns:

  • (String)


45
46
47
# File 'lib/spreen_clean/cli.rb', line 45

def mode
  @mode
end

#skip_confirmationBoolean (readonly)

: bool

Returns:

  • (Boolean)


46
47
48
# File 'lib/spreen_clean/cli.rb', line 46

def skip_confirmation
  @skip_confirmation
end

Class Method Details

.start(argv = ARGV) ⇒ Integer

Parameters:

  • argv (Array[String]) (defaults to: ARGV)

Returns:

  • (Integer)


18
19
20
# File 'lib/spreen_clean/cli.rb', line 18

def self.start(argv = ARGV)
  new(argv).run
end

Instance Method Details

#clean_filesInteger

Returns:

  • (Integer)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spreen_clean/cli.rb', line 50

def clean_files
  application = Application.new(dirname:, pattern:, mode:)
  application.validate_mode!
  application.validate_dirname!
  unless confirmed?(application)
    warn 'Aborted the execution mode without deleting anything.'
    return 1
  end

  application.run
  0
end

#confirmed?(application) ⇒ Boolean

Guards the execution mode against oversized sweeps: above BULK_DELETE_THRESHOLD matches it asks for an explicit y, unless --yes was given for scripted use.

Parameters:

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spreen_clean/cli.rb', line 68

def confirmed?(application)
  return true unless mode == 'e'
  return true if skip_confirmation

  count = application.files.length
  return true if count <= BULK_DELETE_THRESHOLD

  print "About to delete #{count} files (more than #{BULK_DELETE_THRESHOLD}). Type `y` to proceed: "
  answer = $stdin.gets
  return false if answer.nil?

  %w[y yes].include?(answer.strip.downcase)
end

#parserOptionParser

Returns:

  • (OptionParser)


100
101
102
103
104
105
106
107
108
109
# File 'lib/spreen_clean/cli.rb', line 100

def parser
  @parser ||= OptionParser.new('Usage: file-clean [PATTERN] [options]') do |opt|
    opt.on('--dirname DIR', 'Directory tree to clean (default: .)') { |value| @dirname = value }
    opt.on('--mode MODE', MODES,
           "Operation mode (#{MODES.join(' or ')}; d = dry run, e = execute, default: d)") { |value| @mode = value }
    opt.on('--yes', 'Skip the bulk-delete confirmation in the execution mode') { @skip_confirmation = true }
    opt.on('--version', 'Print the version') { @action = :print_version }
    opt.on('-h', '--help', 'Print this help') { @action = :print_help }
  end
end

#patternString

Returns:

  • (String)


95
96
97
# File 'lib/spreen_clean/cli.rb', line 95

def pattern
  argv.shift || '*'
end

Returns:

  • (Integer)


89
90
91
92
# File 'lib/spreen_clean/cli.rb', line 89

def print_help
  puts parser
  0
end

Returns:

  • (Integer)


83
84
85
86
# File 'lib/spreen_clean/cli.rb', line 83

def print_version
  puts VERSION
  0
end

#runInteger

Returns:

  • (Integer)


33
34
35
36
37
38
39
# File 'lib/spreen_clean/cli.rb', line 33

def run
  parser.parse!(argv)
  __send__(action)
rescue Application::InvalidModeError, Application::RootDirnameError, OptionParser::ParseError => e
  warn e.message
  1
end