This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
puppetfile-cli.rb
114 lines (90 loc) · 3.33 KB
/
puppetfile-cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
# This is not supposed to be a fully fledged CLI for the resolver, as this gem is designed to be used
# as a library. The CLI is offered as an example as to how to use the library.
# Add the resolver into the load path
lib_root = File.expand_path(File.join(__dir__, 'lib'))
$LOAD_PATH.unshift(lib_root)
require 'puppetfile-resolver'
require 'optparse'
class CommandLineParser
def self.parse(options)
# Set defaults here
args = {
debug: false,
cache_dir: nil,
module_paths: [],
path: nil,
proxy: nil,
puppet_version: nil,
strict: false
}
opt_parser = OptionParser.new do |opts|
opts.banner = 'Usage: puppetfile-cli.rb [options]'
opts.on('-pPATH', '--path=PATH', 'Puppetfile to parse') do |path|
args[:path] = path
end
opts.on('-vVERSION', '--puppet_version=VERSION', 'Restrict the resolver to only modules which support the specified Puppet version') do |version|
args[:puppet_version] = version
end
opts.on('-cDIR', '--cache_directory=DIR', 'Directory to the persistent on disk cache. Optional') do |cache_dir|
args[:cache_dir] = cache_dir
end
opts.on('--debug', 'Output debug information. Default is no debug output') do
args[:debug] = true
end
opts.on('--proxy=PROXY_URL', 'HTTP/S Proxy server to use. For example http://localhost:8888') do |proxy|
args[:proxy] = proxy
end
opts.on('-s', '--strict', 'Do not allow missing dependencies. Default false which marks dependencies as missing and does not raise an error.') do
args[:strict] = true
end
opts.on('-mTEXT', '--module_paths=TEXT', Array, 'Comma delimited list of modules paths to search') do |text|
args[:module_paths] = text
end
end
opt_parser.parse!(options.dup)
args
end
end
options = CommandLineParser.parse(ARGV)
raise 'Missing --path' if options[:path].nil?
# Configure the cache
if options[:cache_dir].nil?
cache = nil
else
require 'puppetfile-resolver/cache/persistent'
cache = PuppetfileResolver::Cache::Persistent.new(options[:cache_dir])
end
# Parse the Puppetfile into an object model
content = File.open(options[:path], 'rb') { |f| f.read }
require 'puppetfile-resolver/puppetfile/parser/r10k_eval'
puppetfile = ::PuppetfileResolver::Puppetfile::Parser::R10KEval.parse(content)
# Make sure the Puppetfile is valid
unless puppetfile.valid?
puts 'Puppetfile is not valid'
puppetfile.validation_errors.each { |err| puts err }
exit 1
end
# Create the resolver
resolver = PuppetfileResolver::Resolver.new(puppetfile, options[:puppet_version])
# Configure the resolver
if options[:debug]
require 'puppetfile-resolver/ui/debug_ui'
ui = PuppetfileResolver::UI::DebugUI.new
else
ui = nil
end
config = PuppetfileResolver::SpecSearchers::Configuration.new
config.local.puppet_module_paths = options[:module_paths]
unless options[:proxy].nil?
config.git.proxy = options[:proxy]
config.forge.proxy = options[:proxy]
end
opts = { cache: cache, ui: ui, spec_searcher_configuration: config, allow_missing_modules: !options[:strict] }
# Resolve
result = resolver.resolve(opts)
# Output errors
result.validation_errors.each { |err| puts "Resolution Validation Error: #{err}\n" }
# Output the Graph in a DOT format
puts "\n--- Dependency Graph"
puts result.to_dot