generate-puppetfile 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf16afea209bd8ac2e30eed90aeb581f0bce4465
4
- data.tar.gz: 58974916249c3e57b4c91f615bea19a2ac9a463c
3
+ metadata.gz: 4796755a11eed130c7ffdf5587687d38ec0d903f
4
+ data.tar.gz: fb597256b5e7283a42354cdf13fe9a8b572d6725
5
5
  SHA512:
6
- metadata.gz: ccf96edd51e4c63a367ba622d35b05f5e46f575d5bcd2132c40e0693f09f0794e2518119e5d6c1991e46986e2433f5d13ced307268a63584bf99d2bd82a02688
7
- data.tar.gz: dbd28f66578958bfa335fbaa9716df6a3cd1c0e09f4458584bb2eae794f7c6dcdc75795bcdf9e96ad1e4ade7f8dd31b46c7843d637ff428e832d0f496ed2e891
6
+ metadata.gz: 09b2664d006c404941337f80b7a35c3591804ef05b5a594dd446e8fc290319a0162c2075dd49bd6d3ad2de48ace8229c6038020185ceb73f31b64257d398e1cc
7
+ data.tar.gz: b8ba776dd3e63f6feb081110eb28eb3aae59433388c2bafd03d4e91fa57d53ec5ca9705ec9e487f3fed5664aeceaa3c1ad3f79197af180f68b82a377c49cba21
@@ -10,7 +10,7 @@ require 'colorize'
10
10
  module GeneratePuppetfile
11
11
  # Internal: The Bin class contains the logic for calling generate_puppetfile at the command line
12
12
  class Bin
13
- Module_Regex = Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE)
13
+ Module_Regex = Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](,\\s+['\"](\\d+\.\\d+\.\\d+)['\"])?", Regexp::IGNORECASE)
14
14
  @options = {} # Options hash
15
15
  @workspace = nil # Working directory for module download and inspection
16
16
  @module_data = {} # key: modulename, value: version number
@@ -38,26 +38,46 @@ module GeneratePuppetfile
38
38
 
39
39
  helpmsg = "generate-puppetfile: try 'generate-puppetfile --help' for more information."
40
40
 
41
+ if (@options[:fixtures_only] && ! @options[:puppetfile] )
42
+ $stderr.puts "generate-puppetfile: --fixtures-only is only valid when a '-p <Puppetfile>' is used as well.\n".red
43
+ puts helpmsg
44
+ return 1
45
+ end
46
+
41
47
  if @args[0].nil? && (! @options[:puppetfile])
42
- $stderr.puts 'generate-puppetfile: No modules or existing Puppetfile specified.'.red
48
+ $stderr.puts "generate-puppetfile: No modules or existing Puppetfile specified.\n".red
43
49
  puts helpmsg
44
50
  return 1
45
51
  end
46
52
 
47
53
  unless verify_puppet_exists
48
54
  $stderr.puts "generate-puppetfile: Could not find a 'puppet' executable.".red
49
- $stderr.puts ' Please make puppet available in your path before trying again.'.red
55
+ $stderr.puts " Please make puppet available in your path before trying again.\n".red
50
56
  return 1
51
57
  end
52
58
 
59
+
53
60
  forge_module_list = []
54
61
 
62
+ # When using --fixtures-only, simply parse the provided Puppetfile and get out
63
+ if @options[:fixtures_only]
64
+ @module_data = generate_module_data_from_Puppetfile
65
+ fixtures_data = generate_fixtures_data
66
+ write_fixtures_data(fixtures_data)
67
+ return 0
68
+ end
69
+
70
+ # For everything else, run through the whole thing
55
71
  if @args
56
72
  puts "\nProcessing modules from the command line...\n\n" if @options[:debug]
57
73
  cli_modules = []
58
74
  @args.each do |modulename|
59
75
  validate(modulename) && cli_modules.push(modulename)
60
76
  end
77
+ if cli_modules == [] && ! @options[:puppetfile]
78
+ $stderr.puts "No valid modules were found to process.".red
79
+ return 1
80
+ end
61
81
  end
62
82
 
63
83
  puppetfile_contents = {}
@@ -82,7 +102,7 @@ module GeneratePuppetfile
82
102
  @modulepath = "--modulepath #{@workspace} "
83
103
 
84
104
  return 2 if download_modules(forge_module_list) == 2
85
- @module_data = generate_module_data
105
+ @module_data = generate_module_data_from_modulepath
86
106
  puppetfile_contents = generate_puppetfile_contents(extras)
87
107
 
88
108
  if @download_errors == ''
@@ -126,7 +146,7 @@ Your Puppetfile has been generated. Copy and paste between the markers:
126
146
 
127
147
  # Public: Validates that a provided module name is valid.
128
148
  def validate(modulename)
129
- success = (modulename =~ /[a-z0-9_]\/[a-z0-9_]/i)
149
+ success = (modulename =~ /[a-z0-9_][\/-][a-z0-9_]/i)
130
150
  $stderr.puts "'#{modulename}' is not a valid module name. Skipping.".red unless success
131
151
  success
132
152
  end
@@ -178,6 +198,32 @@ Your Puppetfile has been generated. Copy and paste between the markers:
178
198
  puppetfile_contents
179
199
  end
180
200
 
201
+ # Public: Read and parse the contents of an existing Puppetfile
202
+ def read_puppetfile_with_versions(puppetfile)
203
+ puppetfile_contents = {
204
+ modules: [],
205
+ extras: []
206
+ }
207
+
208
+ File.foreach(puppetfile) do |line|
209
+ if Module_Regex.match(line)
210
+ name = Regexp.last_match(1)
211
+ version = Regexp.last_match(3)
212
+ print " #{name} looks like a forge module.\n" if @options[:debug]
213
+ puppetfile_contents[:modules].push([name, version])
214
+ else
215
+ next if line =~ /^forge/
216
+ next if line =~ /^\s+$/
217
+ next if line =~ /#{Puppetfile_Header}/
218
+ next if line =~ /#{Extras_Note}/
219
+
220
+ puppetfile_contents[:extras].push(line)
221
+ end
222
+ end
223
+
224
+ puppetfile_contents
225
+ end
226
+
181
227
  # Public: Verify that Puppet is available in the path
182
228
  def verify_puppet_exists
183
229
  MakeMakefile::Logging.instance_variable_set(:@logfile, File::NULL)
@@ -213,8 +259,8 @@ Your Puppetfile has been generated. Copy and paste between the markers:
213
259
  system(command)
214
260
  end
215
261
 
216
- # Public: generate the module data the @workspace
217
- def generate_module_data
262
+ # Public: generate the module data from the modulepath (@workspace)
263
+ def generate_module_data_from_modulepath
218
264
  command = "puppet module list #{@modulepath} 2>#{File::NULL}"
219
265
  puts "Calling '#{command}'" if @options[:debug]
220
266
  module_output = `#{command}`
@@ -233,6 +279,19 @@ Your Puppetfile has been generated. Copy and paste between the markers:
233
279
  modules
234
280
  end
235
281
 
282
+ # Public: generate the module data from an existing Puppetfile
283
+ def generate_module_data_from_Puppetfile
284
+ puppetfile_contents = read_puppetfile_with_versions(@options[:puppetfile])
285
+
286
+ modules = {}
287
+ puppetfile_contents[:modules].each do |name, version|
288
+ modules[name] = version
289
+ $stderr.puts "Module #{name} has a version of #{version}, it may be deprecated. For more information, visit https://forge.puppet.com/#{name}".blue if version =~ /999/ and ! @options[:silent]
290
+ end
291
+
292
+ modules
293
+ end
294
+
236
295
  # Public: generate the list of modules in Puppetfile format from the @workspace
237
296
  def generate_forge_module_output
238
297
  module_output = ''
@@ -260,6 +319,9 @@ forge 'http://forge.puppetlabs.com'
260
319
  puppetfile_contents += line.to_s
261
320
  end unless extras == []
262
321
 
322
+ # Strip out all contents with --ignore_comments
323
+ puppetfile_contents.gsub!(/^#.*$\n/, '') if @options[:ignore_comments]
324
+
263
325
  puppetfile_contents
264
326
  end
265
327
 
@@ -275,8 +337,6 @@ forge 'http://forge.puppetlabs.com'
275
337
 
276
338
  # Public: Generate a simple fixtures file.
277
339
  def generate_fixtures_data
278
- puts "\nGenerating .fixtures.yml using module name #{@options[:modulename]}" unless @options[:silent]
279
-
280
340
  # Determine if there are symlinks, either for the default modulename, or for anything in the modulepath
281
341
  symlinks = []
282
342
  modulepath = ''
@@ -319,12 +379,15 @@ forge 'http://forge.puppetlabs.com'
319
379
  fixtures_data += " forge_modules:\n" if @module_data != {}
320
380
  @module_data.keys.each do |modulename|
321
381
  shortname = modulename.split('/')[1]
322
- version = @module_data[modulename]
323
- fixtures_data += <<-EOF
382
+ version = @module_data[modulename]
383
+ data = <<-EOF
324
384
  #{shortname}:
325
385
  repo: "#{modulename}"
326
386
  ref: "#{version}"
327
387
  EOF
388
+ data.gsub!(/^ *ref.*$\n/, '') unless version != nil
389
+
390
+ fixtures_data += data
328
391
  end
329
392
 
330
393
  fixtures_data
@@ -28,11 +28,11 @@ module GeneratePuppetfile
28
28
  options[:create_puppetfile] = true
29
29
  end
30
30
 
31
- opts.on('-f', '--create-fixtures', 'Create a .fixtures.yml file in the working directory. This works in a module directory or at the top if your controlrepo..') do
31
+ opts.on('-f', '--create-fixtures', 'Create a .fixtures.yml file in the working directory. This works in a module directory or at the top of your controlrepo.') do
32
32
  options[:create_fixtures] = true
33
33
  end
34
34
 
35
- opts.on('-m', '--modulename NAME', "Name of the module the fixtures file will be used with. Optional, for use with --create-fixtures when used in a module directory. Defaults to 'profile'. ") do |name|
35
+ opts.on('-m', '--modulename NAME', "Name of the module the fixtures file will be used with. Optional, for use with --create-fixtures when used in a module directory. Defaults to 'profile'.") do |name|
36
36
  options[:modulename] = name
37
37
  end
38
38
 
@@ -45,6 +45,14 @@ module GeneratePuppetfile
45
45
  options[:debug] = true
46
46
  end
47
47
 
48
+ opts.on_tail('-i', '--ignore-comments', 'Ignore comments') do
49
+ options[:ignore_comments] = true
50
+ end
51
+
52
+ opts.on_tail('--fixtures-only', 'Create a .fixtures.yml file from an existing Puppetfile. Requires the -p option.') do
53
+ options[:fixtures_only] = true
54
+ end
55
+
48
56
  opts.on_tail('-v', '--version', 'Show version') do
49
57
  puts "generate-puppetfile v#{GeneratePuppetfile::VERSION}"
50
58
  exit
@@ -1,3 +1,3 @@
1
1
  module GeneratePuppetfile
2
- VERSION = '0.10.0'.freeze
2
+ VERSION = '0.11.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: generate-puppetfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Nelson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-06 00:00:00.000000000 Z
12
+ date: 2017-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colorize