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
|
# File 'lib/vagrant-templated/command/init.rb', line 8
def execute
root_path = File.expand_path '../../../', File.dirname(File.expand_path(__FILE__))
templates_attributes = Dir.glob(File.expand_path("config/templates/attributes/*.yml", root_path)).collect do |template|
YAML.load_file(template)
end.reduce Hash.new, :merge
options = {}
opts = OptionParser.new do |o|
o.banner = 'Usage: vagrant templated init [options] <template>'
o.separator ''
o.separator 'Templates availables:'
o.separator ''
templates_attributes.keys.each do |template|
o.separator " #{template}"
end
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('-s', '--suffix SUFFIX', String,
"Output suffix for Vagrantfile and Berksfile. '-' for stdout") do |suffix|
options[:suffix] = suffix
end
o.on('-f', '--force', 'Overwrite an existing box if it exists') do |f|
options[:force] = f
end
end
argv = parse_options(opts)
return if !argv
if argv.empty? || argv.length > 2
raise Vagrant::Errors::CLIInvalidUsage,
help: opts.help.chomp
end
template = argv[0]
if argv.length == 2
options[:name] = argv[0]
template = argv[1]
end
@env.ui.info("Detected template: #{template}", prefix: false, color: :yellow)
raise Vagrant::Errors::VagrantTemplatedOptionNotFound unless templates_attributes.keys.include? template
template_attributes = templates_attributes[template]
vagrantfile_save_path = nil
berksfile_save_path = nil
if options[:suffix] != "-"
vagrantfile_save_path = Pathname.new(['Vagrantfile', options[:suffix]].compact.join('.')).expand_path(@env.cwd)
vagrantfile_save_path.delete if vagrantfile_save_path.exist? && options[:force]
raise Vagrant::Errors::VagrantfileTemplatedExistsError if vagrantfile_save_path.exist?
berksfile_save_path = Pathname.new(['Berksfile', options[:suffix]].compact.join('.')).expand_path(@env.cwd)
berksfile_save_path.delete if berksfile_save_path.exist? && options[:force]
raise Vagrant::Errors::BerksfileTemplatedExistsError if berksfile_save_path.exist?
end
vagrantfile = ERB.new File.read(File.expand_path('config/templates/files/Vagrantfile.erb', root_path)), nil, '-'
berksfile = ERB.new File.read(File.expand_path('config/templates/files/Berksfile.erb', root_path)), nil, '-'
contents = vagrantfile.result binding
if vagrantfile_save_path
begin
vagrantfile_save_path.open('w+') do |f|
f.write(contents)
end
rescue Errno::EACCES
raise Vagrant::Errors::VagrantfileWriteError
end
@env.ui.info('Templated Vagranfile created successfully', prefix: false)
else
@env.ui.info('VAGRANTFILE', prefix: false, color: :green)
@env.ui.info(contents, prefix: false)
end
contents = berksfile.result binding
if berksfile_save_path
begin
berksfile_save_path.open('w+') do |f|
f.write(contents)
end
rescue Errno::EACCES
raise Vagrant::Errors::BerksfileWriteError
end
@env.ui.info('Templated Berksfile created successfully', prefix: false)
else
@env.ui.info('BERKSFILE', prefix: false, color: :green)
@env.ui.info(contents, prefix: false)
end
@env.ui.info('Vagrant Templated finished successfully.', prefix: false, color: :green)
0
end
|