A simple flag validator
The goal of this class is to ensure the flags and arguments given are a valid configuration.
# File lib/fpm/command.rb, line 524 def initialize(command) @command = command @valid = true @messages = [] validate end
# File lib/fpm/command.rb, line 592 def messages return @messages end
# File lib/fpm/command.rb, line 532 def ok? return @valid end
# File lib/fpm/command.rb, line 585 def mandatory(value, message) if value.nil? or !value @messages << message @valid = false end end
# File lib/fpm/command.rb, line 536 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 with(@command.input_type) 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 with(@command.output_type) 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 with (@command.dependencies) 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