Sha256: dd8ad365b8c820350d72ab598842dda8581ac679df85e275a6b99b56cbff7264

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

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

      def initialize(*args)
        action :create
        force false
        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 force(forced)
        @forced = forced
      end

      def forced?
        @forced
      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
          cmd = forced?? 'rm -rf' : 'rmdir'
          command %{#{cmd} #{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.4 lib/sous_chef/resource/directory.rb
sous_chef-0.0.3 lib/sous_chef/resource/directory.rb