Class: Backsum::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/backsum/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

Returns a new instance of Cli.



12
13
14
15
# File 'lib/backsum/cli.rb', line 12

def initialize(*args)
  self.options = { projects_path: "./projects" }
  self.action = nil
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



10
11
12
# File 'lib/backsum/cli.rb', line 10

def action
  @action
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/backsum/cli.rb', line 10

def options
  @options
end

Instance Method Details

#execute(argv = []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/backsum/cli.rb', line 17

def execute(argv = [])
  option_parser!(argv)

  Backsum.logger.level = :debug if ::Backsum.verbose

  if self.action
    send self.action
  else
    self.show_usage
    exit 1
  end
end

#option_parser!(argv) ⇒ Object



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
# File 'lib/backsum/cli.rb', line 30

def option_parser!(argv)
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options] action ..."

    opts.on("-V", "--version", "Display the Backsum version and exit.") do
      self.action = :show_version
    end

    opts.on("--all[=PATH]", "excute all the files. default: '#{self.options[:projects_path]}'") do |path|
      self.options[:projects_path] = path if path
      self.action = :perform_by_dir
    end

    opts.on("-v", "--[no-]verbose", "increase verbosity. default: #{Backsum.verbose}") do |value|
      Backsum.verbose = value
    end
  end

  @option_parser.parse!(argv)

  if !self.action && argv.any?
    self.action = :perform_by_inputs
    @input_files = argv
  end
end

#perform_by_dirObject



65
66
67
68
# File 'lib/backsum/cli.rb', line 65

def perform_by_dir
  files = Dir[File.join(self.options[:projects_path], '*.rb')]
  perform_files(files)
end

#perform_by_inputsObject



70
71
72
# File 'lib/backsum/cli.rb', line 70

def perform_by_inputs
  perform_files(@input_files)
end

#show_usageObject



56
57
58
59
# File 'lib/backsum/cli.rb', line 56

def show_usage
  stderr "Please specify one action to execute."
  stderr @option_parser.help
end

#show_versionObject



61
62
63
# File 'lib/backsum/cli.rb', line 61

def show_version
  stdout "Backsum v#{Backsum::VERSION}"
end