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
|
# File 'lib/vagrant-templated/command/init.rb', line 8
def execute
defaults = YAML.load_file File.join(File.dirname(File.expand_path(__FILE__)), '../templates/defaults.yml')
options = {}
opts = OptionParser.new do |o|
o.banner = 'Usage: vagrant templated init [options] <template>'
o.separator ''
o.separator 'Templates availables: rails'
o.separator ''
o.separator ' rails5'
o.separator ' vagrant-plugin'
o.separator ''
o.separator 'Options:'
o.separator ''
o.on('-f', '--force', 'Overwrite an existing box if it exists') do |f|
options[:force] = f
end
o.on('-s', '--suffix SUFFIX', String,
"Output suffix for Vagrantfile and Bersfile. '-' for stdout") do |suffix|
options[:suffix] = suffix
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 defaults.keys.include? 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.join(File.dirname(__FILE__), '../templates/Vagrantfile.erb')), nil, '-'
berksfile = ERB.new File.read(File.join(File.dirname(__FILE__), '../templates/Berksfile.erb')), 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
|