Module: Chsource::CLI

Defined in:
lib/chsource/cli.rb

Class Method Summary collapse

Class Method Details

.check_inputObject



8
9
10
# File 'lib/chsource/cli.rb', line 8

def self.check_input
  ARGV << '-h' if ARGV.empty? && $stdin.tty? || invalid_input?
end

.invalid_input?Boolean

Returns:

  • (Boolean)


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

def self.invalid_input?
  ARGV.size > 1 || !Chsource::Source.keys.include?(ARGV[0].to_sym)
end

.no_gem_fileObject



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

def self.no_gem_file
  if !File.exist? 'Gemfile'
    puts "\e[31mCan't find Gemfile in current directory\e[0m"
    true
  else
    false
  end
end

.start(args) ⇒ Object



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

def self.start(args)
  check_input

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: chsource SOURCE" 

    opts.separator " "

    opts.separator "Examples:"
    opts.separator "  $ chsource taobao   => source 'http://ruby.taobao.org'"
    opts.separator "  $ chsource gzruby   => source 'http://gems.gzruby.org"
    opts.separator "  $ chsource tsinghua => source 'http://mirrors.tuna.tsinghua.edu.cn/rubygems'"
    opts.separator "  $ chsource rubygems => source 'http://rubygems.org'"

    opts.separator " "
    opts.separator "Options:"

    opts.on("-v", "--version", "Print the version") do |v|
      puts "Chsource v#{VERSION}"
      exit
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.parse!(args)
  end

  exit if no_gem_file

  Chsource.change_source ARGV[0]
end