Sha256: e998f373e6c1a98bdd77adf627445fe24a67c582b76ecf572a075419e2f4c2ce

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require_relative 'base'

module WhirledPeas
  module Command
    # Abstract command that expects a config file as an argument and then requires the
    # specified file. All implementing classes must call `super` if they override `start`
    # or `validate!`
    class ConfigCommand < Base
      def start
        require config_file
      rescue LoadError
        puts "Error loading #{config_file}"
        exit(1)
      end

      private

      attr_reader :config_file

      def validate!
        super
        # Note that the main script consumes the <command> argument from ARGV, so we
        # expect the config file to be at index 0.
        config_file = args.shift
        if config_file.nil?
          @error_text = "#{command_name} requires a config file"
        elsif !File.exist?(config_file)
          @error_text = "File not found: #{config_file}"
        elsif config_file[-3..-1] != '.rb'
          @error_text = 'Config file should be a .rb file'
        else
          # We think we have a valid ruby config file, set the absolute path to @config
          @config_file = config_file[0] == '/' ? config_file : File.join(Dir.pwd, config_file)
        end
      end

      def options_usage
        '<config file>'
      end
    end
    private_constant :ConfigCommand
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whirled_peas-0.10.0 lib/whirled_peas/command/config_command.rb