Sha256: 633e3c4ac59b7ed0fae1842455c50b7106a5fba9acc2f6c5f745aed69771ae29

Contents?: true

Size: 1.23 KB

Versions: 19

Compression:

Stored size: 1.23 KB

Contents

require 'guignol/configuration'
require 'guignol/commands/base'
require 'uuidtools'
require 'yaml'


Guignol::Shell.class_eval do
  desc 'clone SOURCE', 'Print a new config similar to the server named SOURCE'
  method_option :name,
    :aliases => %w(-n),
    :type => :string, :default => 'new-server',
    :desc => 'Name to use for the new server'
  def clone(source)
    Guignol::Commands::Clone.new(source, options[:name]).run
  end
end


module Guignol::Commands
  class Clone
    def initialize(source_name, target_name)
      @source_name = source_name
      @target_name = target_name

      @source_config = Guignol.configuration[source_name]
      unless @source_config
        raise Thor::Error.new "machine '#{source_name}' is unknown"
      end
    end


    def run
      new_config = @source_config.map_to_hash(:deep => true) do |key,value|
        value = value.gsub(/#{@source_name}/, @target_name) if value.kind_of?(String)
        key   = key.gsub(  /#{@source_name}/, @target_name) if key.kind_of?(String)

        case key
        when :uuid
          [key, UUIDTools::UUID.random_create.to_s.upcase]
        else
          [key, value]
        end
      end

      $stdout.puts({@target_name => new_config}.to_yaml)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
guignol-0.3.16 lib/guignol/commands/clone.rb
guignol-0.3.15 lib/guignol/commands/clone.rb
guignol-0.3.14 lib/guignol/commands/clone.rb
guignol-0.3.13 lib/guignol/commands/clone.rb
guignol-0.3.12 lib/guignol/commands/clone.rb
guignol-0.3.10 lib/guignol/commands/clone.rb
guignol-0.3.9 lib/guignol/commands/clone.rb
guignol-0.3.8 lib/guignol/commands/clone.rb
guignol-0.3.7 lib/guignol/commands/clone.rb
guignol-0.3.6.2 lib/guignol/commands/clone.rb
guignol-0.3.6.1 lib/guignol/commands/clone.rb
guignol-0.3.6 lib/guignol/commands/clone.rb
guignol-0.3.5.1 lib/guignol/commands/clone.rb
guignol-0.3.5 lib/guignol/commands/clone.rb
guignol-0.3.4 lib/guignol/commands/clone.rb
guignol-0.3.3 lib/guignol/commands/clone.rb
guignol-0.3.2 lib/guignol/commands/clone.rb
guignol-0.3.1 lib/guignol/commands/clone.rb
guignol-0.3.0 lib/guignol/commands/clone.rb