Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FilBot3/a7d62519eb62517a961c to your computer and use it in GitHub Desktop.
Save FilBot3/a7d62519eb62517a961c to your computer and use it in GitHub Desktop.
An example knife-plugin that displays Hello World and a user name.
#
#
#
#
# Loads the knife classes and code to be used.
require 'chef/knife'
module ExamplePlugin
# This module is arbritary and mainly for keeping your plugin code
# seperate. The module name can be anything that can help organize
# your code.
class HelloWorld < Chef::Knife
# This class will be displayed as the banner below.
# When putting the words HelloWorld are capitalized as such
# Chef will split by the capital words, but if the World was not
# capitalized, then it would be shown as one word.
# This is the "show help" bit. Doing the -h also shows the
# options defined below as well as the default ones.
banner "knife hello world -U USERNAME (options)"
# This method loads dependencies and require's only when this plugin
# is being used.
deps do
# Nothing here yet
end
# This defines the command line options, and there are more of them
# Its preferable to have as many options as possible.
option :user_name,
:short => "-U USER",
:long => "--ser_name USER",
:description => "This is just a user name.",
:default => "default_user"
# This is what is ran when the knife plugin is executed.
def run
puts "Hello World #{config[:user_name]}"
end
end
class HelloYou < Chef::Knife
banner "knife hello you (options)"
deps do
#
end
def run
puts "Hello You! I don't have a name."
end
end
class HelloUser < Chef::Knife
deps do
#
end
banner "knife hello user NAME (options)"
def run
puts "Hello #{ARGV[2]}"
puts "Hello #{name_args.first}"
end
end
class HelloToMe < Chef::Knife
deps do
#
end
banner "knife hello to me USER (options)"
def run
puts "Hello to me from #{name_args.first}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment