puppetfile_editor 0.2.0 → 0.3.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: f3364e07c1a49414ed25fecced5c31afd13a15be
4
- data.tar.gz: 3377a602b97e701d1f21a8dae430d9da65e418e4
3
+ metadata.gz: 0dd06f4116c562fe9db2c70ee918c1a178cffb2c
4
+ data.tar.gz: 51ef0ec280c7fbcbb2a225528579471b1451f05b
5
5
  SHA512:
6
- metadata.gz: 4768b06d6ea7345a02cdd938716fc61a44cbb156dd2fcf056eaff6bb79adc98670adb09c85b5090bfc86911642e941df02a4a447a47815cff93cdc2481b8f14d
7
- data.tar.gz: a3f6515c3d4ae5cd647e6fe8b25311d49e06de5b63abff8d44418e578cdc1769a90eb8e76a3d44353c17c3acf88438b6a5d4f7098767707b5dacd4b23a7c726a
6
+ metadata.gz: 227795078fcb7cc4b46ba31a482aec3cfd488a92c36f8c26bc4f0e7d06e9b133854c41d37dfca389ef9410fe8f09fc247655ee4ae951a68b0ec370ad0cbda84a
7
+ data.tar.gz: 8e982d0e9ac8dab8851de23d0056f0a67c4741a8dec1b7ff6eecd53e43f2baef092399a44854b42a10b970683675e05f2961de67b83302dd9cd81dcdf40e6694
data/CHANGELOG.md ADDED
@@ -0,0 +1,30 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/).
6
+
7
+ ## [0.3.0] - 2017-09-27
8
+ ### Added
9
+ - Puppetfile can be instantiated using provided contents.
10
+ - `Puppetfile#compare_with` method.
11
+
12
+ ### Changed
13
+ - Removed named parameters from `Puppetfile#initialize` to support older Rubies.
14
+ - Moved all IO to `Puppetfile::CLI`.
15
+
16
+ ### Fixed
17
+ - Proper status for `kept at ...` messages.
18
+ - Return earlier on `:matched` status so that no re-ordering of parameters can occur.
19
+ - Various CLI logic issues.
20
+
21
+ ### Removed
22
+ - No more old_hashes support.
23
+
24
+ ## [0.2.0] - 2017-09-25
25
+ ### Added
26
+ - Merge function.
27
+
28
+ ## [0.1.0] - 2017-09-20 [YANKED]
29
+ ### Added
30
+ - Initial release.
data/bin/pfile CHANGED
@@ -8,14 +8,7 @@ filename = File.basename(__FILE__)
8
8
  # Display help if no arguments are specified
9
9
  ARGV.push('-h') if ARGV.empty?
10
10
 
11
- options = { puppetfile: 'Puppetfile' }
12
- OptionParser.new do |parser|
13
- parser.banner = "Usage: #{filename} [options] [command] [command options]"
14
- parser.on('-v', '--[no-]verbose', 'Run verbosely') { |v| options[:verbose] = v }
15
- parser.on('-f', '--filename PUPPETFILE', 'Path to your Puppetfile.') do |puppetfile|
16
- options[:puppetfile] = puppetfile
17
- end
18
- end.order!
11
+ options = { puppetfile: './Puppetfile' }
19
12
 
20
13
  subcommands = {
21
14
  'edit' => OptionParser.new do |parser|
@@ -24,12 +17,7 @@ subcommands = {
24
17
  options[:name] = setting
25
18
  end
26
19
  parser.on('-u', '--update PARAM=VALUE', 'What to update') do |setting|
27
- if (match = setting.match(/^(\w+)=([^=]+)$/))
28
- options[:param], options[:value] = match[1], match[2]
29
- else
30
- warn 'Version must match PARAM=VALUE pattern'
31
- exit 1
32
- end
20
+ options[:version] = setting
33
21
  end
34
22
  end,
35
23
  'format' => OptionParser.new do |parser|
@@ -69,6 +57,20 @@ subcommands = {
69
57
  end,
70
58
  }
71
59
 
60
+ OptionParser.new do |parser|
61
+ parser.banner = "Usage: #{filename} [options] [command] [command options]"
62
+ parser.separator ''
63
+ parser.separator ' Supported commands:'
64
+ subcommands.keys.each do |command|
65
+ parser.separator " * #{command}"
66
+ end
67
+ parser.separator ''
68
+ parser.separator ' Supported global options:'
69
+ parser.on('-p', '--puppetfile PUPPETFILE', 'Path to your Puppetfile.') do |puppetfile|
70
+ options[:puppetfile] = puppetfile
71
+ end
72
+ end.order!
73
+
72
74
  command = ARGV.shift
73
75
  subcommands[command].order!
74
76
 
@@ -4,23 +4,32 @@ module PuppetfileEditor
4
4
  # CLI methods
5
5
  class CLI
6
6
  def initialize(pfile_path)
7
- @pfile = PuppetfileEditor::Puppetfile.new(path: pfile_path)
7
+ @pfile = PuppetfileEditor::Puppetfile.new(pfile_path)
8
8
  @logger = PuppetfileEditor::Logging.new
9
9
  begin
10
10
  @pfile.load
11
11
  rescue IOError, NoMethodError => e
12
- warn_and_exit(e.message)
12
+ @logger.log_and_exit(e.message)
13
+ rescue StandardError => e
14
+ @logger.log_and_exit(e.message)
13
15
  end
14
16
  end
15
17
 
16
18
  def edit(opts)
17
19
  opts[:value] = :latest if opts[:value] == 'latest'
18
- @pfile.update_module(
19
- opts[:name],
20
- opts[:param],
21
- opts[:value],
22
- opts[:verbose],
23
- )
20
+ @logger.log_and_exit('Please specify module name') unless opts[:name].is_a?(String)
21
+ @logger.log_and_exit('Please specify version') unless opts[:version].is_a?(String)
22
+ if (match = opts[:version].match(/^(\w+)=([^=]+)$/))
23
+ param = match[1]
24
+ value = match[2]
25
+ else
26
+ @logger.log_and_exit('Version must match PARAM=VALUE pattern')
27
+ end
28
+ begin
29
+ @pfile.update_module(opts[:name], param, value)
30
+ rescue StandardError => e
31
+ @logger.log_and_exit(e.message)
32
+ end
24
33
  @pfile.dump
25
34
  end
26
35
 
@@ -49,8 +58,12 @@ module PuppetfileEditor
49
58
  end
50
59
 
51
60
  def merge(opts)
52
- @pfdata = PuppetfileEditor::Puppetfile.new(from_stdin: true)
53
- @pfdata.load
61
+ @pfdata = PuppetfileEditor::Puppetfile.new(nil, true)
62
+ begin
63
+ @pfdata.load
64
+ rescue SyntaxError
65
+ @logger.log_and_exit('Format error.')
66
+ end
54
67
  new_mod_types = @pfdata.modules.values.group_by(&:type)
55
68
  new_mod_types.each do |mod_type, mods|
56
69
  puts "\n #{@pfile.module_sections[mod_type]}\n\n"
@@ -61,14 +74,11 @@ module PuppetfileEditor
61
74
  indent = mods.map(&:name).max_by(&:length).length
62
75
  mods.each do |mod|
63
76
  if @pfile.modules.key? mod.name
64
- begin
65
- @pfile.modules[mod.name].merge_with(mod, opts[:force])
66
- @logger.module_log(mod.name, indent, @pfile.modules[mod.name].message, @pfile.modules[mod.name].status)
67
- rescue StandardError => e
68
- @logger.module_log(mod.name, indent, e.message, @pfile.modules[mod.name].status)
69
- end
77
+ @pfile.modules[mod.name].merge_with(mod, opts[:force])
78
+ @logger.mod_message(@pfile.modules[mod.name], indent)
70
79
  else
71
- @logger.module_log(mod.name, indent, 'does not exist in source Puppetfile', :not_found)
80
+ mod.set_message('does not exist in source Puppetfile', :not_found)
81
+ @logger.mod_message(mod, indent)
72
82
  end
73
83
  end
74
84
  end
@@ -94,6 +104,7 @@ module PuppetfileEditor
94
104
  not_found: "[ \e[31;1mx\e[0m ]",
95
105
  type_mismatched: "[ \e[31;1mx\e[0m ]",
96
106
  wont_upgrade: "[ \e[33;1m!\e[0m ]",
107
+ warn: "[ \e[31;1m!!\e[0m ]",
97
108
  undef: '',
98
109
  }
99
110
  end
@@ -107,8 +118,14 @@ module PuppetfileEditor
107
118
  puts "#{status} #{message}"
108
119
  end
109
120
 
110
- def module_log(mod_name, indent, message, message_type = :undef)
111
- log("#{mod_name.ljust(indent)} => #{message}", message_type)
121
+ def log_and_exit(message)
122
+ log(message, :warn)
123
+ exit 1
112
124
  end
125
+
126
+ def mod_message(mod, indent)
127
+ log("#{mod.name.ljust(indent)} => #{mod.message}", mod.status)
128
+ end
129
+
113
130
  end
114
131
  end
@@ -31,43 +31,45 @@ module PuppetfileEditor
31
31
  def set(param, newvalue, force = false)
32
32
  case @type
33
33
  when :hg, :git
34
- if %w[branch tag ref].include? param
34
+ if !force && (@params.key?(:branch) || @params.key?(:ref))
35
+ set_message("kept at (#{full_version})", :wont_upgrade)
36
+ elsif !%w[branch tag ref].include? param
37
+ set_message("only 'branch', 'tag', and 'ref' are supported for '#{@type}' modules.", :unsupported)
38
+ else
39
+ set_message("updated (#{full_version} to #{param}: #{newvalue}", :updated)
35
40
  @params.delete :branch
36
41
  @params.delete :tag
37
42
  @params.delete :ref
38
43
  @params[param.to_sym] = newvalue
39
44
  calculate_indent
40
- else
41
- raise StandardError, "Only 'branch', 'tag', and 'ref' are supported for '#{@type}' modules."
42
45
  end
43
46
  when :forge
44
47
  if param == 'version'
45
48
  @params[:version] = newvalue
49
+ set_message("successfully set #{param} to #{newvalue} for #{@name}.", :updated)
46
50
  else
47
- raise StandardError, "Only 'version' is supported for forge modules."
51
+ set_message("only 'version' is supported for forge modules.", :unsupported)
48
52
  end
49
53
  else
50
- raise StandardError, "Editing params for '#{@type}' modules is not supported."
54
+ set_message("editing params for '#{@type}' modules is not supported.", :unsupported)
51
55
  end
52
56
  end
53
57
 
54
58
  def merge_with(mod, force = false)
55
59
  unless mod.type == @type
56
- @status = :type_mismatched
57
- raise(StandardError, "type mismatch ('#{@type}' vs '#{mod.type}')")
60
+ set_message("type mismatch ('#{@type}' vs '#{mod.type}')", :type_mismatched)
58
61
  end
59
62
  case @type
60
63
  when :hg, :git
61
64
  new = mod.params.reject { |param, _| param.eql? @type }
62
65
  if !force && new.keys == [:tag] && (@params.key?(:branch) || @params.key?(:ref))
63
- raise(StandardError, "kept at #{full_version}")
66
+ set_message("kept at #{full_version}", :wont_upgrade)
64
67
  end
65
68
  if full_version == mod.full_version
66
- @message = "versions match (#{full_version})"
67
- @status = :matched
69
+ set_message("versions match (#{full_version})", :matched)
70
+ return
68
71
  else
69
- @message = "updated (#{full_version} to #{mod.full_version})"
70
- @status = :updated
72
+ set_message("updated (#{full_version} to #{mod.full_version})", :updated)
71
73
  end
72
74
  @params.delete_if { |param, _| [:branch, :tag, :ref].include? param }
73
75
  @params.merge!(new)
@@ -75,25 +77,22 @@ module PuppetfileEditor
75
77
  when :forge
76
78
  unless force
77
79
  if mod.params.nil? or mod.params.is_a? Symbol
78
- @status = :wont_upgrade
79
- raise(StandardError, "won't upgrade to #{mod.full_version}")
80
+ set_message("won't upgrade to #{mod.full_version}", :wont_upgrade)
80
81
  end
81
82
  end
82
83
  if full_version == mod.full_version
83
- @message = "versions match (#{full_version})"
84
- @status = :matched
84
+ set_message("versions match (#{full_version})", :matched)
85
+ return
85
86
  else
86
- @message = "updated (#{full_version} to #{mod.full_version})"
87
- @status = :updated
87
+ set_message("updated (#{full_version} to #{mod.full_version})", :updated)
88
88
  end
89
89
  @params = mod.params
90
90
  else
91
- @status = :skipped
92
- raise(StandardError, 'only git, forge, and hg modules are supported for merging')
91
+ set_message('only git, forge, and hg modules are supported for merging', :skipped)
93
92
  end
94
93
  end
95
94
 
96
- def dump(old_hashes = false)
95
+ def dump
97
96
  output = []
98
97
  case @type
99
98
  when :hg, :git
@@ -104,7 +103,7 @@ module PuppetfileEditor
104
103
  else
105
104
  "'#{param_value}'"
106
105
  end
107
- param = old_hashes ? ":#{param_name.to_s.ljust(@indent - 1)} =>" : "#{param_name}:".ljust(@indent)
106
+ param = "#{param_name}:".ljust(@indent)
108
107
  output.push " #{param} #{value}"
109
108
  end
110
109
  when :local
@@ -136,6 +135,11 @@ module PuppetfileEditor
136
135
  end
137
136
  end
138
137
 
138
+ def set_message(message, status)
139
+ @message = message
140
+ @status = status
141
+ end
142
+
139
143
  private
140
144
 
141
145
  def parse_title(title)
@@ -14,10 +14,10 @@ module PuppetfileEditor
14
14
  attr_reader :module_sections
15
15
 
16
16
  # @param [String] path path to Puppetfile
17
- def initialize(path: 'Puppetfile', from_stdin: false, old_hashes: false)
17
+ def initialize(path = 'Puppetfile', from_stdin = false, contents = nil)
18
18
  @puppetfile_path = path
19
19
  @from_stdin = from_stdin
20
- @old_hashes = old_hashes
20
+ @contents = contents
21
21
  @modules = {}
22
22
  @loaded = false
23
23
  @forge = nil
@@ -34,9 +34,10 @@ module PuppetfileEditor
34
34
  def load
35
35
  puppetfile_contents = if @from_stdin
36
36
  $stdin.gets(nil).chomp
37
+ elsif @contents
38
+ @contents
37
39
  else
38
- raise(IOError, "Puppetfile #{@puppetfile_path} missing or unreadable") unless File.readable?(@puppetfile_path)
39
-
40
+ raise(IOError, "'#{@puppetfile_path}' is missing or unreadable") unless File.readable?(@puppetfile_path)
40
41
  File.read @puppetfile_path
41
42
  end
42
43
 
@@ -57,7 +58,7 @@ module PuppetfileEditor
57
58
  next unless module_list.any?
58
59
  contents.push "# #{module_comment}"
59
60
  module_list.values.sort_by(&:name).each do |mod|
60
- contents.push mod.dump(@old_hashes)
61
+ contents.push mod.dump
61
62
  end
62
63
  contents.push ''
63
64
  end
@@ -69,18 +70,35 @@ module PuppetfileEditor
69
70
  File.write(@puppetfile_path, generate_puppetfile) if @loaded
70
71
  end
71
72
 
72
- def update_module(name, param, value, verbose = false)
73
+ def update_module(name, param, value)
73
74
  if @modules.key? name
74
- begin
75
- @modules[name].set(param, value)
76
- puts "Successfully set #{param} to #{value} for #{name}." if verbose
77
- rescue StandardError => e
78
- warn e.message
79
- exit 1
80
- end
75
+ @modules[name].set(param, value)
81
76
  else
82
- warn "Module #{name} does not exist in your Puppetfile"
77
+ raise StandardError, "Module #{name} does not exist in your Puppetfile"
78
+ end
79
+ end
80
+
81
+ def compare_with(pf)
82
+ diff = {}
83
+ pf.modules.each do |mod_name, mod|
84
+ next unless [:git, :hg, :forge].include? mod.type
85
+ version_key = @type == :forge ? :version : :tag
86
+
87
+ unless @modules.key? mod_name
88
+ if mod.params.key?(version_key)
89
+ diff[mod_name] = { new: mod.params[version_key] }
90
+ end
91
+ next
92
+ end
93
+
94
+ local_mod = @modules[mod_name]
95
+
96
+ next unless mod.type == local_mod.type
97
+ next unless mod.params.key?(version_key) && local_mod.params.key?(version_key)
98
+ next if mod.params[version_key] == local_mod.params[version_key]
99
+ diff[mod_name] = { old: local_mod.params[version_key], new: mod.params[version_key] }
83
100
  end
101
+ diff
84
102
  end
85
103
 
86
104
  # @param [String] name Module name
@@ -1,3 +1,3 @@
1
1
  module PuppetfileEditor
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppetfile_editor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Piven
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-25 00:00:00.000000000 Z
11
+ date: 2017-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,6 +61,7 @@ executables:
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
+ - CHANGELOG.md
64
65
  - README.md
65
66
  - bin/pfile
66
67
  - lib/puppetfile_editor.rb