Class: IPASend::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ipasend/option_parser.rb

Class Method Summary collapse

Class Method Details

.parseObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/ipasend/option_parser.rb', line 9

def self.parse

  show_version = false
  config_path = nil

  parser = ::OptionParser.new do |opts|
    opts.banner = "Usage: ipasend [options] <archive path>"

    opts.on("-v", "--version", "Show version.") do |v|
      show_version = v
    end

    opts.on("-c", "--config=config_path", "Path to a config file in YAML format.") do |v|
      config_path = v
    end
  end

  parser.parse!

  if show_version
    puts "ipasend: #{IPASend::VERSION}"
    exit 0
  end

  archive_path ||= ARGV[0]

  if archive_path.nil?
    puts parser.help
    raise 'Archive path is mandatory'
  end

  if config_path
    config = YAML.load_file(config_path) rescue nil
    if config.nil?
      puts "Cannot read configuration file #{config_path}"
      exit 1
    end
    config = OpenStruct.new(config)
    config.config_path = File.expand_path(config_path)
    config.archive_path = archive_path
    config.prefix ||= "apps/#{SecureRandom.uuid}"
    return config
  else
    raise 'Config file is mandatory'
  end

  puts "ipasend #{IPASend::VERSION} starting..."
end