Class: PuppetfileEditor::CLI
- Inherits:
-
Object
- Object
- PuppetfileEditor::CLI
- Defined in:
- lib/puppetfile_editor/cli.rb
Overview
CLI methods
Instance Method Summary collapse
- #add(opts) ⇒ Object
- #edit(opts) ⇒ Object
- #format(_) ⇒ Object
-
#initialize(pfile_path) ⇒ CLI
constructor
A new instance of CLI.
- #merge(opts) ⇒ Object
- #warn_and_exit(message) ⇒ Object
Constructor Details
#initialize(pfile_path) ⇒ CLI
Returns a new instance of CLI.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/puppetfile_editor/cli.rb', line 6 def initialize(pfile_path) @pfile = PuppetfileEditor::Puppetfile.new(pfile_path) @logger = PuppetfileEditor::Logging.new begin @pfile.load rescue IOError, NoMethodError => e @logger.log_and_exit(e.) rescue StandardError => e @logger.log_and_exit(e.) end end |
Instance Method Details
#add(opts) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/puppetfile_editor/cli.rb', line 40 def add(opts) warn_and_exit "Module #{opts[:name]} is already present in your Puppetfile." if @pfile.modules.key? opts[:name] case opts[:type] when :hg, :git warn_and_exit 'URL must be provided for Git and Hg modules' unless opts.key? :url warn_and_exit 'Version must be provided for Git and Hg modules' unless opts.key? :param opts[:value] = :latest if opts[:value] == 'latest' @pfile.add_module(opts[:name], opts[:type] => opts[:url], opts[:param] => opts[:value]) when :local @pfile.add_module(opts[:name], :local) when :forge warn_and_exit 'Version must be provided for Forge modules' unless opts.key? :version @pfile.add_module(opts[:name], opts[:version]) else warn_and_exit 'Only hg, git, local, and forge modules are supported at the moment.' end @pfile.dump end |
#edit(opts) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/puppetfile_editor/cli.rb', line 18 def edit(opts) opts[:value] = :latest if opts[:value] == 'latest' @logger.log_and_exit('Please specify module name') unless opts[:name].is_a?(String) @logger.log_and_exit('Please specify version') unless opts[:version].is_a?(String) if (match = opts[:version].match(/^(\w+)=([^=]+)$/)) param = match[1] value = match[2] else @logger.log_and_exit('Version must match PARAM=VALUE pattern') end begin @pfile.update_module(opts[:name], param, value) rescue StandardError => e @logger.log_and_exit(e.) end @pfile.dump end |
#format(_) ⇒ Object
36 37 38 |
# File 'lib/puppetfile_editor/cli.rb', line 36 def format(_) @pfile.dump end |
#merge(opts) ⇒ Object
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 |
# File 'lib/puppetfile_editor/cli.rb', line 60 def merge(opts) @pfdata = PuppetfileEditor::Puppetfile.new(nil, true) begin @pfdata.load rescue SyntaxError @logger.log_and_exit('Format error.') end new_mod_types = @pfdata.modules.values.group_by(&:type) new_mod_types.each do |mod_type, mods| puts "\n #{@pfile.module_sections[mod_type]}\n\n" unless [:hg, :git, :forge].include? mod_type puts " Skipping #{mod_type} section." next end indent = mods.map(&:name).max_by(&:length).length mods.each do |mod| if @pfile.modules.key? mod.name @pfile.modules[mod.name].merge_with(mod, opts[:force]) @logger.(@pfile.modules[mod.name], indent) else mod.('does not exist in source Puppetfile', :not_found) @logger.(mod, indent) end end end if opts[:stdout] $stdout.puts(@pfile.generate_puppetfile) else @pfile.dump end end |
#warn_and_exit(message) ⇒ Object
92 93 94 95 |
# File 'lib/puppetfile_editor/cli.rb', line 92 def warn_and_exit() warn exit 1 end |