Skip to content

Commit

Permalink
Added option parsing and common options
Browse files Browse the repository at this point in the history
Added --template command line option

Added --configdir command line option

Removed .gemspec

Added CHANGELOG.rdoc

Updated README.rdoc
  • Loading branch information
jamtur01 committed May 9, 2010
1 parent 468bb90 commit 81fd57d
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 70 deletions.
28 changes: 0 additions & 28 deletions .gemspec

This file was deleted.

20 changes: 20 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Puppet Scaffold
===============

0.0.3
-----

- Added --template command line option
- Added --configdir command line option
- Added optparse
- Removed .gemspec and included this in Rakefile

0.0.2
-----

- Fixed bug with template directory

0.0.1
-----

- Initial release
22 changes: 14 additions & 8 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,32 @@ Creates:

== SYNOPSIS:

Use the --template option to specify the type of template you want to create.

Create a module:
$ scaffold module module_name
$ scaffold --template=module module

Create a node:
$ scaffold node node_name
$ scaffold --template=node node

Create a class:
$ scaffold class module_name class_name
$ scaffold --template=class module class

Create a definition:
$ scaffold define module_name define_name
$ scaffold --template=define module define

Create a function:
$ scaffold function module_name function_name function_type
$ scaffold --template=function module function type

Create a type/provider:
$ scaffold type module_name type_name
$ scaffold --template=type module type

Create Puppet configuration:
$ scaffold puppet
$ scaffold --template=puppet

Puppet configuration is specified in the directory specified in the Puppet confdir variable but you can override this with the --configdir option.

$ scaffold --template=puppet --configdir=/opt/puppet

== REQUIREMENTS:

Expand All @@ -52,7 +58,7 @@ Create Puppet configuration:

== LICENSE:

(The MIT License)
The MIT License

Copyright (c) 2010 James Turnbull

Expand Down
35 changes: 28 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
require 'rubygems'
require 'newgem/tasks'
require 'rake'
require 'rake/gempackagetask'
require 'fileutils'
require 'templater'
require File.dirname(__FILE__)+'/lib/scaffold.rb'
require 'ftools'

version = Scaffold::VERSION
version = File.read('lib/scaffold.rb')[/VERSION *= *'(.*)'/,1] or fail "Couldn't find Scaffold version"

desc "Build gem"
task :build do
system "gem build .gemspec"
GEM_FILES = FileList[
'[A-Z]*',
'bin/**/*',
'lib/**/*',
]

spec = Gem::Specification.new do |spec|
spec.name = 'scaffold'
spec.files = GEM_FILES.to_a
spec.executables = 'scaffold'
spec.version = version
spec.add_dependency('templater', '>= 0.5.0')
spec.summary = 'Scaffold is a templating tool for Puppet'
spec.description = 'Scaffold allows you to create basic Puppet configuration, modules, nodes, classes, functions and types.'
spec.author = 'James Turnbull'
spec.email = 'james@lovedthanlost.net'
spec.homepage = 'http://github.com/jamtur01/puppet-scaffold'
spec.rdoc_options = ["--main", "README.rdoc"]
spec.require_paths = ["lib"]
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_zip = true
pkg.need_tar = true
end

desc "Release gem to Gemcutter"
task :release => :build do
system "gem push scaffold-#{version}.gem"
Expand Down
6 changes: 2 additions & 4 deletions TODO.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
= scaffold TODO

* Create generators for:
- Check for presence of modules before creation
- Tests

- Check for presence of modules before creation
- Tests
87 changes: 73 additions & 14 deletions bin/scaffold
Original file line number Diff line number Diff line change
@@ -1,21 +1,80 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'puppet'
require 'templater'
require File.dirname(__FILE__)+'/../lib/scaffold.rb'
require 'optparse'
require 'scaffold'

Puppet.parse_config
TEMPLATES = %w[puppet module node class define function type]

case ARGV[0]
when 'puppet', 'node'
options = {}

optparse = OptionParser.new do |opts|
# Set a banner, displayed at the top
# of the help screen.
opts.banner = 'Usage: scaffold [options] --template="template" options ...'

opts.separator ''
opts.separator 'Configuration options:'

options[:configfile] = nil
opts.on( '-f', '--configfile=FILE', 'Use file as configuration file') do |file|
options[:configfile] = file
end

options[:configdir] = nil
opts.on( '-c', '--configdir=DIRECTORY', 'Specify the location of your Puppet configuration directory') do |confdir|
options[:configdir] = confdir
end

options[:templatedir] = nil
opts.on( '--templatedir=DIRECTORY', 'Use directory as template loation') do |templatedir|
options[:templatedir] = templatedir
end

options[:template] = nil
opts.on( '--template=TEMPLATE', TEMPLATES, 'Template to create') do |template|
options[:template] = template
end

opts.separator ""
opts.separator "Common options:"

opts.on_tail('-v', '--version', 'Show version') do
puts Scaffold::VERSION
exit
end

opts.on_tail('-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end

begin
optparse.parse!
mandatory = [:template]
missing = mandatory.select{ |param| options[param].nil? }
if not missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts optparse
exit
end
rescue OptionParser::InvalidArgument, OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
end

if options[:template] == 'puppet'
if options[:configdir]
dir = options[:configdir]
else
dir = Puppet[:confdir]
puts "#{dir}"
Scaffold::Generator.run_cli(dir, 'scaffold', Scaffold::VERSION, ARGV)
when 'module', 'class', 'define'
dir = Puppet[:modulepath].split(':')
Scaffold::Generator.run_cli(dir[0], 'scaffold', Scaffold::VERSION, ARGV)
when 'function', 'type'
dir = Puppet[:modulepath].split(':')
Scaffold::Generator.run_cli(dir[0], 'scaffold', Scaffold::VERSION, ARGV)
end
else
dir = Puppet[:modulepath].split(':')
end

ARGV.unshift(options[:template])

Scaffold::Generator.run_cli dir.to_s, 'scaffold', Scaffold::VERSION, ARGV
29 changes: 28 additions & 1 deletion lib/scaffold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@

require 'rubygems'
require 'templater'

require 'puppet'
require path + 'generator'

module Scaffold

Puppet.parse_config

VERSION = '0.0.2'

def config
config = {}

configfile = options[:configfile] || Puppet[:confdir] + 'scaffold.conf'

File.foreach(configfile) do |line|
line.strip!
# Skip comments and whitespace
if (line[0] != ?# and line =~ /\S/ )
i = line.index('=')
if (i)
config[line[0..i - 1].strip] = line[i + 1..-1].strip
else
config[line] = ''
end
end
end

# Print it out
config.each do |key, value|
print key + " = " + value
print "\n"
end
end

end
16 changes: 8 additions & 8 deletions lib/scaffold/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ module Generator
##
class ModuleGenerator < Templater::Generator
desc <<-DESC
Create an empty Puppet module
Create an empty Puppet module. You must specify the name of the module.
DESC

first_argument :module_name, :required => true, :desc => "Your module name."

def self.source_root
File.join(File.dirname(__FILE__), 'templates/module')
File.join(File.dirname(__FILE__), 'templates/module')
end

# Create all subsdirectories
Expand Down Expand Up @@ -53,7 +53,7 @@ def self.source_root

class NodeGenerator < Templater::Generator
desc <<-DESC
Generate a basic Puppet node
Generate a basic Puppet node. You must specify the name of the node.
DESC

first_argument :node_name, :required => true, :desc => "Your node name."
Expand All @@ -76,7 +76,7 @@ def self.source_root

class ClassGenerator < Templater::Generator
desc <<-DESC
Create a Puppet class.
Create a Puppet class. You must specify the name of the module to create the class in and the name of the class.
DESC

first_argument :module_name, :required => true, :desc => "The module that contains the class"
Expand All @@ -94,7 +94,7 @@ def self.source_root

class DefineGenerator < Templater::Generator
desc <<-DESC
Create a Puppet definition.
Create a Puppet definition. You must specify the name of the module to create the definition in and the name of the definition.
DESC

first_argument :module_name, :required => true, :desc => "The module that contains the definition"
Expand All @@ -112,7 +112,7 @@ def self.source_root

class FunctionGenerator < Templater::Generator
desc <<-DESC
Create a Puppet function.
Create a Puppet function. Specify the name of the module, the name of the function and the type of the function, either statement or rvalue. Generator defaults to statement.
DESC

first_argument :module_name, :required => true, :desc => "The module that contains the function"
Expand All @@ -136,7 +136,7 @@ def self.source_root

class TypeGenerator < Templater::Generator
desc <<-DESC
Create a Puppet type and provider.
Create a Puppet type and provider. You must specify the module to create the type and provider in and the name of the type to be created.
DESC

first_argument :module_name, :required => true, :desc => "The module that contains the type"
Expand Down Expand Up @@ -168,7 +168,7 @@ def self.source_root

class PuppetGenerator < Templater::Generator
desc <<-DESC
Generate a basic Puppet configuration - specify the location of your Puppet configuration directory, for example /etc/puppet.
Generate a basic Puppet configuration.
DESC

def self.source_root
Expand Down

0 comments on commit 81fd57d

Please sign in to comment.