Module: BitFlipper::CLI

Defined in:
lib/corruption_monkey/cli.rb

Constant Summary collapse

DOCS =
"CLI options:\n\n  1. Specific bit flip (by absolute or relative indexing)\n    $ flip <file_path> (<bit_index>|<percentage of file>%)\n\n  2. Chaos mode (randomized)\n    $ flip <file_path> chaos\n\nTo display help:\n  $ flip help\n"

Class Method Summary collapse

Class Method Details



17
18
19
20
21
22
# File 'lib/corruption_monkey/cli.rb', line 17

def self.banner(s = $stdout)
  s.puts 'πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ '
  s.puts 'πŸ”₯ bit flipper chaos πŸ™ˆ  CLIπŸ”₯'
  s.puts 'πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ πŸ”₯ '
  s.puts
end

.start(args = ARGV) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/corruption_monkey/cli.rb', line 24

def self.start(args = ARGV)
  file_path = args.shift
  second_argument = args.shift

  # Empty imput or docs request
  if (file_path.nil? && second_argument.nil?) ||
      (!File.file?(file_path) && file_path == 'help')
    puts DOCS
    exit 0
  end

  # First argument is not a (existing) file
  unless File.file?(file_path)
    $stderr.puts "The file with path=`#{file_path}` does not exist"
    exit 1
  end

  logger = Logger.new(STDOUT)
  banner

  case second_argument
  when 'chaos'
    puts 'chaos mode selected'
    puts
    BitFlipper.random_bit_flip(file_path, logger: logger)
  when /([0-9]{1, 2}|100)%/
    # this regex feels... weird
    puts 'percent mode selected'
    puts
    BitFlipper.flip_in_range(file_path,
                             percentile: second_argument.to_i,
                             logger: logger)
  when /[0-9]+/
    puts 'accurate mode selected'
    puts
    BitFlipper.flip!(file_path,
                     bit: second_argument.to_i,
                     logger: logger)
  else
    $stderr.puts 'nothing matches!'
    $stderr.puts DOCS
  end
end