Class: KnifePlugins::PluginCreate

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/plugin_create.rb

Overview

namespaced for the knife plugin magic

Instance Method Summary collapse

Instance Method Details

#plugin_classObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/knife/plugin_create.rb', line 44

def plugin_class
  # TODO: There exist methods for making snake case and camelcase
  # strings, but I couldn't internet on a plane. :)
  class_name = ''
  if @name_args.length > 1
    class_name = @name_args.map { |p| p.capitalize }.join
  else
    class_name = @name_args[0].capitalize
  end
  class_name
end

#plugin_fileObject



56
57
58
# File 'lib/chef/knife/plugin_create.rb', line 56

def plugin_file
  @name_args.join('_').downcase
end

#plugin_scaffold(plugin_class) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef/knife/plugin_create.rb', line 60

def plugin_scaffold(plugin_class)
  scaffolding = "# You should replace this comment with a license header\n# \#{plugin_file}\nrequire 'chef/knife'\n\nmodule KnifePlugins\n  class \#{plugin_class} < Chef::Knife\ndeps do\n\nend\n\nbanner \"knife \"\n\ndef run\n\nend\n  end\nend\n"
  scaffolding
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chef/knife/plugin_create.rb', line 29

def run
  class_name = plugin_class
  # TODO: Make this location configurable
  plugin_dir = File.join(ENV['HOME'], '.chef', 'plugins', 'knife')
  file_name = File.join(plugin_dir, "#{plugin_file}.rb")
  if ::File.exists?(file_name)
    ui.info "Plugin #{class_name} may already exist at #{file_name}"
  else
    FileUtils.mkdir_p(plugin_dir)
    file = ::File.open(file_name, 'w')
    file.puts plugin_scaffold(plugin_class)
    ui.info plugin_scaffold(plugin_class)
  end
end