Class: Fig::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/parser.rb

Overview

Parses configuration files and deals with a few restrictions on them.

Instance Method Summary collapse

Constructor Details

#initialize(application_config) ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
# File 'lib/fig/parser.rb', line 14

def initialize(application_config)
  @parser = FigParser.new
  @application_config = application_config
end

Instance Method Details

#find_bad_urls(package, package_name, version_name) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fig/parser.rb', line 19

def find_bad_urls(package, package_name, version_name)
  bad_urls = []
  package.walk_statements do |statement|
    statement.urls.each do |url|
      # collect all bad urls in bad_urls
      next if not Repository.is_url?(url)
      bad_urls << url if not @application_config.url_access_allowed?(url)
    end
  end

  raise URLAccessError.new(bad_urls, package_name, version_name) if not bad_urls.empty?
end

#find_multiple_command_statements(package) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fig/parser.rb', line 32

def find_multiple_command_statements(package)
  command_processed = false
  package.walk_statements do |statement|
    if statement.is_a?(Package::Command)
      if command_processed == true
        raise UserInputError.new("Multiple command statements cannot be processed.")
      end
      command_processed = true
    elsif statement.is_a?(Package::Configuration)
      command_processed = false
    end
  end
end

#parse_package(package_name, version_name, directory, input) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fig/parser.rb', line 46

def parse_package(package_name, version_name, directory, input)
  input = input.gsub(/#.*$/, '')
  result = @parser.parse(" #{input} ")

  if result.nil?
    Logging.fatal "#{directory}: #{@parser.failure_reason}"
    raise PackageError.new("#{directory}: #{@parser.failure_reason}")
  end

  package = result.to_package(package_name, version_name, directory)

  find_bad_urls(package, package_name, version_name)
  find_multiple_command_statements(package)

  return package
end