Skip to content

Instantly share code, notes, and snippets.

@rodtreweek
Created August 16, 2017 17:47
Show Gist options
  • Save rodtreweek/d1ce5ed8925b8d1af6ac90a2fc71d1e4 to your computer and use it in GitHub Desktop.
Save rodtreweek/d1ce5ed8925b8d1af6ac90a2fc71d1e4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'yaml'
require 'json'
def puppet_parser_validate(file)
system('puppet parser validate --storeconfigs ' + file)
end
def r10k_puppetfile_check(file)
system('r10k puppetfile check ' + file)
end
def bash_check(file)
system('bash -n ' + file)
end
def shell_check(file)
system('shellcheck ' + file)
end
def puppet_lint(file)
system('puppet-lint --log-format="%{filename} - %{KIND}:%{check} - %{message} on line %{line}" ' + File.absolute_path(file))
end
def puppet_epp_check(file)
system('puppet epp validate ' + file)
end
def puppet_erb_check(file)
system("erb -x -T '-' #{file} | ruby -c")
end
def yaml_check(file)
YAML.load_file(file)
rescue Exception => err
puts "YAML invalid: #{err}"
false
end
def json_check(file)
JSON.parse( IO.read(file) )
rescue Exception => err
puts "JSON invalid: #{err}"
false
end
# Go through list of files, and call an appropriate checker.
IO.popen('git diff --cached --name-only --diff-filter=ACM').readlines.each { |file|
file.sub!(/^\w (.*)\n/,'\1')
file.chomp! unless file.nil?
puts "Processing #{file}"
if file.match('\.pp$')
exit 1 unless puppet_parser_validate file
exit 1 unless puppet_lint file
elsif file.match('\.epp$')
exit 1 unless puppet_epp_check file
elsif file.match('\.erb$')
exit 1 unless puppet_erb_check file
elsif file.match('Puppetfile')
exit 1 unless r10k_puppetfile_check file
elsif file.match('\.sh$')
exit 1 unless bash_check file
exit 1 unless shell_check file
elsif file.match(/(\.yaml$|\.yml$|\.eyaml$)/)
exit 1 unless yaml_check file
elsif file.match('\.json$')
exit 1 unless json_check file
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment