Class: UniFaCop::CLI

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

Constant Summary collapse

CONFIG_FILE_NAME =
'.rubocop.yml'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



33
34
35
36
# File 'lib/unifacop/cli.rb', line 33

def self.print_help
  puts 'unifacop commands:'
  puts '  init - Setup .rubocop.yml'
end

.retrieve_command_name(args) ⇒ Object



26
27
28
29
30
31
# File 'lib/unifacop/cli.rb', line 26

def self.retrieve_command_name(args)
  return nil if args.empty?

  meth = args.first.to_s
  args.shift unless /^-/.match?(meth)
end

.start(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/unifacop/cli.rb', line 5

def self.start(args)
  action_name = retrieve_command_name(args)
  unless action_name
    print_help
    exit
  end

  instance = self.new
  if instance.public_methods(false).include?(action_name.to_sym)
    instance.__send__(action_name, args)
    exit
  end

  puts "Could not find command #{action_name}."
  print_help
  exit(1)
rescue StandardError => e
  puts e.message
  exit(1)
end

Instance Method Details

#init(args) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/unifacop/cli.rb', line 40

def init(args)
  raise 'usage: unifacop init' unless args.empty?

  template_path = File.expand_path('../../templates', __dir__)
  puts "#{File.exist?(CONFIG_FILE_NAME) ? 'overwrite' : 'create'} #{CONFIG_FILE_NAME}"
  FileUtils.copy_file(File.join(template_path, CONFIG_FILE_NAME), CONFIG_FILE_NAME)
end