Class: PuppetfileEditor::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetfile_editor/cli.rb

Overview

CLI methods

Instance Method Summary collapse

Constructor Details

#initialize(pfile_path) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/puppetfile_editor/cli.rb', line 8

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.message)
  rescue StandardError => e
    @logger.log_and_exit(e.message)
  end
end

Instance Method Details

#add(opts) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puppetfile_editor/cli.rb', line 49

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

#delete(opts) ⇒ Object



42
43
44
45
46
47
# File 'lib/puppetfile_editor/cli.rb', line 42

def delete(opts)
  warn_and_exit "Module #{opts[:name]} does not exist in your Puppetfile." unless @pfile.modules.key? opts[:name]

  @pfile.delete_module(opts[:name])
  @pfile.dump
end

#edit(opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppetfile_editor/cli.rb', line 20

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.message)
  end
  @pfile.dump
end

#format(opts) ⇒ Object



38
39
40
# File 'lib/puppetfile_editor/cli.rb', line 38

def format(opts)
  @pfile.dump(opts)
end

#merge(opts) ⇒ Object



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
# File 'lib/puppetfile_editor/cli.rb', line 69

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.mod_message(@pfile.modules[mod.name], indent)
      else
        mod.set_message('does not exist in source Puppetfile', :not_found)
        @logger.mod_message(mod, indent)
      end
    end
  end
  if opts[:stdout]
    $stdout.puts(@pfile.generate_puppetfile)
  else
    @pfile.dump
  end
end

#warn_and_exit(message) ⇒ Object



101
102
103
104
# File 'lib/puppetfile_editor/cli.rb', line 101

def warn_and_exit(message)
  warn message
  exit 1
end