ManageIQ/httpd_configmap_generator

View on GitHub
bin/httpd_configmap_generator

Summary

Maintainability
Test Coverage
#! /usr/bin/env ruby
#
# Script for configuring container with external authentication
#
# Stdout: standard processing messages ...
# Stderr: any errors during configuration ...
# -o filename: for the generated auth-config map.
#

Dir.chdir(__dir__) { require "bundler/setup" }
require "optimist"
require "httpd_configmap_generator"

CMD = File.basename($PROGRAM_NAME)

def error_msg(msg)
  $stderr.puts msg
  exit 1
end

module HttpdConfigmapGenerator
  class Cli
    SUB_COMMANDS = [HttpdConfigmapGenerator.supported_auth_types] | %w(update export)

    def run
      Optimist.options do
        version("#{CMD} #{HttpdConfigmapGenerator::VERSION} - External Authentication Configuration script")
        banner <<-EOS
#{version}

Usage: #{CMD} auth_type | update | export [--help | options]

supported auth_type: #{HttpdConfigmapGenerator.supported_auth_types.sort.join(', ')}

#{CMD} options are:
      EOS
        opt :version, "Version of the #{CMD} command",
            :default => false, :short => "-V"
        stop_on(SUB_COMMANDS)
      end

      auth_type = ARGV.shift
      Optimist.die "Must specify an authentication type" if auth_type.nil?

      begin
        auth_config =
          case auth_type
          when "update" then HttpdConfigmapGenerator::Update.new
          when "export" then HttpdConfigmapGenerator::Export.new
          else HttpdConfigmapGenerator.new_config(auth_type)
          end
      rescue => err
        error_msg(err.to_s)
      end

      params = Optimist.options do
        auth_config.required_options.each do |key, key_options|
          opt key, key_options[:description], HttpdConfigmapGenerator::Cli.options_for(key_options, true)
        end
        auth_config.optional_options.each do |key, key_options|
          opt key, key_options[:description], HttpdConfigmapGenerator::Cli.options_for(key_options)
        end
      end

      case auth_type
      when "update" then auth_config.update(params)
      when "export" then auth_config.export(params)
      else auth_config.run_configure(params)
      end
    end

    def self.options_for(key_options, required = false)
      options = {}
      if key_options[:default].nil?
        options[:type] = key_options[:type] || :string
      else
        options[:default] = key_options[:default]
      end
      options[:required] = required
      options[:short]    = key_options[:short] if key_options[:short]
      options[:multi]    = key_options[:multi] if key_options[:multi]
      options
    end
  end
end

HttpdConfigmapGenerator::Cli.new.run