class FPM::Command::Validator

A simple flag validator

The goal of this class is to ensure the flags and arguments given are a valid configuration.

Public Class Methods

new(command) click to toggle source
# File lib/fpm/command.rb, line 568
def initialize(command)
  @command = command
  @valid = true
  @messages = []

  validate
end

Public Instance Methods

messages() click to toggle source
# File lib/fpm/command.rb, line 636
def messages
  return @messages
end
ok?() click to toggle source
# File lib/fpm/command.rb, line 576
def ok?
  return @valid
end

Private Instance Methods

mandatory(value, message) click to toggle source
# File lib/fpm/command.rb, line 629
def mandatory(value, message)
  if value.nil? or !value
    @messages << message
    @valid = false
  end
end
validate() click to toggle source
# File lib/fpm/command.rb, line 580
def validate
  # Make sure the user has passed '-s' and '-t' flags
  mandatory(@command.input_type,
            "Missing required -s flag. What package source did you want?")
  mandatory(@command.output_type,
            "Missing required -t flag. What package output did you want?")

  # Verify the types requested are valid
  types = FPM::Package.types.keys.sort
  @command.input_type.tap do |val|
    next if val.nil?
    mandatory(FPM::Package.types.include?(val),
              "Invalid input package -s flag) type #{val.inspect}. " \
              "Expected one of: #{types.join(", ")}")
  end

  @command.output_type.tap do |val|
    next if val.nil?
    mandatory(FPM::Package.types.include?(val),
              "Invalid output package (-t flag) type #{val.inspect}. " \
              "Expected one of: #{types.join(", ")}")
  end

  @command.dependencies.tap do |dependencies|
    # Verify dependencies don't include commas (#257)
    dependencies.each do |dep|
      next unless dep.include?(",")
      splitdeps = dep.split(/\s*,\s*/)
      @messages << "Dependencies should not " \
        "include commas. If you want to specify multiple dependencies, use " \
        "the '-d' flag multiple times. Example: " + \
        splitdeps.map { |d| "-d '#{d}'" }.join(" ")
    end
  end

  if @command.inputs
    mandatory(@command.input_type == "dir", "--inputs is only valid with -s dir")
  end

  mandatory(@command.args.any? || @command.inputs || @command.input_type == 'empty',
            "No parameters given. You need to pass additional command " \
            "arguments so that I know what you want to build packages " \
            "from. For example, for '-s dir' you would pass a list of " \
            "files and directories. For '-s gem' you would pass a one" \
            " or more gems to package from. As a full example, this " \
            "will make an rpm of the 'json' rubygem: " \
            "`fpm -s gem -t rpm json`")
end