Sha256: ec4308b5374e90b86af22985cac0a40f8def8cff5f28b49355090511eb56ffb2

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 KB

Contents

module Hookit
  module Resource
    class Directory < Base

      field :path
      field :recursive
      field :mode
      field :owner
      field :group
      
      actions :create, :delete
      default_action :create

      def initialize(name)
        path name unless path
        super
      end

      def run(action)
        case action
        when :create
          create!
          chown!
          chmod!
        when :delete
          delete!
        end
      end

      protected

      def create!
        return if ::File.exists? path
        cmd = "mkdir #{"-p " if recursive}#{path}"
        `#{cmd}`
        code = $?.exitstatus
        if code != 0
          raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
        end
      end

      def delete!
        return if not ::File.exists? path
        cmd = "rm -rf #{path}"
        `#{cmd}`
        code = $?.exitstatus
        if code != 0
          raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
        end
      end

      def chown!
        return unless owner or group
        if ::File.exists? path
          `chown #{(group.nil?) ? owner : "#{owner}:#{group}"} #{path}`
        end
      end

      def chmod!
        if ::File.exists? path and mode
          ::File.chmod(mode, path)
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hookit-1.0.0 lib/hookit/resource/directory.rb
hookit-0.12.3 lib/hookit/resource/directory.rb
hookit-0.12.2 lib/hookit/resource/directory.rb
hookit-0.12.1 lib/hookit/resource/directory.rb
hookit-0.12.0 lib/hookit/resource/directory.rb
hookit-0.11.2 lib/hookit/resource/directory.rb
hookit-0.11.1 lib/hookit/resource/directory.rb
hookit-0.11.0 lib/hookit/resource/directory.rb
hookit-0.10.0 lib/hookit/resource/directory.rb
hookit-0.9.2 lib/hookit/resource/directory.rb
hookit-0.9.0 lib/hookit/resource/directory.rb
hookit-0.8.0 lib/hookit/resource/directory.rb
hookit-0.7.11 lib/hookit/resource/directory.rb