#!/usr/bin/env ruby

require 'optparse'
require_relative '../lib/validator/cli'

options = {}
required_options = {stemcell: '--stemcell', config_path: '--config'}
option_parser = OptionParser.new do |parser|
  parser.banner = 'Usage: cf-openstack-validator [options]'

  parser.on('-h', '--help', 'Prints this help') do
    puts parser
    exit
  end

  parser.on('-r', '--cpi-release RELEASE', 'CPI release .tgz path. Latest version will be downloaded if not specified (optional)') do |release|
    options[:cpi_release] = release
  end

  parser.on('-s', "#{required_options[:stemcell]} STEMCELL", 'Stemcell path') do |stemcell|
    options[:stemcell] = stemcell
  end

  parser.on('-c', "#{required_options[:config_path]} CONFIG_FILE", 'Configuration YAML file path') do |config_path|
    options[:config_path] = config_path
  end

  parser.on('-t', '--tag TAG', 'Run tests that match a specified RSpec tag (optional)') do |tag|
    options[:tag] = tag
  end

  parser.on('-k', '--skip-cleanup', 'Skip cleanup of OpenStack resources (optional)') do
    options[:skip_cleanup] = true
  end

  parser.on('-v', '--verbose', 'Print more output for failing tests (optional)') do
    options[:verbose] = true
  end

  parser.on('-f', '--fail-fast', 'Stop execution after the first test failure (optional)') do
    options[:fail_fast] = true
  end
end
option_parser.parse!

missing_required_options = required_options.keys.select do |required_option|
  !options.include?(required_option)
end

unless missing_required_options.empty?
  STDERR.puts("Required options are missing: #{missing_required_options.map { |o| required_options[o] }.join(", ")}")
  puts option_parser
  exit 1
end

context = Validator::Cli::Context.new(options)
validator = Validator::Cli::CfOpenstackValidator.create(context)
validator.run
