Sha256: e0dc5fee16449cccb44c769813e965e3a2db90c98ce6a1f15b0bfd3f8a568759

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module SousChef
  module Resource
    class Directory < Base
      ACTIONS = %w[create delete]

      def initialize(*args)
        action :create
        super
      end

      def path(path=nil)
        set_or_return(:path, path) || name
      end

      def mode(mode=nil)
        set_or_return(:mode, mode)
      end

      def action(action=nil)
        set_or_return(:action, action && validate_action(action))
      end

      def to_script
        @script ||= begin
          setup
          __send__(action)
          [super, mode_command].compact.join("\n")
        end
      end

      protected
        def create
          command %{mkdir -p #{escape_path(path)}}
        end

        def delete
          command %{rmdir #{escape_path(path)}}
        end

        def validate_action(action)
          return action if ACTIONS.include?(action.to_s)
          raise ArgumentError, "Invalid action #{action}, only #{ACTIONS.join(', ')} allowed"
        end

        def mode_command
          if mode
            sprintf(%{chmod %04o %s}, mode, escape_path(path))
          end
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sous_chef-0.0.2 lib/sous_chef/resource/directory.rb
sous_chef-0.0.1 lib/sous_chef/resource/directory.rb