Sha256: 9703657650c14f8b57a238d557176fbd855b0a51dca1769f70f4f3e7d53c6c51

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module SousChef
  module Resource
    class File < Directory
      def content(content=nil)
        set_or_return(:content, content && content.to_s)
      end

      protected
        def escaped_content
          content
        end

        def create
          not_if file_exist_command unless forced?
          command create_file_command
        end

        def delete
          only_if file_exist_command unless forced?
          command "rm #{'-f ' if forced?}#{escape_path(path)}"
        end

        def file_exist_command
          %{test -e #{escape_path(path)}}
        end

        def heredoc
          @heredoc ||= begin
            candidate = "SousChefHeredoc"
            candidate += "1" while content.include?(candidate)
            candidate
          end
        end

        def create_file_command
          if content
            lines = []
            lines << "cat <<'#{heredoc}' > #{escape_path(path)}"
            lines << escaped_content
            lines << heredoc
            lines.join("\n")
          else
            %{touch #{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/file.rb
sous_chef-0.0.3 lib/sous_chef/resource/file.rb