class KnifePlugin::SyntaxCheck

Public Instance Methods

check_syntax(dirpath, dir = nil, type) click to toggle source

Common method to test file syntax

# File lib/chef/knife/syntax-check.rb, line 90
def check_syntax(dirpath, dir = nil, type)
  files = Dir.glob("#{dirpath}").select { |f| File.file?(f) }
  files.each do |file|
    fname = Pathname.new(file).basename
    if ("#{fname}".end_with?('.json'))
      ui.msg("Testing file #{file}")
      json = File.new(file, 'r')
      parser = Yajl::Parser.new
      hash = parser.parse(json)
    elsif("#{fname}".end_with?('.rb'))
      ui.msg("Testing file #{file}")
      result = `ruby -c #{file}`
    end 
  end 
end
run() click to toggle source
# File lib/chef/knife/syntax-check.rb, line 44
def run 
  if config[:roles] 
    test_object("roles/*", "role")
  elsif config[:environments]
    test_object("environments/*", "environment")
  elsif config[:nodes]
    test_object("nodes/*", "node")
  elsif config[:databags]
    test_databag("data_bags/*", "data bag")
  elsif config[:cookbooks]
    test_cookbooks()
  elsif config[:all]
    test_object("roles/*", "role")
    test_object("environments/*", "environment")
    test_object("nodes/*", "node")
    test_databag("data_bags/*", "data bag")
    test_cookbooks()
  else
    ui.msg("Usage: knife syntax-check --help")
  end 
end
test_cookbooks() click to toggle source

Test all cookbooks

# File lib/chef/knife/syntax-check.rb, line 67
def test_cookbooks()
  ui.msg("Testing all cookbooks...")
  result = `knife cookbook test --all`
  ui.msg(result)
end
test_databag(dirname, type) click to toggle source

Test databag syntax

# File lib/chef/knife/syntax-check.rb, line 80
def test_databag(dirname, type)
  ui.msg("Finding type #{type} to test from #{dirname}")
  dirs = Dir.glob(dirname).select { |d| File.directory?(d) }
  dirs.each do |directory|
    dir = Pathname.new(directory).basename
    check_syntax("#{directory}/*", dir, type)
  end 
end
test_object(dirname,type) click to toggle source

Test object syntax

# File lib/chef/knife/syntax-check.rb, line 74
def test_object(dirname,type)
  ui.msg("Finding type #{type} to test from #{dirname}")
  check_syntax(dirname,nil,type)
end